gpt4 book ai didi

ios - 加载二维码到另一个 View Controller (Xcode)

转载 作者:行者123 更新时间:2023-11-28 08:05:33 25 4
gpt4 key购买 nike

我目前正在做一个测验应用程序,用户必须扫描二维码才能做第一个问题,然后扫描另一个二维码到第二个问题。截至目前,我的二维码扫描仪只能将二维码扫描到网址。我已经为我的问题完成了布局和按钮,我只需要 qr 代码链接到我的 qn Controller 。提前谢谢你。

这是我的 QRScannerController.swift

import UIKit
import AVFoundation

class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

@IBOutlet var messageLabel:UILabel!
@IBOutlet var topbar: UIView!

var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?

let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeAztecCode,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode]

override func viewDidLoad() {
super.viewDidLoad()

// 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 = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)

// Start video capture.
captureSession?.startRunning()

// Move the message label and top bar to the front
view.bringSubview(toFront: messageLabel)
view.bringSubview(toFront: topbar)

// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()

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

} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


// MARK: - AVCaptureMetadataOutputObjectsDelegate Methods

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}

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

if supportedCodeTypes.contains(metadataObj.type) {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

if metadataObj.stringValue != nil {
let url = URL(string: metadataObj.stringValue)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}

}

这是我的问题 Controller

import UIKit    
class Quiz1Controller: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

我建议您创建一个帮助程序文件,以将 QRCode 扫描与 UIViewController 分开。这会清理您的代码并实现可重用性。

回答你的问题。您可以更改方法 func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { 来处理扫描的 QRCode 的输出。

我实际上实现了一个委托(delegate)来从 QRCodeHelper 返回值,但您也可以在没有文件的情况下调用函数。

为此,创建一个新函数 func qrScanDidScanCode(_ code:String) 并像这样在 captureOutput 中调用它。

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
var QRCode: String?
for metadata in metadataObjects as! [AVMetadataObject] {
if metadata.type == AVMetadataObjectTypeQRCode {
QRCode = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
}
}

if let qr = QRCode {
qrScanDidScanCode(qr)
}
}

qr 变量将 QRCodes 值保存为字符串,您现在可以在任何您喜欢的地方使用它。

如果你想在你的下一个 viewController 中使用 QRCodes 的字符串值。使用 segues 方法执行和准备。

在当前的 UIViewController 中创建一个新属性:var selectedQRCode:String?qrScanDidScanCode() 中设置它 在设置 selectedQRCode 后调用 performSegue。

在 prepareSegue 中,做这样的事情:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? SecondViewController {
destination.qrCode = selectedQRCode
}
}

关于ios - 加载二维码到另一个 View Controller (Xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45277648/

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