I have an application that saves all its data to a text file. It can then load the textfile and reinstate the data. That works great. But my problem is I'm not actually saving all the data I want to yet; my program uses jpegs I am not saving. During runtime my program stores paths to the jpgs and loads them as needed. I know how to write the jpgs to disk. But I would like all my data for the program to be saved to a single document.
So my question is, is there perhaps anyway I could write a jpg into a format that could be added to a textfile, or a string? If so I could just tag it and then split() the whole text file when I need to load the data, as I have been doing with my other data. If not, I was thinking I could do a simple xml document that stored the textfile with my application data and then the jpgs as well. But I don't know how to store jpgs in xml. Are there any solutions? Thanks.
Also, I can encode an ImageIcon in Base64 and load it, but I realized I would like to encode a JPG instead of an ImageIcon, so a program not of Java could load it from Base64.
The only way I know how to create a JPG is use ImageIO to write a BufferedImage to a file as JPG. However I don't want to write to a File, I just want to write to a "jpg" object I can convert to Base64 and place in my outputString. Do you know a way that I could do this? Thanks.
Joined: Aug 05, 2005
When you say 'use ImageIO to write a BufferedImage to a file as JPG', I assume you mean you are using the
ImageIO.write(RenderedImage im, String formatName, File output)
method. If so, couldn't you use the
ImageIO.write(RenderedImage im, String formatName, OutputStream output)
method and then use the OutputStream as an input to your Base64 encoding method ?
Note - I've never used ImageIO so this was just a guess based on a quick look at the API.
Joined: Apr 13, 2009
Yes that is the method I am using. As far as I know creating an OutputStream requires a File which requires a path to somewhere on the harddrive. And as soon as I write to the OutputStream it will write that data to the file on the hard drive. Also if I was able to have the OutputStream hold the data without writing to or creating a file, I'm not sure how I would get the data back out of it when loading.
What would be perfect is to just use an Object to write the the jpeg to and use it as a container to hold the jpg, then encode the "jpg" (but really Object).
Except I'm not sure how I would write to an Object, as it seems I can only write jpgs to an OutputStream, which requires me to write to disk.
Joined: Aug 05, 2005
No. An OutputStream doesn't have to write to a file.
Take a look at the PipedOutputStream class for example. You could use the OutputStream end of the pipe as the parameter to the ImageIO.write method and use the InputStream end as input to your Base64 encoding class.
Or the ByteArrayOutputStream class. Use that as the OutputStream and then get the Base64 encoding class to read the data from the byte array that it creates.
Joined: Apr 13, 2009
Thanks this should work great. I am going to use the byteArrayOuputStream. My only concern is how to it back into an image. I'm trying, where "data is the string data":
At this last point img is never initialized and is null when I try to use it. ImageIO.read() says it accepts an inputStream. I really don't know how to read back the jpg I save.
The only other thing I would add is that the size of the text file that holds the jpg in Base64 is 40 KB, versus the actual jpg file size is 144. Is it the case that Base64 saves things in a smaller file, or could this mean I'm not saving correctly? There is text in the file so I know the pic is being saved.
I got it to work. Instead of created a byte I used a built in method of the Base64 class for dealing with bytes, and it worked. I did this because I had read a post in these forums about somebody corrupting the Byte64 data when they copied it to a string.
It's working, but the file size (that is text) with the saved picture is still 40 KB on disk (so my finder window says). O well. It's working.
subject: A question regarding saving jpgs into text files.