//編碼
// 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 ;
}
@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.rawBytes和result.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