iOS Question How to recognize QR code in image

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes.

1. Add this code:
B4X:
#if OBJC
@import CoreImage;
- (NSArray*) detectQR: (UIImage*)img {
int exifOrientation;
switch (img.imageOrientation) {
  case UIImageOrientationUp:
  exifOrientation = 1;
  break;
  case UIImageOrientationDown:
  exifOrientation = 3;
  break;
  case UIImageOrientationLeft:
  exifOrientation = 8;
  break;
  case UIImageOrientationRight:
  exifOrientation = 6;
  break;
  case UIImageOrientationUpMirrored:
  exifOrientation = 2;
  break;
  case UIImageOrientationDownMirrored:
  exifOrientation = 4;
  break;
  case UIImageOrientationLeftMirrored:
  exifOrientation = 5;
  break;
  case UIImageOrientationRightMirrored:
  exifOrientation = 7;
  break;
  default:
  break;
}

NSDictionary *detectorOptions = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh };
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode  context:nil options:detectorOptions];

NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:img.CGImage]
  options:@{CIDetectorImageOrientation:[NSNumber numberWithInt:exifOrientation]}];
   return features;
}
#end if

2. Use it like this:
B4X:
Dim no As NativeObject = Me
Dim res As List = no.RunMethod("detectQR:", Array(LoadBitmap(File.DirAssets, "qr.png")))
For Each qr As NativeObject In res
   Log(qr.RunMethod("messageString", Null).AsString)
Next
 
Upvote 0
Top