Results 1 to 8 of 8
Thread: Reading image files into Strings
- 10-29-2011, 07:51 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Reading image files into Strings
For a program I'm working on, it's required to read in data from an image file, store it in as a string in it's own file, and then be able to read the data and put it into a BufferedImage. I wrote some testing code to try to get a hang of what the process might need.
If I read in the data as a byte array and use a byte stream to create the BufferedImage, it displays fine in a Swing application:
The image displays just as it should. But if I convert the byte array into a String, use getBytes on that String, and supply that array to the byte stream, the image comes out wrong.Java Code:import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.stream.FileImageInputStream; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class TestCode { public static void main(String[] args) { try { File f = new File("star.jpg"); FileImageInputStream im = new FileImageInputStream(f); byte[] data = new byte[(int) f.length()]; int read = 0; int bytesRead = 0; while (read < data.length && (bytesRead = im.read(data, read, data.length - read)) > -1) { read += bytesRead; } BufferedImage img = ImageIO.read(new ByteArrayInputStream(data)); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(800, 600)); frame.getContentPane().add(new JLabel(new ImageIcon(img))); frame.pack(); frame.setVisible(true); } catch (IOException e) { e.printStackTrace(); } } }
Is there a particular process I have to do when converting between the byte array and String that will make this work? Also, as soon as I find a solid method I can use in a closed-source proprietary program, I plan on encoding the data in Base64. I don't know if that changes anything, but I figured I should mention it.Java Code:import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.stream.FileImageInputStream; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class TestCode { public static void main(String[] args) { try { File f = new File("star.jpg"); FileImageInputStream im = new FileImageInputStream(f); byte[] data = new byte[(int) f.length()]; int read = 0; int bytesRead = 0; while (read < data.length && (bytesRead = im.read(data, read, data.length - read)) > -1) { read += bytesRead; } String dataStr = new String(data); BufferedImage img = ImageIO.read(new ByteArrayInputStream(dataStr.getBytes())); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(800, 600)); frame.getContentPane().add(new JLabel(new ImageIcon(img))); frame.pack(); frame.setVisible(true); } catch (IOException e) { e.printStackTrace(); } } }
-
Re: Reading image files into Strings
You may be running into a Charset issue where bytes that aren't part of the default Charset are being transformed into the byte equivalent of '?'. What if you read data into a String with a different Charset, and read it out with the same?
For example:
Java Code:while (read < data.length && (bytesRead = im.read(data, read, data.length - read)) > -1) { read += bytesRead; } System.out.println("charset: " + Charset.defaultCharset().displayName()); Charset charset = Charset.forName("ISO-8859-1"); String dataStr = new String(data, charset); byte[] data2 = dataStr.getBytes(charset); BufferedImage img = ImageIO.read(new ByteArrayInputStream(data2));
- 10-29-2011, 08:57 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Re: Reading image files into Strings
ISO-8859-1 does work on this image. Is there any way to figure out how images are encoded? Or is there a standard that says all images have to be encoded under a certain charset?
-
Re: Reading image files into Strings
The images aren't "encoded" since they aren't Strings. You do need to use a Charset that allows maximum flexibility and accepts all bytes. Probably better would be to manually write and read the byte Strings in and out.
- 10-29-2011, 10:27 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Re: Reading image files into Strings
Ah ok. Well the only one that doesn't give me an error in my code is ISO-8859-1. Would that be a safe one? I'll know nothing about the incoming files. They are user supplied.
-
Re: Reading image files into Strings
I don't know, but the whole thing sounds iffy to me. Again if I were to do this and I absolutely had to use a String, I'd go the safe route and write each byte out explicitly, possibly to a StringBuilder. Then I'd have no need to worry about Charset.
- 10-29-2011, 11:58 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Re: Reading image files into Strings
Ok. Well it's being saved into an XML file with other data. The current design is to generate the XML and save it in a String and write that all to a file using an output stream. That's the reason for converting it to a String. I do happen to use a StringBuilder for that functionality, so it wouldn't be any extra work to use a StringBuilder.
Thanks for all the help by the way. I've (obviously) not learned much about encoding, working with bytes, and other things of that nature.
-
Re: Reading image files into Strings
And I'm not a pro regarding Charsets either, so please take all advice I give on this subject with a grain of salt. I'm learning as I go! Best of luck!
Similar Threads
-
Reading strings from the user but dont want to accept numbers ,!
By javanew in forum New To JavaReplies: 1Last Post: 09-24-2010, 07:08 PM -
Bug Reading txt files
By MHardeman25 in forum New To JavaReplies: 4Last Post: 08-13-2010, 10:03 PM -
Reading in strings
By thekermo in forum New To JavaReplies: 2Last Post: 10-19-2008, 05:24 PM -
Text and image files within jar files
By erhart in forum Advanced JavaReplies: 8Last Post: 01-19-2008, 04:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks