iOS QRcode识别及相册图片二维码读取识别
對(duì)二維碼的處理及系統(tǒng)原生API掃描、 Zbar掃描和Zing掃描 比較
iOS原生API
系統(tǒng)掃描的效率是最高,反正包括各種你見過(guò)的沒見過(guò)的碼,但是有一點(diǎn)我不是很清楚 iOS7 掃描二維碼可以,但從相冊(cè)照片讀取二維碼蘋果不支持,必須是iOS8+。
https://github.com/yannickl/QRCodeReaderViewController
https://github.com/zhengjinghua/MQRCodeReaderViewController
這兩個(gè)例子已經(jīng)寫的非常清楚了
// 對(duì)于識(shí)別率的精度 就是屏幕有波浪一樣[self.captureSession setSessionPreset:AVCaptureSessionPresetHigh];// 改成了 降低采集頻率[self.captureSession setSessionPreset:AVCaptureSessionPreset640x480];不知道 有什么好的方法
ZXing 和 ZBar
具體代碼 我就不在這里寫了,Demo里 都寫了,而且百度谷歌千篇一律,說(shuō)說(shuō)ZXing的一些用到的特點(diǎn):
- 對(duì)那種小圓點(diǎn)的二維碼 ZXing和原生API 都是支持的,而ZBar 貌似不是支持的,反正我沒搞定。
- ZXing 現(xiàn)在還在繼續(xù)維護(hù) ZBar 而已經(jīng)停止了
- ZXing 可以識(shí)別圖片中多個(gè)二維碼
ZBar中的識(shí)別率 本人覺得還是比較低
//ZBar 中對(duì) 圖片中二維碼 識(shí)別UIImage * aImage = ZBarReaderController *read = [ZBarReaderController new];CGImageRef cgImageRef = aImage.CGImage;ZBarSymbol* symbol = nil;for(symbol in [read scanImage:cgImageRef]){qrResult = symbol.data ;NSLog(@"qrResult = symbol.data %@",qrResult);}ZXing 對(duì)圓點(diǎn)二維碼的支持 及能夠識(shí)別圖片中多個(gè)二維碼
// ZXing 只識(shí)別單個(gè)二維碼UIImage *image = ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:image.CGImage]; ZXHybridBinarizer *binarizer = [[ZXHybridBinarizer alloc] initWithSource: source];ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc] initWithBinarizer:binarizer];NSError *error;id<ZXReader> reader; if (NSClassFromString(@"ZXMultiFormatReader")) {reader = [NSClassFromString(@"ZXMultiFormatReader") performSelector:@selector(reader)];}ZXDecodeHints *_hints = [ZXDecodeHints hints];ZXResult *result = [reader decode:bitmap hints:_hints error:&error];if (result == nil) {NSLog(@"無(wú)QRCode");return;}NSLog(@"QRCode = %d,%@",result.barcodeFormat,result.text);ZXing 識(shí)別圖片中多個(gè)二維碼
UIImage *image = #需要識(shí)別的圖片# ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:image.CGImage]; ZXHybridBinarizer *binarizer = [[ZXHybridBinarizer alloc] initWithSource: source];ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc] initWithBinarizer:binarizer];ZXDecodeHints *hints = [ZXDecodeHints hints];NSError *error = nil;ZXQRCodeMultiReader * reader2 = [[ZXQRCodeMultiReader alloc]init];NSArray *rs = [reader2 decodeMultiple:bitmap error:&error];// 或者 NSArray *rs =[reader2 decodeMultiple:bitmap hints:hints error:&error];NSLog(@" err = %@",error);for (ZXResult *resul in rs) {NSLog(@" ---%@",resul.text);}后面 就看你需求了
iOS WebView中 長(zhǎng)按二維碼的識(shí)別
思路:
本人目前用的是 第二種方式。求大神教我第三種...
我在TOWebViewController?做了類擴(kuò)展
基本就可以了。
- 終于找到取 webView 緩存的圖片的方法了。 用的是 NSURLProtocol 方式,在github上找了一個(gè)?RNCachingURLProtocol,基本原理是:webView 在處理請(qǐng)求的過(guò)程中會(huì)調(diào)用
URLProtocol 把webView請(qǐng)求返回來(lái)的 data 用壓縮的方式的存儲(chǔ)在 cache的文件夾下, 發(fā)出請(qǐng)求的時(shí)候會(huì)先去讀取緩存。本人對(duì) RNCachingURLProtocol 做過(guò)修改 具體請(qǐng)看Demo
- 轉(zhuǎn)載請(qǐng)注明 作者:好迪?文章地址
- 水平有限 歡迎批評(píng)指正評(píng)論
Demo地址
https://github.com/cuiwe000/QRCodeDemo.git
總結(jié)
以上是生活随笔為你收集整理的iOS QRcode识别及相册图片二维码读取识别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FOB价格计算
- 下一篇: IOS开发之证书共享(团队协同开发)