?
Solved

how to convert an image file to binary string in java

Posted on 2014-07-13
8
Medium Priority
?
2,347 Views
Last Modified: 2014-07-19
Would like to convert the image such as "forward.gif" into a binary string in java.
forward.gif
0
Comment
Question by:jl66
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40193182
http://technojeeves.com/index.php/aliasjava1/12-copying-streams

The class on that page also has a method to convert a File to byte[]. You can then Base64-encode that array
0
 

Author Comment

by:jl66
ID: 40193220
CEHJ, thanks a lot for the tip.
Could you please explain it a bit detailed to me? I guess the method "copyStream" I should use. Right?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40193237
IOUtils.inputStreamToByteArray would be the method you'd need

However, since Java (shame on it) doesn't have a supported Base64 class, you might as well use Apache Commons which will give you a Base64 class and the io routines
http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/IOUtils.html#toByteArray(java.io.InputStream)

http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64String(byte[])
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jl66
ID: 40196167
Thanks a lot for more tips. It took a while to make all work.
However, what was got is
"[71, 73, 70, 56, 57, 97, 32, 0, 32, 0, -9,......]" after calling
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
and then calling
Base64.encodeBase64URLSafeString(imageData);
got the following string
"R0lGODlhIAAgAPcAAP_9weHSof7xgP7......". However, what is needed is a binary string that the low level hardware can recognize. A basic conversion is needed here. It does not seem necessary to encode/decode it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40196358
It does not seem necessary to encode/decode it.
It is necessary actually. What is the alternative? It's not a string of characters. You could represent it as a string of hex numbers but

a. it would be longer than Base64
b. it's still encoded
0
 

Author Comment

by:jl66
ID: 40197761
After reading the image file to byte array, we analyzing the number series: 71, 73, 70, 56, 57, 97, 32, 0, 32, 0, -9,..... Why does each number stands for? Does it represent each byte for the image file? How about "-9"?
These are not the ACSII codes. What codes are they? It seems to have more questions.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 40198079
These are not the ACSII codes. What codes are they?
No, they are not ASCII codes. Such codes are for representing text. You have an image and those are its raw bytes. The byte type is signed in  Java, so the maximum value is 127 and thereafter it starts going negative
0
 

Author Closing Comment

by:jl66
ID: 40206192
Thanks.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 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