1. 程式人生 > >iOS 從相簿中匯入二維碼圖片識別

iOS 從相簿中匯入二維碼圖片識別

使用的第三方庫ZXingObjC(https://github.com/TheLevelUp/ZXingObjC)進行識別,主要程式碼如下:

CGImageRef imageToDecode = image.CGImage// Given a CGImage in which we are looking for barcodes

ZXLuminanceSource *source = [[ZXCGImageLuminanceSourcealloc] initWithCGImage:imageToDecode];

ZXBinaryBitmap *bitmap = [ZXBinaryBitmapbinaryBitmapWithBinarizer

:[ZXHybridBinarizerbinarizerWithSource: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 = [ZXDecodeHintshints];

ZXMultiFormatReader *reader = [ZXMultiFormatReaderreader

];

       ZXResult *result = [reader decode:bitmap

                                   hints:hints

                                   error:&error];

       if (result) {

           NSString *contents = result.text;

           NSLog(@"%@",contents);

        }else {

        }

由於拍照的圖片大於280,二維碼的標準圖片是280*280,所以要講圖片進行縮小操作,

if

((image.size.width >280)&&(image.size.height >280))

{

    image = ;//使用圖片縮小函式,網上有很多程式碼都可以的

}

然後就可以識別了。從相簿裡匯入圖片,我就省了,自己從網上找。