File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes save and load JPEG image from disk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "save and load JPEG image from disk" Watch "save and load JPEG image from disk" New topic
Author

save and load JPEG image from disk

Padol Girish
Greenhorn

Joined: Mar 27, 2007
Posts: 6
I have created Whiteboard application using Applet (like MS Paint). I am facing one problem that how i can save the existing contents of drawing Canvas in one JPEG file on disk and how i can load JPEG file from disk to drawing Canvas.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27
Welcome to JavaRanch.

First of all, to do any file I/O, the applet needs to be signed, or the local security policy altered. Both are described in HowCanAnAppletReadFilesOnTheLocalFileSystem.

Reading and writing images is done using the javax.imageio.ImageIO class and its read and write methods. An example is here.
[ May 29, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - updated with new look and Ping home screen widget
Padol Girish
Greenhorn

Joined: Mar 27, 2007
Posts: 6
Thanks Ulf
Its working fine, it save the JPEG file on local disk.
But now i will have to save/load JPEG file to/from server, so how can i do that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27
Well, that's an entirely different problem than saving on the client - ImageIO will not help in that case.

You could either use HTTP to POST the image data to a servlet on the server, or use FTP to transfer the file (if the server has FTP access). Involving a servlet has the advantage that it's easy to perform any post-processing after the upload has finished.
Padol Girish
Greenhorn

Joined: Mar 27, 2007
Posts: 6
Hi Ulf thanks again
I have one more problem
I am not able to figure out How to pass the Applet data(in this case JPEG image) to Http POST method.

Thanks ....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27
Here's an example of an applet posting to a server. In this case, serialized objects are being transferred, but you can send the image data instead.

Alternatively, you can use HTTP file upload if the image exists as a file on the disk. See FileUpload.
Padol Girish
Greenhorn

Joined: Mar 27, 2007
Posts: 6
Facing a new problem
Client requirement changed, now he wants to save/load JPEG image to/from Windows IIS server. How can i do that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27
The applet shouldn't need to care whether it's talking to a Java servlet or IIS (unless you're serializing Java objects, which obviously won't work with IIS).

If you're stuck with the IIS server-side, then this is probably the wrong site to ask for help
Padol Girish
Greenhorn

Joined: Mar 27, 2007
Posts: 6
Hi Ulf,

Here's an example of an applet posting to a server. In this case, serialized objects are being transferred, but you can send the image data instead.


I gone through that EchoApplet example. it working properly and i use that code in my application. But when i run my application i got an exception.
I am posting the stack trace below.

java.net.UnknownServiceException: protocol doesn't support output
at java.net.URLConnection.getOutputStream(URLConnection.java:682)
at src.WhiteBoardApplet.saveImage(WhiteBoardApplet.java:232)
at src.WhiteBoardApplet.actionPerformed(WhiteBoardApplet.java:204)
at java.awt.Button.processActionEvent(Button.java:382)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


My code :


Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27
Not sure what's going on there, but why are you still persuing this using serialization if the server needs to be IIS? Serialization only works if the server-side uses Java as well.
Padol Girish
Greenhorn

Joined: Mar 27, 2007
Posts: 6
Hi Ulf,
First of all I am sorry that I forgot to mention in my previous post that we are no longer using Windows IIS - that issue is resolved with the client and now we are back on Tomcat5.0

I am posting the Exception here.



Now from the above I came to conclusion that the exception is coming because the BufferedImage class doesn't implement the Serializable interface.

So i am not able to figure how to resolve this situation.
Can you help me with this please.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27
Yes, a BufferedImage can't be serialized. You can use the BufferedImage.getData method to get a Raster object, from which you can obtain an array of pixel values. That array can then be transferred to the server.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save and load JPEG image from disk
 
Similar Threads
text in canvas
general(intermediate)
J2ME
curves and the html5 canvas
Getting Bitmap from Canvas and converting it into image file format