ZXingObjc - 将对象 c 转换为 swift

ZXingObjc - Converting object c to swift

我是 Ios 的新手。我正在学习 swift 并忽略了对象 c.

目前,我正在用 swift 和 xcode 6.1 编写一个演示,它可以扫描来自相机的二维码和条形码或来自图像库的图像。

之前,我尝试使用 zbar SDK 来执行此操作,但它发生了错误,我不知道如何修复它,我 post 在 post 中编辑了此错误:Scan qrcode and barcode from camera and image which picked from image library in swift ,但没有人回答。

我正在尝试使用 ZXingObjC 从图像和相机扫描二维码和条形码 https://github.com/TheLevelUp/ZXingObjC,我阅读了它的用法并尝试转换为 swift。但是它发生了错误,我不知道如何解决它。

这是我的代码:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var lblResult: UILabel!
    @IBOutlet weak var imgView: UIImageView!
    var imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func scanCode(sender: AnyObject) {
        imagePicker.sourceType = .PhotoLibrary
        imagePicker.allowsEditing = false
        presentViewController(imagePicker, animated: true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
        var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
        imgView.contentMode = .ScaleAspectFit
        imgView.image  = tempImage
        dismissViewControllerAnimated(true, completion: nil)

        //====> object c code <=====
        /*
        ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];
        ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];

        NSError *error = nil;
        ZXDecodeHints *hints = [ZXDecodeHints hints];
        ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
        ZXResult *result = [reader decode:bitmap
            hints:hints
            error:&error];
        if (result) {
        }
        */

        //====> Convert to swift and happen error <=====
        let source: ZXLuminanceSource = ZXCGImageLuminanceSource(initWithCGImage: tempImage)
        let binazer: ZXHybridBinarizer = ZXHybridBinarizer(source: source)
        let bitmap: ZXBinaryBitmap = ZXBinaryBitmap(binarizer: binazer)

        var error: NSError?

        var hints: ZXDecodeHints = ZXDecodeHints()
        var reader: ZXMultiFormatReader = ZXMultiFormatReader()
        var result: ZXResult = reader(bitmap, hints:hints, error: error)
        if (result) {
            lblResult.text = result.text;
        }
    }

}

如果有人告诉我为什么会出错以及如何解决它,我将不胜感激(请给出详细说明,因为我刚刚学习了 swift 和 ios 3 周而没有学习对象 c)。谢谢。

已编辑: 此代码对我有用。

let source: ZXLuminanceSource = ZXCGImageLuminanceSource(CGImage: tempImage.CGImage)
let binazer = ZXHybridBinarizer(source: source)
let bitmap = ZXBinaryBitmap(binarizer: binazer)
var error: NSError?
let hints: ZXDecodeHints = ZXDecodeHints.hints() as ZXDecodeHints
var reader = ZXMultiFormatReader()

if let result = reader.decode(bitmap, hints: hints, error: &error) {
  lblResult.text = result.text;
}

你就快到了——这应该能让你完成剩下的路。注意评论:

// initializers are imported without "initWith"
let source: ZXLuminanceSource = ZXCGImageLuminanceSource(CGImage: tempImage)
let binazer = ZXHybridBinarizer(source: source)
let bitmap = ZXBinaryBitmap(binarizer: binazer)

var error: NSError?

var hints = ZXDecodeHints()
var reader = ZXMultiFormatReader()
// 1) you missed the name of the method, "decode", and
// 2) use optional binding to make sure you get a value
if let result = reader.decode(bitmap, hints:hints, error: error) {
    lblResult.text = result.text;
}