【发布时间】:2018-01-22 08:11:43
【问题描述】:
我想扫描从照片库中提取的二维码。这个link 有类似的东西,但没有多大帮助。 我已经成功使用相机实现了扫描 QR 功能。下面是代码:
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
// lblMesage.text = QRCaptureFailedMessage
return
}
// Get the metadata object.
metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
// Here we use filter method to check if the type of metadataObj is supported
// Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type
// can be found in the array of supported bar codes.
if supportedBarCodes.contains(metadataObj!.type) {
// if metadataObj.type == AVMetadataObjectTypeQRCode {
// 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 {
. . .
}
}
}
func scanQRFromGallery(qrcodeImg : UIImage) {
let detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])!
let ciImage:CIImage = CIImage(image:qrcodeImg)!
var qrCodeLink=""
let features=detector.features(in: ciImage)
for feature in features as! [CIQRCodeFeature] {
qrCodeLink += feature.messageString!
}
if qrCodeLink=="" {
print("qrCodeLink is empty")
}
else{
print("message: \(qrCodeLink)")
}
}
任何帮助将不胜感激。
【问题讨论】:
-
如果有帮助请告诉我
-
@Dev_Tandel 它像今天一样工作。那里发布了稍微修改的代码。