質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

2303閲覧

objective-cからswiftにコードを書き換えました。以下、添削願えませんか?

kento2543

総合スコア163

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2015/08/10 16:16

編集2022/01/12 10:55

こちらのソースコードを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 での呼び出しなどはしております。

正しく動作させたいのですが、どこをどう直したらよいのかわかりませんでした・・・。

お手数ですが、宜しくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

動作未検証ですが、下記でどうでしょう。
*そのままでは、”CGImageRef”がnilなので動きません。

swift

1func decode(){ 2 let imageToDecode :CGImageRef! = nil 3 let source = ZXCGImageLuminanceSource(CGImage: imageToDecode!) 4 if let binarizer = ZXHybridBinarizer.binarizerWithSource(source) as? ZXHybridBinarizer 5 { 6 if let bitmap = ZXBinaryBitmap.binaryBitmapWithBinarizer(binarizer) as? ZXBinaryBitmap 7 { 8 var error :NSError? = nil 9 if let hints = ZXDecodeHints.hints() as? ZXDecodeHints,let reader = ZXMultiFormatReader.reader() as? ZXMultiFormatReader 10 { 11 var result = reader.decode(bitmap, hints: hints, error: &error) 12 if result != nil{ 13 var contents: NSString = result.text 14 var format: ZXBarcodeFormat = result.barcodeFormat; 15 } else { 16 17 } 18 } 19 } 20 } 21 }

投稿2015/08/28 07:09

nazaka

総合スコア44

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問