こちらのソースコードをswiftに変換して使いたいです。
https://github.com/TheLevelUp/ZXingObjC
自分なりに変換してみたのですが、エラーが続出してしまったため
質問に至りました。
以下、objective-cのコードです。
CGImageRef imageToDecode; // Given a CGImage in which we are looking for barcodes ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease]; 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. NSString *contents = result.text; // The barcode format, such as a QR code or UPC-A ZXBarcodeFormat format = result.barcodeFormat; } 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. }
これからが私がswiftへ変換したソースです。
var imageToDecode: CGImageRef // Given a CGImage in which we are looking for barcodes var source: ZXLuminanceSource = ((ZXCGImageLuminanceSource.alloc()).initWithCGImage(imageToDecode)).autorelease // var source: ZXLuminanceSource = ZXCGImageLuminanceSource.alloc().initWithCGImage(imageToDecode) autorelease var bitmap: ZXBinaryBitmap = ZXBinaryBitmap.binaryBitmapWithBinarizer(ZXHybridBinarizer.binarizerWithSource(source)) var error: NSError? // There are a number of hints we can give to the reader, including // possible formats, allowed lengths, and the string encoding. var hints: ZXDecodeHints = ZXDecodeHints.hints() var reader: ZXMultiFormatReader = ZXMultiFormatReader.reader() var result: ZXResult = 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. var contents: NSString = result.text // The barcode format, such as a QR code or UPC-A var format: ZXBarcodeFormat = result.barcodeFormat; } 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. }
※ podfileへの追記、及び、import での呼び出しなどはしております。
正しく動作させたいのですが、どこをどう直したらよいのかわかりませんでした・・・。
お手数ですが、宜しくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。