I am creating a barcode scanner in swift as a NSObject
subclass, but I am having trouble firing the
我在swift中创建一个条形码扫描器作为NSObject子类,但是我在解决这个问题时遇到了麻烦
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!)
delegate method. Here is my Class code.
委托方法。这是我的班级代码。
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!) {}
In my ViewController, I call
在我的ViewController中,我打电话
var scanner = Scanner(frame: frame, _parentLayer: layer)
I also tried to set the delegate in my ViewController instead of in the Scanner class, but with no luck.
我也尝试在我的ViewController中设置委托而不是在Scanner类中,但没有运气。
And yes, I do include AVCaptureMetadataOutputObjectsDelegate
at the top of my file to set the scanner.delegate = self
correctly.
是的,我确实在我的文件顶部包含AVCaptureMetadataOutputObjectsDelegate来正确设置scanner.delegate = self。
1 个解决方案
#1
0
What if you set metadataObjectTypes specifically to
如果将metadataObjectTypes专门设置为,该怎么办?
[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
Any difference?
#1
0
What if you set metadataObjectTypes specifically to
如果将metadataObjectTypes专门设置为,该怎么办?
[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
Any difference?