日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

ZBar与ZXing使用后感觉

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZBar与ZXing使用后感觉 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[原]ZBar與ZXing使用后感覺(上)

2014-3-18閱讀2011 評論1

最近對二維碼比較感興趣,還是那句老話,那么我就對比了一下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拉,至于項目中使用,可能細節更多,但是這些基礎,足夠你后面的使用。

[原]ZBar與ZXing使用后感覺(中)

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
#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的那個方法了嗎,那個和原本的文件的方法有些出入,主要被我修改了一下

外部使用,只要調用qrimageforstring那個方法就行,將你要生成的string當作入參傳入即可。

原理嗎?相信大家一看就明白,qrcode將字符串生成了一個data數據,根據這個數據,然后去繪制一個又一個的小黑塊,這樣就產生了我們看到的 二維碼。那么彩色二維碼,很酷把,知道了原理,我們才獲取到數據在繪制的時候,可以繪制各種顏色的小方塊,如何繪制,相信大家看看就知道了,

但是原色過多,可能無法識別,或者識別率很低,測試了一下,對于一種顏色,幾本沒啥問題。

很多時候,我們看到二維碼中間有個圖片,其實這里又包含了另一個知識,那就是缺省率,

typedef enum {QR_ECLEVEL_L = 0, ///< lowestQR_ECLEVEL_M,QR_ECLEVEL_Q,QR_ECLEVEL_H      ///< highest
} QRecLevel;

這個枚舉,很清楚把,最高,缺省率可以高達30%,就是你選擇了最高編碼等級,所以我們就可以在二維碼中間貼上一張 圖片也不影響使用,但是如果你選擇最低的,那么缺省只能達到5左右,但是越低,掃瞄速度越快,越高,意味著你的二維碼也越復雜,增加掃瞄難度,所以如何權 衡,看自己把。

[原]zbar與ZXing使用后感覺(下)

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: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];}
}


簡單吧,至于原理,還沒看,以后再深究!,因為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];// VibrateAudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}


好了,很簡單,所以以后如果有空繼續補充吧,主要了解這些,以備以后用起來方便!

?

原文:http://m.blog.csdn.net/blog/shidongdong2012/21476909

轉載于:https://www.cnblogs.com/wellsoho/p/4431602.html

總結

以上是生活随笔為你收集整理的ZBar与ZXing使用后感觉的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。