Ios 我们可以使用条形码扫描仪作为QRcode扫描仪吗?

Ios 我们可以使用条形码扫描仪作为QRcode扫描仪吗?,ios,objective-c,qr-code,Ios,Objective C,Qr Code,我正在制作一个带有QRcode的应用程序,我有一个先前使用条形码扫描仪的代码。也可以将其修改为QRcode扫描仪吗 谢谢 将CIdetector类型更改为CIdetector类型qrcode。要扫描条形码和qrcode,可以使用下面的代码段 将其添加到视图控制器中 let supportedCodeTypes = [AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code,

我正在制作一个带有QRcode的应用程序,我有一个先前使用条形码扫描仪的代码。也可以将其修改为QRcode扫描仪吗

谢谢


将CIdetector类型更改为CIdetector类型qrcode。

要扫描条形码和qrcode,可以使用下面的代码段

将其添加到视图控制器中

let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
                              AVMetadataObjectTypeCode39Code,
                              AVMetadataObjectTypeCode39Mod43Code,
                              AVMetadataObjectTypeCode93Code,
                              AVMetadataObjectTypeCode128Code,
                              AVMetadataObjectTypeEAN8Code,
                              AVMetadataObjectTypeEAN13Code,
                              AVMetadataObjectTypeAztecCode,
                              AVMetadataObjectTypePDF417Code,
                              AVMetadataObjectTypeQRCode]
    func startVideoCapture(){
        // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        do {
            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Initialize the captureSession object.
            captureSession = AVCaptureSession()

            // Set the input device on the capture session.
            captureSession?.addInput(input)

            // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)

            // Set delegate and use the default dispatch queue to execute the call back
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            captureMetadataOutput.metadataObjectTypes = supportedCodeTypes

            // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            videoPreviewLayer?.frame = self.qrCodeView.bounds
            self.qrCodeView.layer.addSublayer(videoPreviewLayer!)
            self.qrCodeView.clipsToBounds = true
            // Start video capture.
            captureSession?.startRunning()
            qrCodeFrameView = UIView()

//            if let qrCodeFrameView = qrCodeFrameView {
//                qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
//                qrCodeFrameView.layer.borderWidth = 2
//                self.qrCodeView.addSubview(qrCodeFrameView)
//                self.qrCodeView.bringSubview(toFront: qrCodeFrameView)
//            }

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }
    }
然后在viewcontroller中添加此函数

let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
                              AVMetadataObjectTypeCode39Code,
                              AVMetadataObjectTypeCode39Mod43Code,
                              AVMetadataObjectTypeCode93Code,
                              AVMetadataObjectTypeCode128Code,
                              AVMetadataObjectTypeEAN8Code,
                              AVMetadataObjectTypeEAN13Code,
                              AVMetadataObjectTypeAztecCode,
                              AVMetadataObjectTypePDF417Code,
                              AVMetadataObjectTypeQRCode]
    func startVideoCapture(){
        // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        do {
            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Initialize the captureSession object.
            captureSession = AVCaptureSession()

            // Set the input device on the capture session.
            captureSession?.addInput(input)

            // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)

            // Set delegate and use the default dispatch queue to execute the call back
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            captureMetadataOutput.metadataObjectTypes = supportedCodeTypes

            // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            videoPreviewLayer?.frame = self.qrCodeView.bounds
            self.qrCodeView.layer.addSublayer(videoPreviewLayer!)
            self.qrCodeView.clipsToBounds = true
            // Start video capture.
            captureSession?.startRunning()
            qrCodeFrameView = UIView()

//            if let qrCodeFrameView = qrCodeFrameView {
//                qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
//                qrCodeFrameView.layer.borderWidth = 2
//                self.qrCodeView.addSubview(qrCodeFrameView)
//                self.qrCodeView.bringSubview(toFront: qrCodeFrameView)
//            }

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }
    }
然后在您的
视图中将出现

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.startVideoCapture()
}

在打电话给startVideoCapture之前,别忘了检查摄像头的权限

是的,我们可以这样做。我们所要做的就是更改CIdetector类型:CIdetector*detector=[CIdetector detectorOfType:CIDetectorTypeQRCode context:nil选项:@{cidetectoraccurity:cidetectoraccurityhigh}];您应该提供更多关于您当前使用的条形码扫描仪的详细信息,有很多,包括在iOS中的几个实现,以及更多的外部库。我使用的是Apple提供的默认检测器代码。得到答案@jcaron。谢谢你的帮助,谢谢!这就是我需要的。除了这个,还有其他方法吗?比如不使用CIDetector?谢谢你的帮助。