简体   繁体   English

本机从UIImage扫描条形码(即,不使用ZBar)

[英]Scanning barcode from UIImage natively (i.e., not using ZBar)

I've been using the native barcode scanner functionality provided by apple since iOS7 and it is great, but I needed to scan some static images I had so I could automatically catalogue some barcodes. 自iOS7以来,我一直在使用Apple提供的本机条形码扫描器功能,这很棒,但是我需要扫描一些静态图像,以便能够自动对一些条形码进行分类。

I couldn't find a way to do this natively, so I used an open source package called zBar, and for the most part, it works fine. 我找不到本机执行此操作的方法,因此我使用了一个名为zBar的开源软件包,并且在大多数情况下,它都能正常工作。

However, it returns false values often and sometimes flat out fails to find the barcode. 但是,它经常返回错误的值,有时整齐地找不到条形码。 I also built the c++ library from scratch but I got the same results in my OS X build. 我也从头开始构建c ++库,但是在OS X构建中得到了相同的结果。 It also seems to be an abandoned project. 这似乎也是一个废弃的项目。

Apple's native solution finds the barcode on this static image even if I scan the image from my computer monitor! 即使我从计算机显示器扫描图像,Apple的本机解决方案也会在此静态图像上找到条形码! Same thing goes for the images that return false/incorrect values. 返回错误/不正确值的图像也是如此。

So is there anyway to use apple's libraries to scan a UIImage? 那么,有没有可以使用苹果的库来扫描UIImage呢?

Try this 尝试这个

CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
if (detector)  {
  NSArray* featuresR = [detector featuresInImage:scannedImg.CIImage];
  NSString* decodeR;
  for (CIQRCodeFeature* featureR in featuresR)  {
    NSLog(@"decode %@ ",featureR.messageString);
    decodeR = featureR.messageString;
  }
}

An option available now is MLKit from Firebase. 现在可用的选项是Firebase的MLKit。

Add Firebase to your project after setting up your project in their console https://firebase.google.com 在控制台中设置项目后,将Firebase添加到项目中https://firebase.google.com

I use cocoapods to manage dependencies, if you do then add this to your Podfile and pod update : 我使用cocoapods管理依赖项,如果您这样做,则将其添加到Podfile和pod update

pod 'Firebase/Core'
pod 'Firebase/MLVision'
pod 'Firebase/MLVisionBarcodeModel'

Here's a demo view controller implementation in which you pick a barcode image from your photo library to be scanned by ML kit, and the results are printed in a label. 这是一个演示视图控制器的实现,其中您从照片库中选择要由ML kit扫描的条形码图像,并将结果打印在标签中。

// (Don't forget to ask for Photos access by including this in your info.plist)

    <key>NSPhotoLibraryUsageDescription</key>
    <string>Enable photo library access to select a photo from your library.</string>



import Firebase

class MyDemoViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    lazy var vision = Vision.vision()
    @IBOutlet var textLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func selectImage(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
            print("Button capture")

            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .savedPhotosAlbum;
            imagePicker.allowsEditing = false

            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            return
        }

        dismiss(animated: true, completion: {
            self.checkForCodeInImage(image: image)
        })
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion:nil)
    }

    internal func checkForCodeInImage(image: UIImage) {
        let detector = vision.barcodeDetector(options: VisionBarcodeDetectorOptions(formats: .all))
        let vImage = VisionImage(image: image)

        detector.detect(in: vImage) { features, error in
            guard error == nil, let barcodes = features, barcodes.isEmpty == false else {
                DispatchQueue.main.async {
                    self.textLabel.text = "No code found in selected image."
                }
                return
            }

            var text = ""

            for barcode in barcodes {

                guard let rawValue = barcode.rawValue else {
                    continue
                }

                let corners = barcode.cornerPoints
                let displayValue = barcode.displayValue

                print("Corners: \(String(describing: corners))")
                print("Found: \(String(describing: displayValue))")

                text.append(rawValue)
                text.append("\n\n")

//                let valueType = barcode.valueType
//                switch valueType {
//                case .wiFi:
//                    let ssid = barcode.wifi!.ssid
//                    let password = barcode.wifi!.password
//                    let encryptionType = barcode.wifi!.type
//                case .URL:
//                    let title = barcode.url!.title
//                    let url = barcode.url!.url
//                default:
//                    // See API reference for all supported value types
//                    break
//                }
            }

            DispatchQueue.main.async {
                self.textLabel.text = text
            }
        }
    }
}

Here's a link to their documentation: https://firebase.google.com/docs/ml-kit/ios/read-barcodes 以下是其文档的链接: https : //firebase.google.com/docs/ml-kit/ios/read-barcodes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM