I am having issues creating a file that I can properly use as a ZXCGImageLuminanceSource, when I try use a png file directly as follows
ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:image.CGImage]My app crashes and the error message is that this luminance source does not support cropping
So I thought I would create a CGImage using CGImageCreate as follows
CGImageRef newImageRef = CGImageCreate (
width,
height,
bitsPerComponent,
bitsPerPixel, /
bytesPerRow,
colorspace,
bitmapInfo,
/
provider,
NULL,
false,
kCGRenderingIntentDefault
);But this crashes with the following error message
Jun 13 04:33:33 nwtill[5517] <Error>: CGImageCreate: invalid image bits/pixel: 8.I am unable to find a combination of parameters that works and that will create an image that ZXCGImageLuminanceSource can use and crop.
The entire m file below for reference
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import "scanManager.h"
@import UIKit;
@interface scanManager ()
@end
@implementation scanManager
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"some_barcodes.png"];
if (image == nil ) {
NSLog(@"Picture is NIL");
return;
}
/
CGImageRef imageRef = image.CGImage;
NSData *data = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
char *pixels = (char *)[data bytes];
/
/
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
int pixelNr = 10;
int red = pixelNr*3;
int green = pixelNr*3+1;
int blue = pixelNr*3+2;
/
/
pixels[red] = 0;
pixels[green] = 0;
pixels[blue] = 0;
/
/
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
/
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
/
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL);
/
/
CGImageRef newImageRef = CGImageCreate (
width,
height,
bitsPerComponent,
bitsPerPixel, /
bytesPerRow,
colorspace,
bitmapInfo,
/
provider,
NULL,
false,
kCGRenderingIntentDefault
);
/
/
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
/
/
/
UIImageWriteToSavedPhotosAlbum(newImage,nil,nil,nil);
ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:newImage.CGImage];
ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
ZXDecodeHints *hints = [ZXDecodeHints hints];
hints.tryHarder = YES;
ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
ZXGenericMultipleBarcodeReader *multiReader = [[ZXGenericMultipleBarcodeReader alloc] initWithDelegate:reader];
NSError *error = nil;
NSArray *results = [multiReader decodeMultiple:bitmap hints:hints error:&error];
for (ZXResult *result in results) {
NSLog(@"Decoded barcode %@", result.text);
}
NSLog(@"Before cleanup");
/
/
/
/
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);
/
CGImageRelease(newImageRef);
NSLog(@"After Cleanup");
}
@end