gpt4 book ai didi

ios - 作为 NSObject 子类的条码扫描器

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:54 27 4
gpt4 key购买 nike

我正在快速创建一个条形码扫描仪作为 NSObject 子类,但我无法触发

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!)

委托(delegate)方法。这是我的类(class)代码。

class CKScanner: NSObject, AVCaptureMetadataOutputObjectsDelegate {

//test
var delegate: AVCaptureMetadataOutputObjectsDelegate?

var frame: CGRect
//UPC Scan properties
let avSession:AVCaptureSession = AVCaptureSession()
let avDevice:AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

var avInput:AVCaptureDeviceInput?
var avOutput:AVCaptureMetadataOutput?
var avPreviewLayer: AVCaptureVideoPreviewLayer?
var scannedUPC: NSString
{
get {
return self.scannedUPC
}
}

required init(_frame: CGRect, _parentView: CALayer) {
frame = _frame

super.init()

startScanning(_parentView)
}

func startScanning(_parentLayer: CALayer) {
println("started scanning")
var error: NSError?

avInput = AVCaptureDeviceInput(device: avDevice, error: &error)
if error != nil {
println("Error: \(error!.localizedDescription)")
return
}

if avSession.canAddInput(avInput!) {
avSession.addInput(avInput!)
}

avPreviewLayer = AVCaptureVideoPreviewLayer(session: avSession)
if avPreviewLayer != nil {
avPreviewLayer!.frame = frame
avPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
_parentLayer.addSublayer(avPreviewLayer!)
}
avOutput = AVCaptureMetadataOutput()
avOutput!.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
if avSession.canAddOutput(avOutput!) {
avSession.addOutput(avOutput!)
avOutput!.metadataObjectTypes = avOutput!.availableMetadataObjectTypes
}
else {
println("cant add output")
}

avSession.startRunning()

}

//scanner was able to scan a barcode
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {}

在我的 ViewController 中,我调用

var scanner = Scanner(frame: frame, _parentLayer: layer) 

我还尝试在我的 ViewController 中而不是在 Scanner 类中设置委托(delegate),但没有成功。

是的,我确实在我的文件顶部包含了 AVCaptureMetadataOutputObjectsDelegate 以正确设置 scanner.delegate = self

最佳答案

如果您专门将 metadataObjectTypes 设置为

[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]

有什么区别吗?

关于ios - 作为 NSObject 子类的条码扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222966/

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