ZBar與ZXing使用後感覺
最近對二維碼比較感興趣,還是那句老話,那麼我就對比了一下zxing和zbar
如果對於這兩個的背景不瞭解的話,可以看我以前的文章,介紹了幾個比較基礎的知識。
首先,現在有個很好用的cocoapods第三方庫管理工具,至於如何安裝,那麼以前分享過一片如何安裝cocoapods的介紹。
如果這兩點你都滿足的話,可以繼續這個對比拉,其實為什麼不直接從github下載一步步配置編譯呢?至少我覺得cocoapods這個工具很方便。而且免去了一些配置編譯的缺點。
如果你要深究加入什麼庫啊,setting裡面要配置什麼啊,可以去網上搜索一下,也很多。
廢話不多說,在podfile裡面加入這3個命令:
pod 'ZBarSDK', '~> 1.3.1'
pod 'ZXingObjC', '~> 2.2.4'
pod 'libqrencode', '~> 3.4.2'
這是我目前對於二維碼掃瞄,所使用到的一些庫,zbar是用的zbar開源庫,支援我們常見的條形碼以及二維碼掃瞄,使用簡單,方便,但是不能生成二維碼,所以我們要藉助libqrencode,這個庫很好用,但是一般剛接觸可能不是很清楚如何使用。
zbar:
一般裡面有個
ZBarReaderViewController * ctrl = [[ZBarReaderViewController alloc] init];
ctrl.readerDelegate = self;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
for (ZBarSymbol * symbol in set){
break;
}
ZBarReaderView
看看怎麼使用把,其實他就是一個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,才能正確呼叫開始掃瞄,至於torchmode是關於閃光燈的,預設2是自動,0是關閉把。這樣只要掃瞄到,就是呼叫代理
didReadSymbols: (ZBarSymbolSet*) symbols
fromImage: (UIImage*) image
你可以在這裡處理出結果,有個特殊,就是掃瞄中文的二維碼亂碼問題,解決很簡單,由於zbar是日本人搞的,所以他把中文預設為日文,你用utf8是無法解碼的,附上程式碼
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拉,至於專案中使用,可能細節更多,但是這些基礎,足夠你後面的使用。
2014-3-18閱讀1272 評論6
上一篇文章中,介紹了一些zbar的幾本使用,由於zbar本書無法生成二維碼,所以我們必須藉助另一個庫,libqrencode,這個庫可以幫 助你生成二維碼,但是這個庫都是一些。c檔案,真正的使用需要額外的兩個檔案,其實如果例項非凡,不需要這兩個檔案也可以,貼上源 碼:QRCodeGenerator
#import "QRCodeGenerator.h"
#import <qrencode.h>
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
#define kCGImageAlphaPremultipliedLast (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
#else
#define kCGImageAlphaPremultipliedLast kCGImageAlphaPremultipliedLast
#endif
enum {
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 context
CGColorSpaceRef 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 image
CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];
// some releases
CGContextRelease(ctx);
CGImageRelease(qrCGImage);
CGColorSpaceRelease(colorSpace);
QRcode_free(code);
return qrImage;
}
注意到drawcode的那個方法了嗎,那個和原本的檔案的方法有些出入,主要被我修改了一下
外部使用,只要呼叫qrimageforstring那個方法就行,將你要生成的string當作入參傳入即可。
原理嗎?相信大家一看就明白,qrcode將字串生成了一個data資料,根據這個資料,然後去繪製一個又一個的小黑塊,這樣就產生了我們看到的 二維碼。那麼彩色二維碼,很酷把,知道了原理,我們才獲取到資料在繪製的時候,可以繪製各種顏色的小方塊,如何繪製,相信大家看看就知道了,
但是原色過多,可能無法識別,或者識別率很低,測試了一下,對於一種顏色,幾本沒啥問題。
很多時候,我們看到二維碼中間有個圖片,其實這裡又包含了另一個知識,那就是預設率,
typedef enum {
QR_ECLEVEL_L = 0, ///< lowest
QR_ECLEVEL_M,
QR_ECLEVEL_Q,
QR_ECLEVEL_H ///< highest
} QRecLevel;
這個列舉,很清楚把,最高,預設率可以高達30%,就是你選擇了最高編碼等級,所以我們就可以在二維碼中間貼上一張 圖片也不影響使用,但是如果你選擇最低的,那麼預設只能達到5左右,但是越低,掃瞄速度越快,越高,意味著你的二維碼也越複雜,增加掃瞄難度,所以如何權 衡,看自己把。
2014-3-18閱讀1397 評論2
其實,感覺介紹的有點簡單,主要是作為自己的積累的一部分,所以有些屬性,自己去試了試,但是並沒有在文章中體現,所以最終啥時候用到,某一方面, 再去深究把,我只能把一些基礎的介紹出來,前面介紹了zbar,這裡就著重介紹一下zxing,其實說實話,zxing更方便,但是不支援條形碼,據說可 以修改實現,但是沒去研究,又興趣的可以研究下,zxing本身很龐大,支援各個平臺,pod search zxing
以外發現一個其他的開源庫
-> 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,看到這個名字再熟悉不過了,去了github上看了一下,維護情況也行,所以決定使用這個,還是兩方面介紹,這個庫支援掃瞄和生成哦!:
生成:
- (void)crateQRcode
{
NSError* error = nil;
ZXMultiFormatWriter* writer = [ZXMultiFormatWriter writer];
ZXBitMatrix* result = [writer encode:@"A string to encode"
format:kBarcodeFormatQRCode
width:500
height:500
error:&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];
}
}
簡單吧,至於原理,還沒看,以後再深究!,因為libqrcode看了,這個估計也是差不多的。
掃瞄就更簡單了:
本來想自己寫一下,但是發現,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];
結果回撥:
- (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];
// Vibrate
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
好了,很簡單,所以以後如果有空繼續補充吧,主要了解這些,以備以後用起來方便!
原文:http://m.blog.csdn.net/blog/shidongdong2012/21476909