gpt4 book ai didi

iphone - ZXingObjc - 将对象 c 转换为 swift

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:14 30 4
gpt4 key购买 nike

我是 Ios 新手。我正在学习快速和被忽视的对象 c。

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

之前,我尝试使用 zbar SDK 来执行此操作,但它发生了错误,我不知道如何修复它,我在帖子中发布了此错误: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 周而没有学习 object 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;
}

关于iphone - ZXingObjc - 将对象 c 转换为 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28402422/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com