Solved

Image not serializable

Posted on 1998-11-13
5
247 Views
Last Modified: 2012-06-22
May I know how can I grap the image in a canvas a send it to a client and based on the object received, the client will paint the image back to a canvas...

I've tried the writeObject function but it is not able to write an Image object coz Image object does not implements java.io.Serializable
0
Comment
Question by:ctusa
  • 3
5 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
Comment Utility
You can use the PixelGrabber class from the java.awt.image package to get an integer array that represents the image. Use the constructor that takes an Image argument:

public PixelGrabber(Image img,int x,int y,int w,int h,int []pix,int off,int scansize);

The production of pixel data is initiated with the command:

public boolean grabPixels() throws InterruptedException;

from the same class.
You can break this down into bytes, send it across, and on the other end it can be reassembled with a Toolkit.createImage call.

0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
P.S. there is a createImage method in the Toolkit that takes a byte array as an argument.

0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
P.P.S. scansize can simply be set to the width of the image in pixels.

0
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
you have to trap the Image pixel data into a byte array, then (eventually) encode it, than send it
, decode it and show it

so very quickly
Image image = new Image (....mycanvas.getSize())  - creaty new image with the size of canvas
mycanvas.paint(image.getGraphics())  - trap the canvas image into an offscreen image

PixelGrabber pg = new PixelGrabber(imgage, x, y, w, h, pixels, 0, w); - to grab pixels from the image
(see java.awt.image.PixelGrabber  docs)
than you can
1. code the byte array as a Gif file and use Toolkit.CreateImage(bytes) on the client computer
2. send the raw data and implement ImageProducer that creates Image from the raw data

I have no time to write you sample code at moment, so if you have more specific questions please post them here, i'll try to answer (an post code) during the weekend ...
Also can give you the URL of free GIF encoder (byte[] -> gifData) with sources if you need it

hope this helps
  heyhey

0
 

Author Comment

by:ctusa
Comment Utility
Thanx, but I noticed that the conversion of int[] to byte[] is very very slow... is there a faster way...? That's b4 sending across the network...juz local processing power only...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now