Hello team,
I use this code to get the rect of a QR code from an image and then i want to save it as a new image (just the QR).
The problem is that code does not crop the QR code, but another random rect from that image.
What is the problem with this code?
Thank you for your time.
I use this code to get the rect of a QR code from an image and then i want to save it as a new image (just the QR).
B4X:
Sub fileQR(bmp As Bitmap)
Dim no As NativeObject = Me
Dim res As List = no.RunMethod("detectRectQR:", Array(bmp))
For Each feature As NativeObject In res
Dim CGRect As Object = feature.GetField("bounds").RunMethod("CGRectValue", Null)
Dim b As Bitmap
b=no.RunMethod("ImageInRect::",Array(bmp,CGRect))
Dim Out As OutputStream
Out = File.OpenOutput(xui.DefaultFolder, "pass.png", False)
b.WriteToStream(Out, 100, "PNG")
Out.Close
Next
End Sub
#if OBJC
@import CoreImage;
- (NSArray*) detectRectQR: (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 *faceDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:detectorOptions];
NSArray *features = [faceDetector featuresInImage:[CIImage imageWithCGImage:img.CGImage]
options:@{CIDetectorImageOrientation:[NSNumber numberWithInt:exifOrientation]}];
if (features != nil && features.count > 0) {
for (CIQRCodeFeature* qrFeature in features) {
NSLog(@"QRFeature.messageString : %@ ", qrFeature.messageString);
}
}
return features;
}
-(UIImage*)ImageInRect: (UIImage*)image :(CGRect)rect
{
UIImage * LandscapeImage = image;
UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
scale: 1.0
orientation: UIImageOrientationLeft];
CGImageRef imageRef = CGImageCreateWithImageInRect([PortraitImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
#end if
The problem is that code does not crop the QR code, but another random rect from that image.
What is the problem with this code?
Thank you for your time.