国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS二維碼的生成和掃描

2019-10-21 18:48:00
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下

屬性

@property (strong,nonatomic)AVCaptureDevice * device;@property (strong,nonatomic)AVCaptureDeviceInput * input;@property (strong,nonatomic)AVCaptureMetadataOutput * output;@property (strong,nonatomic)AVCaptureSession * session;@property (strong,nonatomic)AVCaptureVideoPreviewLayer * layer; @property (nonatomic, strong)UIImageView *imageView;

二維碼的生成

// 1.創建過濾器  CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];   // 2.恢復默認  [filter setDefaults];   // 3.給過濾器添加數據(正則表達式/賬號和密碼)  NSString *dataString = @"http://www.520it.com";  NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];  [filter setValue:data forKeyPath:@"inputMessage"];   // 4.獲取輸出的二維碼  CIImage *outputImage = [filter outputImage];   //因為生成的二維碼模糊,所以通過createNonInterpolatedUIImageFormCIImage:outputImage來獲得高清的二維碼圖片   // 5.顯示二維碼  self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

 * createNonInterpolatedUIImageFormCIImage:outputImage方法的實現

/** * 根據CIImage生成指定大小的UIImage * * @param image CIImage * @param size 圖片寬度 */- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size{  CGRect extent = CGRectIntegral(image.extent);  CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));   // 1.創建bitmap;  size_t width = CGRectGetWidth(extent) * scale;  size_t height = CGRectGetHeight(extent) * scale;  CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();  CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);  CIContext *context = [CIContext contextWithOptions:nil];  CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];  CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);  CGContextScaleCTM(bitmapRef, scale, scale);  CGContextDrawImage(bitmapRef, extent, bitmapImage);   // 2.保存bitmap到圖片  CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);  CGContextRelease(bitmapRef);  CGImageRelease(bitmapImage);  return [UIImage imageWithCGImage:scaledImage];}

 二維碼的掃描

// 1.創建捕捉會話  AVCaptureSession *session = [[AVCaptureSession alloc] init];  self.session = session;   // 2.添加輸入設備(數據從攝像頭輸入)  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];  [session addInput:input];   // 3.添加輸出數據(示例對象-->類對象-->元類對象-->根元類對象)  AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];  [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];  [session addOutput:output];   // 3.1.設置輸入元數據的類型(類型是二維碼數據)  [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];   // 4.添加掃描圖層  AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];  layer.frame = self.view.bounds;  [self.view.layer addSublayer:layer];  self.layer = layer;   // 5.開始掃描  [session startRunning];

 *掃描到結果后會調用的方法

// 當掃描到數據時就會執行該方法- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{  if (metadataObjects.count > 0) {    //獲得掃描數據,最后一個時最新掃描的數據    AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];    NSLog(@"%@", object.stringValue);     // 停止掃描    [self.session stopRunning];     // 將預覽圖層移除    [self.layer removeFromSuperlayer];  } else {    NSLog(@"沒有掃描到數據");  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 饶河县| 吉首市| 太和县| 遵化市| 谷城县| 南和县| 罗源县| 云梦县| 安丘市| 商丘市| 临邑县| 清流县| 伊通| 乳山市| 西贡区| 通城县| 大同县| 陵川县| 隆安县| 大城县| 乡城县| 绥滨县| 吉安市| 常宁市| 滕州市| 凉山| 云南省| 绵阳市| 汉阴县| 雷波县| 鲁山县| 阳城县| 江永县| 利辛县| 福海县| 遵化市| 浑源县| 革吉县| 五华县| 临安市| 临洮县|