【IOS】集成zxing(二维码扫描)
生活随笔
收集整理的這篇文章主要介紹了
【IOS】集成zxing(二维码扫描)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
現在zxing已經到了2.2版本,以前的集成方式出了點問題。下面我做出一點修正。
以前的版本的集成方法,參考:http://blog.devtang.com/blog/2012/12/23/use-zxing-library/
按照以前的方式做好后 ?然后就是適配以下現在的版本的修改
1.增加 ??SenTestingKit ?框架 設置為Optional
2.把目錄 iphone/ZXingWidget 下的Tests刪了
3.保留目錄 cpp/core/src 的bigint文件
最后提醒: ?設置查找路徑時應該選擇 ?User Header Search Paths 選項添加查找路徑
保險一點把 ?Always Search User Paths ?選為Yes 這樣就可以了
在線生成二維碼的網站:http://cli.im/ 【拓展:特定區域掃描】
二維碼掃描處理過程:
程序有一個回調方法獲取屏幕圖片,然后進行解析。掃描會不斷獲取屏幕圖片去解析。
自定義掃描區域思路:
只要我們在回調方法里面將獲取的整個屏幕圖片截取某一個區域,就可以實現特定區域掃描功能
1 //回調方法 2 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 3 { 4 // 第一步,將sampleBuffer轉成UIImage 5 UIImage *image = [self imageFromSampleBuffer:sampleBuffer]; 6 // 第二步,用Decoder識別圖象 7 [self decodeImage:image]; 8 } 9 10 #pragma mark - 截取圖片 11 12 - (void)cutMapView:(UIView *)theView 13 14 { 15 16 //************** 得到圖片 ******************* 17 18 CGRect rect = theView.frame; //截取圖片大小 19 20 21 22 //開始取圖,參數:截圖圖片大小 23 24 UIGraphicsBeginImageContext(rect.size); 25 26 //截圖層放入上下文中 27 28 [theView.layer renderInContext:UIGraphicsGetCurrentContext()]; 29 30 //從上下文中獲得圖片 31 32 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 33 34 //結束截圖 35 36 UIGraphicsEndImageContext(); 37 38 39 40 41 42 //************** 存圖片 ******************* 43 44 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 45 46 NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.png",@"jietu"]]; // 保存文件的名稱 47 48 NSLog(@"filePath = %@",filePath); 49 50 //UIImagePNGRepresentation方法將image對象轉為NSData對象 51 52 //寫入文件中 53 54 BOOL result = [UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES]; 55 56 NSLog(@"result = %d",result); 57 58 59 60 61 62 //*************** 截取小圖 ****************** 63 64 CGRect rect1 = CGRectMake(90, 0, 82, 82);//創建矩形框 65 66 //對圖片進行截取 67 68 UIImage * image2 = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect1)]; 69 70 NSString *filePath2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.png",@"jietu2"]]; // 保存文件的名稱 71 72 NSLog(@"filePath = %@",filePath); 73 74 BOOL result2 = [UIImagePNGRepresentation(image2)writeToFile:filePath2 atomically:YES]; 75 76 NSLog(@"result2 = %d",result2); 77 78 79 80 //存入相冊 81 82 //UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 83 84 } 85 86 87 //截取圖片特定區域 88 /** 89 *從圖片中按指定的位置大小截取圖片的一部分 90 * UIImage image 原始的圖片 91 * CGRect rect 要截取的區域 92 */ 93 - (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect { 94 CGImageRef sourceImageRef = [image CGImage]; 95 CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect); 96 UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 97 return newImage; 98 }
?
轉載于:https://www.cnblogs.com/DannyApple/p/3940516.html
總結
以上是生活随笔為你收集整理的【IOS】集成zxing(二维码扫描)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet与JSP间的传值问题
- 下一篇: R中统计假设检验总结(一)