2
我是一個Ios新手。我在學習迅速和忽視的對象c。ZXingObjc - 將對象c轉換爲swift
目前,我正在用swift和xcode 6.1編寫演示程序,它可以從相機或圖像庫中掃描qrcode和條形碼。
之前,我嘗試使用zbar SDK來做到這一點,但它發生錯誤,我不知道如何解決它,我發佈這個錯誤在後:Scan qrcode and barcode from camera and image which picked from image library in swift,但沒有人回答。
我正在嘗試使用ZXingObjC從圖像和相機掃描qrcode和條形碼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;
}
}
}
我會很感激,如果有人讓我知道它爲什麼會發生錯誤,以及如何解決它(請提供詳細的說明,因爲我剛剛得知迅速和3 IOS幾周沒有學習對象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;
}
兒子,你可以分享工作項目,我無法解決ZXLuminanceSource的依賴關係。 – harsha 2015-09-12 13:01:19
我們可以使用AVFoundation框架實現從庫中掃描QR嗎? – 2017-08-14 13:48:25