Solved

Converting a Picture into Hexadecimal values in JAVA

Posted on 2007-11-19
7
732 Views
Last Modified: 2008-02-01
I have a Picture of size 72x28 pixels. I want to send this picture to a mobile as an EMS. How can I convert this picture to Hexadecimal values?
0
Comment
Question by:sangameshrh
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 20318519
You need to make sure that the picture is formatted in the correct way.

For that you have to make sure that the pciture is PURE black and white.
As the size is 72 X 28, it is an OTA image, the size being 252 bytes (cannot be more)

The content to be sent should start with these 4 bytes :

byte0 -> 0000 0000 (0)
byte1 -> 0100 1000 (72)
byte2 -> 0001 1100 (28)
byte3 -> 0000 0001 (1)



Now read image, pixel by pixel, from left to right and top to bottom, every 8 pixel makes a byte for appending to the content:

black pixels = 0
Other pixels = 1


So in all you would have 4+252 = 256 bytes.


In short, create a byte array of 256.
Add the first 4 bytes as shown above.
Parse the entire image pixel-wise and append the bytes to array
Remember that 8 pixels makes a byte, so you need to convert every 8 pixels (bits) and calculate ists byte values, for eg, if the 8 pixels are 00000100, teh byte value will be 4;
0
 

Author Comment

by:sangameshrh
ID: 20318606
I have code which looks like this.

public String convertImage2Hex(Image img)
      {
            zero_ones="";
            this.img = img;
            
            width            =      72;
            height            =      28;
            
            int arr[] = new int[width*height];
            
            PixelGrabber pg = new PixelGrabber(img,0,0,width,height,arr,0,width);
            
            try
            {
                  pg.grabPixels();
            }
            catch (InterruptedException e)
            {
                  System.err.println("interrupted waiting for pixels!");
            }
            
            StringBuffer buff = new      StringBuffer("");
            for (int j = 0; j < height; j++)
            {
                  for (int i = 0; i < width; i++)
                  {
                        buff.append(getPixelInfo(i,j,arr[j*width+i]));
                  }
            }
            zero_ones      =      String.valueOf(buff);
            String hex = getHexNumers();
            return(hex.toUpperCase());
      }
      
      public String getPixelInfo(int x, int y, int pixel)
      {
            String res = "-1";
            
            int red   = (pixel >> 16) & 0xff;
            int green = (pixel >>  8) & 0xff;
            int blue  = (pixel      ) & 0xff;
            
            if(red>=200 && green>=200 && blue>=200)
            {
                  res = "0";
            }
            else if(red<=199 && green<=199 && blue<=199)
            {
                  res = "1";
            }
            return(res);
      }
      
      public String getHexNumers()
      {
            int i=0;
            String hex="", temp="";
            StringBuffer buff = new      StringBuffer("");
            for(i=0; i<zero_ones.length();i+=4)
            {
                  temp = zero_ones.substring(i,(4+i));
                  BigInteger val = new BigInteger(temp,2);
                  buff.append(val.toString(16));
            }
            hex      =      String.valueOf(buff);
            return (hex);
      }

Any problems with that?
0
 
LVL 12

Accepted Solution

by:
keyurkarnik earned 500 total points
ID: 20318999
The program is fine
But I am sure that you are doing this for sending PIC SMS. So you need to add a header.

Also, though the code is correct, here is a slightly modified version, removing the class variables - its better to pass values in such cases!!


Here is the code with the headers!!
public static  String getHexImageWithHeaders(Image img, int width, int height)

   {

     

//      String w = Integer.toHexString(width);

      String w = Integer.toHexString(width/8);

      w = w.length()==1 ? "0"+w : w;

      String h = Integer.toHexString(height);

      h = h.length()==1 ? "0"+h : h;

      String header = "00" + w + h + "00";

      return header + convertImage2Hex(img, width, height);

   }

   

   

   public static  String convertImage2Hex(Image img, int width, int height)

   {

         String zero_ones="";

         

         int arr[] = new int[width*height];

         

         PixelGrabber pg = new PixelGrabber(img,0,0,width,height,arr,0,width);

         

         try

         {

               pg.grabPixels();

         }

         catch (InterruptedException e)

         {

               System.err.println("interrupted waiting for pixels!");

         }

         

         StringBuffer buff = new      StringBuffer("");

         for (int j = 0; j < height; j++)

         {

               for (int i = 0; i < width; i++)

               {

                     buff.append(getPixelInfo(i,j,arr[j*width+i]));

               }

         }

         zero_ones      =      String.valueOf(buff);

         String hex = getHexNumers(zero_ones);

         return(hex.toUpperCase());

   }

   

   public static String getPixelInfo(int x, int y, int pixel)

   {

         String res = "-1";

         

         int red   = (pixel >> 16) & 0xff;

         int green = (pixel >>  8) & 0xff;

         int blue  = (pixel      ) & 0xff;

         

         if(red>=200 && green>=200 && blue>=200)

         {

               res = "0";

         }

         else if(red<=199 && green<=199 && blue<=199)

         {

               res = "1";

         }

         return(res);

   }

   

   public static  String getHexNumers(String zero_ones)

   {

         int i=0;

         String hex="", temp="";

         StringBuffer buff = new      StringBuffer("");

         for(i=0; i<zero_ones.length();i+=4)

         {

               temp = zero_ones.substring(i,(4+i));

               BigInteger val = new BigInteger(temp,2);

               buff.append(val.toString(16));

         }

         hex      =      String.valueOf(buff);

         return (hex);

   }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:keyurkarnik
ID: 20319003
I dont remember whether line 4 or line 5 should be used. Try whichever works
0
 

Author Comment

by:sangameshrh
ID: 20325472
There is something wrong in your code.
I should get the header as
00481C01
for 72x28 image.
But your code is giving
00481C00

with this header mobile is unable to read the picture message.
0
 

Author Comment

by:sangameshrh
ID: 20326060
How can I convert Hexdump back to to Picture Message?
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 20326624
Oops, that was a typo at line 9

String header = "00" + w + h + "00";

It should be String header = "00" + w + h + "01";


Anyways, to read the hex back to SMS, you need to follow the exact reverse.

1. First read the first 4 bytes : 00481C01
Depending on the size of the image the values will vary.

Convert the 2nd and 3rd byte to decimal values. That will give you the width and height respectively (48 = 72, 1C = 28)

2. Once that is done, Read the rest of the hex array, and convert it to bits. Each character will represent 4 bits. Create a full String or byte array as you please.

The size of this array will be width * height - in pixels

3. Now parse the entire array and convert it into a pixel array
Apply the same logic - if the pixel bit is 1, it is black, if its is 0 - it is white.  (Do the reverse of the ARGB bit shifting)

Now convert the pixel array into picture again.

Here is a sample code for the conversion :

http://www.seas.gwu.edu/~simhaweb/cs133/useful/ImageTool.java

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 69
micro services vs rest web services 16 84
Java passing arguments (type error) 2 48
Way to decrease size of apk file 9 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now