I'm having problems converting a UIImage to a pure black and white format. So far I've looked at these two sites:
http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/
http://stackoverflow.com/questions/4401567/getting-a-black-and-white-uiimage-not-grayscale
I followed the first tutorial on converting an image to a bitmap and back. And I'm using code similar to the code on stackoverflow:
I know this can working because when I supply an image such as my app's default image it does get converted to black and white, quite nicely. But if I use an image taken on the phone (an image of, say, a paper with large text print on it) the image comes out as an ugy soup of black and white, no matter how good I make the lighting and how clear the image is. Can anyone suggest anything?
Also, I have tried messing around with the darkness threshold, to no avail.
http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/
http://stackoverflow.com/questions/4401567/getting-a-black-and-white-uiimage-not-grayscale
I followed the first tutorial on converting an image to a bitmap and back. And I'm using code similar to the code on stackoverflow:
Code:
+(UIImage *)blackAndWhiteImageForImage:(UIImage *)image {
unsigned char *bitmap = [self convertImageToBitmap:image];
for (int i=0;i<image.size.width * image.size.height * 4; i+=4) {
if ((bitmap[i] + bitmap[i + 1] + bitmap[i + 2]) < (255 * 3 / 2)) {
bitmap[i ] = 0;
bitmap[i + 1] = 0;
bitmap[i + 2] = 0;
} else {
bitmap[i ] = 255;
bitmap[i + 1] = 255;
bitmap[i + 2] = 255;
}
}
image = [self convertBitsToImage:bitmap withSize:image.size];
return image;
}
I know this can working because when I supply an image such as my app's default image it does get converted to black and white, quite nicely. But if I use an image taken on the phone (an image of, say, a paper with large text print on it) the image comes out as an ugy soup of black and white, no matter how good I make the lighting and how clear the image is. Can anyone suggest anything?
Also, I have tried messing around with the darkness threshold, to no avail.