views:

273

answers:

2

Anyone knows how to convert .jpg image to .bmp format in iphone using objective-C? And how i process(or RGB color) the each pixel of IPhone Device caputured image? Is there need to conversion of image type?

A: 

You won't be able to easily get to a bmp representation on the iPhone. In Cocoa on the Mac, it is managed by the NSBitmapImageRep class and is pretty straight forward as outlined below.

At a high level, you need to get the .jpg into an NSBitmapImageRep object and then let the frameworks handle the conversion for you:

a. Convert the JPG image to an NSBitmapImageRep

b. Use built in NSBitmapImageRep methods to save in desired formats.

NSBitmapImageRep *origImage = [self documentAsBitmapImageRep:[NSURL fileURLWithPath:pathToJpgImage]];
NSBitmapImageRep *bmpImage = [origImage representationUsingType:NSBMPFileType properties:nil];

- (NSBitmapImageRep*)documentAsBitmapImageRep:(NSURL*)urlOfJpg;
{

    CIImage *anImage = [CIImage imageWithContentsOfURL:urlOfJpg];
    CGRect outputExtent = [anImage extent];

    // Create a new NSBitmapImageRep.
    NSBitmapImageRep *theBitMapToBeSaved = [[NSBitmapImageRep alloc]  
                                            initWithBitmapDataPlanes:NULL pixelsWide:outputExtent.size.width  
                                            pixelsHigh:outputExtent.size.height  bitsPerSample:8 samplesPerPixel:4  
                                            hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace  
                                            bytesPerRow:0 bitsPerPixel:0];

    // Create an NSGraphicsContext that draws into the NSBitmapImageRep.  
    NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:theBitMapToBeSaved];

    // Save the previous graphics context and state, and make our bitmap context current.
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext: nsContext];
    CGPoint p = CGPointMake(0.0, 0.0);

    // Get a CIContext from the NSGraphicsContext, and use it to draw the CIImage into the NSBitmapImageRep.
    [[nsContext CIContext] drawImage:anImage atPoint:p fromRect:outputExtent];

    // Restore the previous graphics context and state.
    [NSGraphicsContext restoreGraphicsState];

    return [[theBitMapToBeSaved retain] autorelease];

}

On the iPhone, BMP is not directly supported by UIKit, so you would have to drop down into Quartz/Core Graphics and manage the transformation yourself.

Pixel by pixel processing is much more involved. Again, you should get intimately familiar with the core graphics capabilities on the device if this is a hard requirement for you.

Chip Coons
A: 
  1. Load the JPG image into a UIImage, which it can handle natively.
  2. Then you can grab the CGImageRef from the UIImage object.
  3. Create a new bitmap CG image context with the same properties of the image you already have, and provide your own data buffer to hold the bytes of the bitmap context.
  4. Draw the original image into the new bitmap context: the bytes in your provided buffer are now the image's pixels.
  5. Now you need to encode the actual BMP file, which isn't a functionality that exists in the UIKit or CoreGraphics (as far as I know) frameworks. Fortunately, it's an intentionally trivial format-- I've written quick and dirty encoders for BMP in an hour or less. Here's the spec: http://www.fileformat.info/format/bmp/egff.htm (Version 3 should be fine, unless you need to support alpha, but coming from JPEG you probably don't.)

Good luck.

quixoto
@quixoto, hi, your answer really helpful to me thank.Can i read or comparing the words(which is lies on the image) with other words(this also be lies on the image)? If it possible how i do that?
RRB
Do you mean actual rendered text within the image? If so, not trivially-- text recognition from images is an entire field of research. :) (If you mean words as in chunks of bytes, that's different)
quixoto