aspose file tools*
The moose likes Java in General and the fly likes A question regarding saving jpgs into text files. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "A question regarding saving jpgs into text files. " Watch "A question regarding saving jpgs into text files. " New topic
Author

A question regarding saving jpgs into text files.

Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
Base64 encoding is a commonly used way of saving binary data to a text file.


Joanne
Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
Thankyou for the kind response. I was googling this and I couldn't find byte64 but base64. I am reading about it now.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
Whoops. You're right, base64 is what I meant. I've edited my post.
Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
Thanks that helped a lot. I found a good class for Base64 encoding:

http://iharder.sourceforge.net/current/java/base64/

and I successfully saved an image and loaded it.
Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
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.

Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
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.
Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
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":

byte[] arrayOfBytes=Base64.decode(data);

System.out.println("Byte Array exists?: "+arrayOfBytes.length);

ByteArrayInputStream bais=new ByteArrayInputStream(arrayOfBytes);

System.out.println("Byte Array Input Stream exists?: "+bais.available());

//Doesn't work:
BufferedImage img = ImageIO.read(bais);


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.
Thanks again.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41812
    
  62
Base-64 encoded binary data would be larger than the original, so if you get 40KB instead of 144KB, then yes - something's not working right.


Ping & DNS - my free Android networking tools app
Sumner Khloe
Greenhorn

Joined: Apr 13, 2009
Posts: 14
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.

Thanks lots.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question regarding saving jpgs into text files.