某些二維碼中間有圖片,能夠直接在imageView添加圖片居中 //1.建立過濾器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; //2.恢復默認 [filter setDefaults]; //3.給過濾器添加數據 NSString *str = @"二維碼學習"; //也直接輸出網址,掃描會自動跳轉 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKey:@"inputMessage"]; //4.獲取輸出二維碼 CIImage *outputImage = [filter outputImage]; //5.顯示二維碼 self.imageView.image = [UIImage imageWithCIImage:outputImage]; //解決模糊方法 // self.imageView.image = [self createNonInterpolatedUIImageFromCIImage:outputImage withSize:200];
生成二維碼解決方法session
-(UIImage *)createNonInterpolatedUIImageFromCIImage:(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]; }
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> @property (weak, nonatomic)AVCaptureSession *session; @property (weak, nonatomic)AVCaptureVideoPreviewLayer *layer; - (IBAction)scan:(UIButton *)sender { //1.建立捕捉會話 AVCaptureSession *session = [[AVCaptureSession alloc] init]; self.session = session; //2.設置輸入設備 設置爲攝像頭 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; [session addInput:inputDevice]; //3.設置輸出方式 代理數據輸出數據 AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [session addOutput:output]; [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; //4.添加一個顯示的layer 相似攝像頭拍照閱覽 AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession: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) { //1.獲取掃描結果 AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject]; self.outLabel.text = object.stringValue; //2.中止掃描 [self.session stopRunning]; //3.移除圖層 [self.layer removeFromSuperlayer]; }else{ NSLog(@"沒有掃描到數據"); } }
導入必要的框架框架
實現ZBarReaderDelegate協議ide
//二維碼識別 -(void)sysbutbuttonclick{ //初始化相機控制器 ZBarReaderViewController *reader = [ZBarReaderViewController new]; //設置代理 reader.readerDelegate = self; //基本適配 reader.supportedOrientationsMask = ZBarOrientationMaskAll; //二維碼/條形碼識別設置 ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; //彈出系統照相機,全屏拍攝 [self presentViewController:reader animated:YES completion:nil]; } - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { NSLog(@"info=%@",info); // 獲得條形碼結果 id<NSFastEnumeration> results =[info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) // EXAMPLE: just grab the first barcode break; NSLog(@"%@",symbol.data); }
實現ZBarReaderViewDelegate協議學習
//二維碼識別 -(void)sysbutbuttonclick{ //初始化照相機窗口 ZBarReaderView *readview = [ZBarReaderView new]; //自定義大小 readview.frame = myScreen; //自定義添加相關指示.........發揮各自的APP的想象力 //此處省略美化10000行代碼................... //……………………….. // 好進入正題—— 接着設置好代理 readview.readerDelegate = self; //將其照相機拍攝視圖添加到要顯示的視圖上 [self.view addSubview:readview]; //二維碼/條形碼識別設置 ZBarImageScanner *scanner = readview.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; //啓動,必須啓動後,手機攝影頭拍攝的即時圖像菜能夠顯示在readview上 [readview start]; } -(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image { ZBarSymbol *symbol = nil; for (symbol in symbols) { break; } NSLog(@"========%@==========",symbol.data); }