Sumner Khloe

Greenhorn
+ Follow
since Apr 13, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sumner Khloe

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.
14 years ago
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.

14 years ago
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.
14 years ago
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.
14 years ago
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.
14 years ago
Thankyou for the kind response. I was googling this and I couldn't find byte64 but base64. I am reading about it now.
14 years ago
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.
14 years ago
Thankyou Maneesh, once again you've been a great help. I think I will go with the second option.
14 years ago
So I have to use an Action Object for what is invoked in the actionMap? They seem kind of complicated. Is there any way I could use an ActionListener instead of Action? Do you get what I mean? If it sounds like I am just trying to sidestep the work, I am. But really I just want a buttons action message to be fired to its action listeners whenever a key is pressed.
14 years ago
Thank you! This looks like just what I need.
14 years ago
This seems like it would be a simple thing to do but after searching I couldn't find the solution I was looking for.

I would like to set keys on the keyboard to be the equivalent as pressing certain JButtons in my application. Could somebody help me out? Thanks.
14 years ago
Thanks for the speedy reply.

In this case the user is creating something. The same way the user of a text writing program enters text and saves it to a file. The user is inputting into the program, among other things, images via JFileChooser. When they choose these files and the program runs whenever it needs the photos it will go to the images specified by the user during runtime. But, when the user saves to a location, the program copies all those images from various locations into the new location where the file will be saved (a folder), with the rest of saved data of the program, a serialized object of the application.

This way if a user were to load the saved files after the original images had been moved, the program would still work because it would go to the (new) images it had saved into the save folder that was specified when saving, which are just copies of the original images the user specified during run time.


So my goal is instead of the program saving multiple things into the path the user specified to save in, all of which are loaded when the user loads the serialized object, to just save one "thing", one file. It would be the document for my application.

Thank-you, I hope I make more sense now.

Sumner



15 years ago
Hello. I've built a java application in which the user loads a number of images into the application at run time. The application stores the path to the image. When the application needs the image it constructs a new imageicon with the path specified, when it no longer needs it it deletes it for garbage collector. When the user saves they specify a folder to save in as well as a name to save as, with JFileChooser. My main data object is serialized here with the specified name, + each image in the program is loaded and written to this location as well, with the specified name + an identifier number. When this file is loaded by the program later it will go to these images it has written when it needs them instead of the original images on disk, which may be in various places on the computer.

So when I save my program with name "Balloon" in folder "Rex" Rex will hold Balloon, as well as BalloonImage0, BalloonImage0-1, BalloonImage0-2, etc. But I would rather just have *one* file saved on disk, instead of saving multiple files to a folder. I want people to be able to move the saved document of the program as a single file, and also not to be able to delete an image in the file as they would be able to do now. So I guess I would want the folder "Rex" to look like one file.

Could anybody help me out with how to do this? I am new to java and I'm not even sure if I'm asking this the right way. Thank-you for anybody's reply.
15 years ago