gpt4 book ai didi

ios - 在swift ios中扫描后如何获取二维码图像?

转载 作者:行者123 更新时间:2023-11-28 07:27:25 26 4
gpt4 key购买 nike

我正在使用 Swift 在 Xcode 中开发一个二维码阅读器应用程序。我用以下代码扫描了二维码:

import UIKit
import AVFoundation

class ScannerViewController: UIViewController {

var captureSession = AVCaptureSession()

var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?

var scannerMetadataObj : AVMetadataMachineReadableCodeObject?

@IBOutlet weak var testImageView: UIImageView!


override func viewDidLoad() {
super.viewDidLoad()


let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)

guard let captureDevice = deviceDiscoverySession.devices.first else {
print("Failed to get the camera device")
return
}

do {

let input = try AVCaptureDeviceInput(device: captureDevice)

captureSession.addInput(input)

let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession.addOutput(captureMetadataOutput)

captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = supportedCodeTypes

} catch {
return
}

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)

qrCodeFrameView = UIView()

if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubviewToFront(qrCodeFrameView)
}


}


override func viewWillAppear(_ animated: Bool) {
captureSession.startRunning()

}

override func viewWillDisappear(_ animated: Bool) {
captureSession.stopRunning()
qrCodeFrameView?.frame = CGRect.zero
}

}


extension ScannerViewController: AVCaptureMetadataOutputObjectsDelegate {

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
return
}


// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

scannerMetadataObj = metadataObj

let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

self.testImageView.image = // set image with qr-code
}

}

扫描二维码后,我想获取二维码的图像并将testImageView的图像设置为获取的图像。怎么可能?我这样做是因为我想将该图像保存在数据库中以备将来使用。在这里我唯一的问题是如何从 videoPreviewLayer 获取二维码图像并将 testImageView 的图像设置为该图像。

最佳答案

您可以在需要时添加 AVCapturePhotoOutput 和 capturePhoto。

  1. 在 ScannerViewController 添加两个属性:

    private let photoOutput = AVCapturePhotoOutput()
    private var isCapturing = false
  2. 在 viewDidLoad 中向 captureSession 添加新的输出:

    captureSession.addOutput(photoOutput)
  3. 在 metadataOutput 方法中调用 capturePhoto 并记住捕获状态。因为 metadataOutput 方法可以被非常频繁地调用:

    let photoSettings = AVCapturePhotoSettings()
    if !isCapturing {
    isCapturing = true
    photoOutput.capturePhoto(with: photoSettings, delegate: self)
    }
  4. 实现 AVCapturePhotoCaptureDelegate 协议(protocol):

    extension ScannerViewController: AVCapturePhotoCaptureDelegate {
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    isCapturing = false
    guard let imageData = photo.fileDataRepresentation() else {
    print("Error while generating image from photo capture data.");
    return
    }
    guard let qrImage = UIImage(data: imageData) else {
    print("Unable to generate UIImage from image data.");
    return
    }
    testImageView.image = qrImage
    }
    }
  5. 如果需要,进行一些额外的照片设置和错误处理也是个好主意。

关于ios - 在swift ios中扫描后如何获取二维码图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56088575/

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