How to scan Qr code from an image picked with UIIm

2019-04-10 03:40发布

问题:

I am able to scan qr code with camera without using any external libraries like ZBAR or ZXING.

Now what I actually want is to let the user upload an image from photo library and get the qr code scanned from that uploaded image.

I know how to use UIImagePickerController (used it before).

What I want to ask is that, How can I scan qr code from an uploaded image (without using the camera)

You can suggest some code (.h / .m files) that I can add in my project.

Please don't suggest me any external libraries like ZBAR or ZXING.

回答1:

I know you might have found solution for this but i am replying to the post if my answer may help other for relevant question.

Here I am posting my code to ScanQRCode from image picked from gallery without using any Library.

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



    if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
                 let detector:CIDetector=CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])

    let ciImage:CIImage = CIImage(image:pickedImage)!

    var qrCodeLink = ""

    let features=detector.featuresInImage(ciImage)

    for feature in features as! [CIQRCodeFeature] {

        qrCodeLink += feature.messageString
    }

    print(qrCodeLink)//Your result from QR Code
}

    dismissViewControllerAnimated(true, completion: nil)
}


回答2:

In Apple's iOS, a QR code reader is not natively included. Therefore there is no API from Apple you can use.

You have to rely either or third party SDKs or write your own code.



标签: ios qr-code