OObjective-c 二维码(第三方库ZXing)

//编码

//  ViewController.mspa

//  二维码.net

//code

//  Created by DC017 on 15/12/31.orm

//  Copyright © 2015 DC017. All rights reserved.图片

//ip


#import "ViewController.h"字符串

#import <ZXingObjC.h>get




@interface ViewController ()string

{

    UIImage *image1 ;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self bianma];

    [self jiema];

    

}

//编码

-(void)bianma{

    

    NSError *error = nil;

    ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer];

    ZXBitMatrix* result = [writer encode:@"http://www.baidu.com"

                                  format:kBarcodeFormatQRCode

                                   width:500

                                  height:500

                                   error:&error];

    if (result) {

        CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];

        

        image1 = [UIImage imageWithCGImage:image];

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

        imageView.image = image1;

        [self.view addSubview:imageView];

        // This CGImageRef image can be placed in a UIImage, NSImage, or written to a file.

    } else {

        NSString *errorMessage = [error localizedDescription];

        NSLog(@"%@",errorMessage);

    }

    

}

//解码

-(void)jiema{

    //二维码图片

    CGImageRef imageToDecode;  // Given a CGImage in which we are looking for barcodes

    //给定一个CGImage中,咱们正在寻找条形码

    imageToDecode=[image1 CGImage];

    ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] ;

    ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];

    NSError *error = nil;

     // There are a number of hints we can give to the reader, including

    // possible formats, allowed lengths, and the string encoding.

    //有许多的提示咱们能够给读者,包括 可能的格式,容许长度和字符串编码

    ZXDecodeHints *hints = [ZXDecodeHints hints];

    

    ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];

    ZXResult *result = [reader decode:bitmap

                                hints:hints

                                error:&error];

    if (result) {

        // The coded result as a string. The raw data can be accessed with

        // result.rawBytes and result.length.

        //编码结果为字符串。原始数据能够与被访问result.rawBytesresult.length

        NSString *contents = result.text;

        NSLog(@"二维码结果:%@",contents);

        

        // The barcode format, such as a QR code or UPC-A

        //条形码格式,如QR码或UPC-A

        ZXBarcodeFormat format = result.barcodeFormat;

        NSLog(@"该二维码格式为:%u",format);

    } else {

        // Use error to determine why we didn't get a result, such as a barcode

        // not being found, an invalid checksum, or a format inconsistency.

       // 使用错误,以肯定为何咱们没有获得结果,如条形码不被人发现,一个无效的校验和,或格式不一致。

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相关文章
相关标签/搜索