• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

save and load JPEG image from disk

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Padol Girish
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


java.io.NotSerializableException: java.awt.image.BufferedImage



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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic