ZBar与ZXing使用后感觉
[原]ZBar與ZXing使用后感覺(jué)(上)
2014-3-18閱讀2011 評(píng)論1
最近對(duì)二維碼比較感興趣,還是那句老話(huà),那么我就對(duì)比了一下zxing和zbar
如果對(duì)于這兩個(gè)的背景不了解的話(huà),可以看我以前的文章,介紹了幾個(gè)比較基礎(chǔ)的知識(shí)。
首先,現(xiàn)在有個(gè)很好用的cocoapods第三方庫(kù)管理工具,至于如何安裝,那么以前分享過(guò)一片如何安裝cocoapods的介紹。
如果這兩點(diǎn)你都滿(mǎn)足的話(huà),可以繼續(xù)這個(gè)對(duì)比拉,其實(shí)為什么不直接從github下載一步步配置編譯呢?至少我覺(jué)得cocoapods這個(gè)工具很方便。而且免去了一些配置編譯的缺點(diǎn)。
如果你要深究加入什么庫(kù)啊,setting里面要配置什么啊,可以去網(wǎng)上搜索一下,也很多。
廢話(huà)不多說(shuō),在podfile里面加入這3個(gè)命令:
pod 'ZBarSDK', '~> 1.3.1'
pod 'ZXingObjC', '~> 2.2.4'
pod 'libqrencode', '~> 3.4.2'
這是我目前對(duì)于二維碼掃瞄,所使用到的一些庫(kù),zbar是用的zbar開(kāi)源庫(kù),支持我們常見(jiàn)的條形碼以及二維碼掃瞄,使用簡(jiǎn)單,方便,但是不能生成二維碼,所以我們要借助libqrencode,這個(gè)庫(kù)很好用,但是一般剛接觸可能不是很清楚如何使用。
zbar:
一般里面有個(gè)
ZBarReaderViewController * ctrl = [[ZBarReaderViewController alloc] init];
ctrl.readerDelegate = self;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
?? ?
?
?? ?
? ? for (ZBarSymbol * symbol in set){
?
? ? ? ? break;
? ? }
?? ?
?
ZBarReaderView
看看怎么使用把,其實(shí)他就是一個(gè)view,比較方便,也比較好用
ZBarReaderView * view = [[ZBarReaderView alloc] init];
?? ?
? ? view.frame = CGRectMake(50, 100, 220, 220);
?? ?
? ? view.readerDelegate = self;
?? ?
? ? view.torchMode = 0;
?? ?
? ? view.showsFPS = YES;
?? ?
? ? [self.view addSubview:view];
?? ?
? ? [view release];
?? ?
? ? [view start];
注意哦start,才能正確調(diào)用開(kāi)始掃瞄,至于torchmode是關(guān)于閃光燈的,默認(rèn)2是自動(dòng),0是關(guān)閉把。這樣只要掃瞄到,就是調(diào)用代理
?? ? didReadSymbols: (ZBarSymbolSet*) symbols
? ? ? ? ? fromImage: (UIImage*) image
你可以在這里處理出結(jié)果,有個(gè)特殊,就是掃瞄中文的二維碼亂碼問(wèn)題,解決很簡(jiǎn)單,由于zbar是日本人搞的,所以他把中文默認(rèn)為日文,你用utf8是無(wú)法解碼的,附上代碼
??for (ZBarSymbol * symbol in symbols){
?
?? ? ? ?
? ? ? ? if (symbol.type == ZBAR_QRCODE) {
?? ? ? ? ? ?
? ? ? ? ? ? if ([symbol.data canBeConvertedToEncoding:NSShiftJISStringEncoding]) {
? ? ? ? ? ? ? ? NSString? * str = [NSString stringWithCString:[symbol.data cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
?
? ? ? ? ? ? }
?? ? ? ? ? ?
//? ? ? ? ? ? NSString * str = [NSString stringWithCString:[symbol.data UTF8String] encoding:NSUTF8StringEncoding];
?? ? ? ? ? ?
? ? ? ? }
?? ? ? ?
? ? ? ? break;
? ? }
要用日文的格式解碼,這樣就ok拉,至于項(xiàng)目中使用,可能細(xì)節(jié)更多,但是這些基礎(chǔ),足夠你后面的使用。
[原]ZBar與ZXing使用后感覺(jué)(中)
2014-3-18閱讀1272 評(píng)論6
上一篇文章中,介紹了一些zbar的幾本使用,由于zbar本書(shū)無(wú)法生成二維碼,所以我們必須借助另一個(gè)庫(kù),libqrencode,這個(gè)庫(kù)可以幫 助你生成二維碼,但是這個(gè)庫(kù)都是一些。c文件,真正的使用需要額外的兩個(gè)文件,其實(shí)如果實(shí)例非凡,不需要這兩個(gè)文件也可以,貼上源 碼:QRCodeGenerator
#import "QRCodeGenerator.h"
#import <qrencode.h>#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
#define kCGImageAlphaPremultipliedLast (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
#else
#define kCGImageAlphaPremultipliedLast kCGImageAlphaPremultipliedLast
#endifenum {qr_margin = 3
};@implementation QRCodeGenerator+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {unsigned char *data = 0;int width;data = code->data;width = code->width;float zoom = (double)size / (code->width + 2.0 * qr_margin);CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);// draw
// CGContextSetFillColor(ctx, CGColorGetComponents([UIColor greenColor].CGColor));int ran;for(int i = 0; i < width; ++i) {for(int j = 0; j < width; ++j) {if(*data & 1) {ran = arc4random() % 3;CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:ran/255.f green:255/255.f blue:255/255.f alpha:1.0].CGColor);rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);// CGContextDrawImage(ctx, rectDraw, [UIImage imageNamed:@"7745002.jpg"].CGImage);CGContextAddRect(ctx, rectDraw);
// CGContextAddEllipseInRect(ctx, rectDraw);CGContextFillPath(ctx);}++data;}}}+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size {if (![string length]) {return nil;}QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);if (!code) {return nil;}// create contextCGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));// draw QR on this context [QRCodeGenerator drawQRCode:code context:ctx size:size];// get imageCGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];// some releasesCGContextRelease(ctx);CGImageRelease(qrCGImage);CGColorSpaceRelease(colorSpace);QRcode_free(code);return qrImage;
}
注意到drawcode的那個(gè)方法了嗎,那個(gè)和原本的文件的方法有些出入,主要被我修改了一下
外部使用,只要調(diào)用qrimageforstring那個(gè)方法就行,將你要生成的string當(dāng)作入?yún)魅爰纯伞?/p>
原理嗎?相信大家一看就明白,qrcode將字符串生成了一個(gè)data數(shù)據(jù),根據(jù)這個(gè)數(shù)據(jù),然后去繪制一個(gè)又一個(gè)的小黑塊,這樣就產(chǎn)生了我們看到的 二維碼。那么彩色二維碼,很酷把,知道了原理,我們才獲取到數(shù)據(jù)在繪制的時(shí)候,可以繪制各種顏色的小方塊,如何繪制,相信大家看看就知道了,
但是原色過(guò)多,可能無(wú)法識(shí)別,或者識(shí)別率很低,測(cè)試了一下,對(duì)于一種顏色,幾本沒(méi)啥問(wèn)題。
很多時(shí)候,我們看到二維碼中間有個(gè)圖片,其實(shí)這里又包含了另一個(gè)知識(shí),那就是缺省率,
typedef enum {QR_ECLEVEL_L = 0, ///< lowestQR_ECLEVEL_M,QR_ECLEVEL_Q,QR_ECLEVEL_H ///< highest
} QRecLevel; 這個(gè)枚舉,很清楚把,最高,缺省率可以高達(dá)30%,就是你選擇了最高編碼等級(jí),所以我們就可以在二維碼中間貼上一張 圖片也不影響使用,但是如果你選擇最低的,那么缺省只能達(dá)到5左右,但是越低,掃瞄速度越快,越高,意味著你的二維碼也越復(fù)雜,增加掃瞄難度,所以如何權(quán) 衡,看自己把。
[原]zbar與ZXing使用后感覺(jué)(下)
2014-3-18閱讀1397 評(píng)論2
其實(shí),感覺(jué)介紹的有點(diǎn)簡(jiǎn)單,主要是作為自己的積累的一部分,所以有些屬性,自己去試了試,但是并沒(méi)有在文章中體現(xiàn),所以最終啥時(shí)候用到,某一方面, 再去深究把,我只能把一些基礎(chǔ)的介紹出來(lái),前面介紹了zbar,這里就著重介紹一下zxing,其實(shí)說(shuō)實(shí)話(huà),zxing更方便,但是不支持條形碼,據(jù)說(shuō)可 以修改實(shí)現(xiàn),但是沒(méi)去研究,又興趣的可以研究下,zxing本身很龐大,支持各個(gè)平臺(tái),pod search zxing
以外發(fā)現(xiàn)一個(gè)其他的開(kāi)源庫(kù)
-> ZXing (2.2)Multi-format 1D/2D barcode image processing library.pod 'ZXing', '~> 2.2'- Homepage: http://code.google.com/p/zxing/- Source: http://zxing.googlecode.com/svn/- Versions: 2.2, 2.1, 2.0 [master repo]- Sub specs:- ZXing/ios (2.2)-> ZXingObjC (2.2.5)An Objective-C Port of ZXing.pod 'ZXingObjC', '~> 2.2.5'- Homepage: https://github.com/TheLevelUp/ZXingObjC- Source: https://github.com/TheLevelUp/ZXingObjC.git- Versions: 2.2.5, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0, 2.1.0, 2.0.2, 2.0.1,1.7, 0.0.1 [master repo]
dhmatoiMac:~ dh$
zxingobjc,看到這個(gè)名字再熟悉不過(guò)了,去了github上看了一下,維護(hù)情況也行,所以決定使用這個(gè),還是兩方面介紹,這個(gè)庫(kù)支持掃瞄和生成哦!:
生成:
- (void)crateQRcode
{NSError* error = nil;ZXMultiFormatWriter* writer = [ZXMultiFormatWriter writer];ZXBitMatrix* result = [writer encode:@"A string to encode"format:kBarcodeFormatQRCodewidth:500height:500error:&error];if (result) {CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];imageView.image =[UIImage imageWithCGImage:image];// This CGImageRef image can be placed in a UIImage, NSImage, or written to a file.} else {NSString* errorMessage = [error localizedDescription];}
}
簡(jiǎn)單吧,至于原理,還沒(méi)看,以后再深究!,因?yàn)閘ibqrcode看了,這個(gè)估計(jì)也是差不多的。
掃瞄就更簡(jiǎn)單了:
本來(lái)想自己寫(xiě)一下,但是發(fā)現(xiàn),github上的介紹也可以:
所以這里直接用demo了,別怪我偷懶啊~哈哈
初始化:
self.capture = [[ZXCapture alloc] init];self.capture.camera = self.capture.back;self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;self.capture.rotation = 90.0f;self.capture.layer.frame = self.view.bounds;[self.view.layer addSublayer:self.capture.layer];[self.view bringSubviewToFront:self.scanRectView];[self.view bringSubviewToFront:self.decodedLabel];
結(jié)果回調(diào):
- (NSString *)barcodeFormatToString:(ZXBarcodeFormat)format {switch (format) {case kBarcodeFormatAztec:return @"Aztec";case kBarcodeFormatCodabar:return @"CODABAR";case kBarcodeFormatCode39:return @"Code 39";case kBarcodeFormatCode93:return @"Code 93";case kBarcodeFormatCode128:return @"Code 128";case kBarcodeFormatDataMatrix:return @"Data Matrix";case kBarcodeFormatEan8:return @"EAN-8";case kBarcodeFormatEan13:return @"EAN-13";case kBarcodeFormatITF:return @"ITF";case kBarcodeFormatPDF417:return @"PDF417";case kBarcodeFormatQRCode:return @"QR Code";case kBarcodeFormatRSS14:return @"RSS 14";case kBarcodeFormatRSSExpanded:return @"RSS Expanded";case kBarcodeFormatUPCA:return @"UPCA";case kBarcodeFormatUPCE:return @"UPCE";case kBarcodeFormatUPCEANExtension:return @"UPC/EAN extension";default:return @"Unknown";}
}#pragma mark - ZXCaptureDelegate Methods- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result {if (!result) return;// We got a result. Display information about the result onscreen.NSString *formatString = [self barcodeFormatToString:result.barcodeFormat];NSString *display = [NSString stringWithFormat:@"Scanned!\n\nFormat: %@\n\nContents:\n%@", formatString, result.text];[self.decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES];// VibrateAudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
好了,很簡(jiǎn)單,所以以后如果有空繼續(xù)補(bǔ)充吧,主要了解這些,以備以后用起來(lái)方便!
?
原文:http://m.blog.csdn.net/blog/shidongdong2012/21476909
轉(zhuǎn)載于:https://www.cnblogs.com/wellsoho/p/4431602.html
總結(jié)
以上是生活随笔為你收集整理的ZBar与ZXing使用后感觉的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 姓张的取什么名字好听
- 下一篇: TI IPNC Web网页之网页修改教程