sher17
sher17

Reputation: 617

UIImage to Hex Conversion in Objective C

I have the following C# coding to convert an uploaded bitmap image to Hex. I need to follow the algorithm to come ip with the same goal in objective C. Assume I have a UIImageView with an image selected from the Picture gallery. I need to convert this selected image to Hex and write to a text file.

Someone please give me a help to convert this code to Objective C. I have some knowledge in IOS development but have not much knowledge in Image processing.

This is my code,

            Bitmap bm1 = (Bitmap)pictureBox1.Image;

            int PictureWidth = pictureBox1.Image.Width;
            int PictureHeight = pictureBox1.Image.Height;
            int PictureX;
            int PictureY;

            int NVImageWidth;
            int NVImageHeight;

            NVImageWidth = PictureWidth;
            NVImageHeight = PictureHeight;

            int Quotient;
            int Remainder;
            int wp;

            wp = 0;

            StreamWriter sw = new StreamWriter("D:\\Test.txt");

            for (PictureX = 0; PictureX <= PictureWidth - 1; PictureX++)
            {
                for (PictureY = 0; PictureY <= NVImageHeight - 1; PictureY++)
                {
                    Color c1 = bm1.GetPixel(PictureX, PictureY);

                    if ((PictureY % 8) == 0)
                    {
                        wp = 0;
                        wp = (c1.G!=0) ? 0 : 1;
                    }
                    else if ((PictureY % 8) == 7)
                    {
                        wp = (wp << 1) | ((c1.G!=0) ? 0 : 1);
                        sw.Write(string.Format("%{0:x2}", wp));
                    }
                    else
                    {
                        wp = (wp << 1) | ((c1.G!=0) ? 0 : 1);
                    }
                }
            }

            sw.Close();

Upvotes: 0

Views: 796

Answers (1)

lakshmen
lakshmen

Reputation: 29094

I have a suggestion:

First convert the UIImage to NSData. Then convert NSData to Hexadecimal.

So for example,

//converting to hexadecimal
    -(NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces
    {
        const unsigned char* bytes = (const unsigned char*)[self bytes];
        NSUInteger nbBytes = [self length];
        //If spaces is true, insert a space every this many input bytes (twice this many output characters).
        static const NSUInteger spaceEveryThisManyBytes = 4UL;
        //If spaces is true, insert a line-break instead of a space every this many spaces.
        static const NSUInteger lineBreakEveryThisManySpaces = 4UL;
        const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;
        NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0);

        NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];
        for(NSUInteger i=0; i<nbBytes; ) {
            [hex appendFormat:@"%02X", bytes[i]];
            //We need to increment here so that the every-n-bytes computations are right.
            ++i;

            if (spaces) {
                if (i % lineBreakEveryThisManyBytes == 0) [hex appendString:@"\n"];
                else if (i % spaceEveryThisManyBytes == 0) [hex appendString:@" "];
            }
        }
        return [hex autorelease];
    }

NSData *imageData = UIImagePNGRepresentation(myImage.image);
NSString* hex = [imageData hexRepresentationWithSpaces_AS:YES];

Upvotes: 1

Related Questions