Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert image(.jpg) in to binary format using java

 
muthu bharathi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to convert image(.jpg or .bmp) in to binary format using java?

Please suggest me..




--
With Regards,
M. Bharathi
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Images are binary. What exactly are you trying to do? What should the result of the conversion be?
 
muthu bharathi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ulf,

I have some .jpg images.

I give my input as <filename>.jpg, It should print the corresponding binary format of an image using java

The output should be the binary format for an image.

--
With Thanks and Regards,
M. Bharathi
 
Henry Wong
author
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output should be the binary format for an image.



As Ulf already mentioned, what does this mean? Meaning given a sample jpg file, how should this output look like?

Henry
 
muthu bharathi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Now i've a xml. I've a tag named as <check_Image>, in this tag i've converted the image in to Readable format. I need to be convert in to the corresponding image. Here with i've included the xml file....

<EE_EAI_MESSAGE>
<Check_Image>
ffd8ffe000104a464</Check_Image>

</EE_EAI_MESSAGE>


-- with regards
M. Bharathi

[RP] removed most of the HEX string to preserve the layout [/RP]
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to read a file in bytes? You can then use Integer.toHexString to convert each byte to a two-character HEX string. Keep two things in mind though:
1) a byte is between -128 and 127, and you don't want to convert negative numbers to a HEX string. Add 256 to negative numbers first, thereby making the range 0 to 255 (0 and FF in HEX).
2) Integer.toHexString will return a one-character HEX string if the number is between 0 and 15 (0 and F in HEX). Don't forget to add that extra 0 before the HEX string.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you have to know the internal structure of that hex string. Is it RGB, RGBA, ARGB, CMY or something else? Do you know the width and height of the image?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Of course, you have to know the internal structure of that hex string. Is it RGB, RGBA, ARGB, CMY or something else? Do you know the width and height of the image?

If the string is the HEX representation of the file itself then that doesn't matter. The code reading the XML file can simply turn it back into the original file.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. Maybe muthu will clue us in what we're looking at.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic