2

我正在寻找一个能够从文件(jpeg、bmp 等)中读取二维码并将该信息作为 NSString 输出的类。

我遇到的问题是,每个项目似乎都围绕着让 iPhone 用相机来做到这一点。

我希望在带有图像文件的 OSX 桌面上执行此操作。我尝试使用 ZXing 附带的 Objective-C OSX 项目,但我无法让它与我自己的项目配合得很好。

有人在这方面取得过成功吗?谢谢!

4

1 回答 1

0

So here was what I came up with and it seems to work for me.

- (NSString*) movieFrameAsString:(CGImageRef)targetMovieFrame
{
    @try 
    {        
        ZXCGImageLuminanceSource* luminancesource = [[ZXCGImageLuminanceSource alloc] initWithCGImage:targetMovieFrame];
        ZXHybridBinarizer* binarizerInput = [[ZXHybridBinarizer alloc] init];
        [binarizerInput initWithSource:luminancesource];
        ZXBinaryBitmap* binarybitmapInput = [[ZXBinaryBitmap alloc] initWithBinarizer:binarizerInput];
        ZXDecodeHints* decodehints   = [[ZXDecodeHints alloc] init];
        ZXQRCodeReader* qrcodereader = [[ZXQRCodeReader alloc] init];

        ZXResult* results = [qrcodereader decode:binarybitmapInput hints:decodehints];

        [luminancesource release];
        [binarizerInput release];
        [binarybitmapInput release];
        [decodehints release];
        [qrcodereader release];

        return [results text];
    }

    @catch (ZXReaderException* rex)
    {
        if (![rex.reason isEqualToString:@"Could not find three finder patterns"]) 
        {
            //NSLog(@"failed to decode, caught ReaderException '%@'", rex.reason);
        }
    } 

    @catch (ZXIllegalArgumentException* iex) 
    {
        //NSLog(@"failed to decode, caught IllegalArgumentException '%@'", iex.reason);
    }

    @catch (id ue) 
    {
        //NSLog(@"Caught unknown exception: %@", ue);
    }

    return 0;
}

The targetMovieFrame is brought in, it runs it through the cocktail of filters to produce a binary image. It produces results by decoding the image using hints, and using a method included in the QRCodeReader object. Then I return the results, which is an NSString. Everything else is just exceptions being caught and returned.

于 2012-08-23T02:03:12.313 回答