我有一个快速扫描扫描条形码的应用程序,但是现在如何存储扫描的条形码(将其数字化),类似于存储卡这样的应用程序如何工作?
1 个答案:
答案 0 :(得分:-3)
尝试:
导入AVFoundation
导入UIKit
类ScannerViewController:UIViewController,AVCaptureMetadataOutputObjectsDelegate {
var captureSession:AVCaptureSession!
var PreviewLayer:AVCaptureVideoPreviewLayer!
覆盖func viewDidLoad(){
super.viewDidLoad()
view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()
警卫让videoCaptureDevice = AVCaptureDevice.default(for:.video)else {return}
让videoInput:AVCaptureDeviceInput
做{
videoInput =试试AVCaptureDeviceInput(device:videoCaptureDevice)
} {
返回
}
如果(captureSession.canAddInput(videoInput)){
captureSession.addInput(videoInput)
}其他{
failed()
返回
}
让metadataOutput = AVCaptureMetadataOutput()
如果(captureSession.canAddOutput(metadataOutput)){
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(自己,队列:DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.ean8,.ean13,.pdf417]
}其他{
failed()
返回
}
PreviewLayer = AVCaptureVideoPreviewLayer(session:captureSession)
PreviewLayer.frame = view.layer.bounds
PreviewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
func failed(){
let ac = UIAlertController(标题:“不支持扫描”,消息:“您的设备不支持从项目中扫描代码。请使用带摄像头的设备。”,preferredStyle:.alert)
ac.addAction(UIAlertAction(title:“ OK”,style:.default))
礼物(ac,动画:true)
captureSession =无
}
覆盖func viewWillAppear(_动画:布尔){
super.viewWillAppear(动画)
如果(captureSession?.isRunning == false){
captureSession.startRunning()
}
}
覆盖func viewWillDisappear(_动画:布尔){
super.viewWillDisappear(动画)
如果(captureSession?.isRunning == true){
captureSession.stopRunning()
}
}
func metadataOutput(_输出:AVCaptureMetadataOutput,didOutput metadataObjects:[AVMetadataObject],来自连接:AVCaptureConnection){
captureSession.stopRunning()
如果让metadataObject = metadataObjects.first {
守卫让可读对象=元数据对象为? AVMetadataMachineReadableCodeObject else {return}
警惕让stringValue =可读Object.stringValue else {返回}
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
找到(代码:stringValue)
}
解雇(动画:true)
}
func found(代码:字符串){
打印(代码)
}
覆盖var PreferredsStatusBarHidden:布尔{
返回真
}
覆盖var supportInterfaceOrientations:UIInterfaceOrientationMask {
返回.portrait
}
}