Parth Patel p1nt0z
Parth Patel p1nt0z

Reputation: 583

Is it possible to decode QRCode image to value

I'm looking for code which help me to convert QRCode from image. there is very easy way with AVFoundation to scan QR Code with help of camera. But if I want to get encoded qr string from UIImage is there any way to do so?

Upvotes: 4

Views: 7291

Answers (5)

Bharat Modi
Bharat Modi

Reputation: 4180

Just posting for people who have tried above answers but that doesn't worked for them. This worked for me.

CIContext *context = [CIContext contextWithOptions:nil];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
CIImage *imageQRCode = [CIImage imageWithCGImage:<YourImageToBeRead>.CGImage];
NSArray *features = [detector featuresInImage:imageQRCode];
        CIQRCodeFeature *feature = [features firstObject];

NSString *result = feature.messageString;

Upvotes: 0

Duncan C
Duncan C

Reputation: 131491

EDIT:

This answer describes how to scan for QR codes in the input from the camera. That's not what the OP asked. The OP asked about scanning a still image. See @DanSkeel's answer for that.

I'm leaving this answer since it is still useful.


It is indeed possible. Apple added an API for detecting barcodes in iOS 7.

Your search terms are AVCaptureMetadataOutput, AVMetadataMachineReadableCodeObject.

It's a little involved. You have to create a AVCaptureSession, find the camera and attach it as a capture device, and then create an AVCaptureMetadataOutput object and tell it to monitor for specific types of bar codes (in your case AVMetadataObjectTypeQRCode).

There is a sample project by shinobicontrols that shows how to do it. Go to this link:

shinobicontrols - Introducing iOS7 Day-by-Day

The project you want is Day 16: Decoding QR codes.

Note that Apple's bar code reading API can read a couple of dozen types of barcodes. You tell it which types you are interested in. I modified the demo project to detect ALL supported barcodes. It was able to detect UPC codes, US postal barcodes, QR codes, and lots of others that I was able to find and test.

BTW, that's a great site. Lots of very helpful little demos of new APIs Apple introduced in iOS 7 there.

EDIT:

Ok, I guess I exaggerated. Apple's code can decode the following barcode formats (I count 10, which is hardly "dozens"...)

AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode

Upvotes: 3

Peter Lapisu
Peter Lapisu

Reputation: 21005

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;
  }
}

Upvotes: 5

DanSkeel
DanSkeel

Reputation: 4005

Native solution:
As far as I know you can scan QR from video since iOS 7. (See other answers)
But scanning from image appeared only in iOS 8.

See CIQRCodeFeature

and this shinobicontrols tutorial

third party libs:
For example ZBar

Upvotes: 9

ahwulf
ahwulf

Reputation: 2584

We use https://github.com/TheLevelUp/ZXingObjC which supports every kind of barcode including QR. It's a pure Objective-C version of ZXing which is now java only.

Upvotes: 2

Related Questions