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

//

//  ViewController.m

//  二维码

//

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

//  Copyright © 2015 DC017. All rights reserved.

//


#import "ViewController.h"

#import <ZXingObjC.h>




@interface ViewController ()

{

    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


转载于:https://my.oschina.net/u/2483781/blog/597849

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值