• 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
    #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左右,但是越低,扫瞄速度越快,越高,意味着你的二维码也越复杂,增加扫瞄难度,所以如何权 衡,看自己把。

    [原]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:kBarcodeFormatQRCode
                                       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!
    
    Format: %@
    
    Contents:
    %@", formatString, result.text];
      [self.decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES];
    
      // Vibrate
      AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
    


    好了,很简单,所以以后如果有空继续补充吧,主要了解这些,以备以后用起来方便!

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

  • 相关阅读:
    Linux 之 文件压缩解压
    Linux 之 文件搜索命令
    Linux 之 文件内容查看
    Linux 之 Vim常用命令
    Linux 之 CentOS练习
    CentOS找不到想要的镜像版本?
    Swoole 简单学习(2)
    Swoole 简单学习
    svn的简单知识
    8、16、32-BIT系列单片机区别与特点
  • 原文地址:https://www.cnblogs.com/wellsoho/p/4431602.html
Copyright © 2020-2023  润新知