Solved

Image not serializable

Posted on 1998-11-13
5
257 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
ID: 1227407
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
ID: 1227408
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
ID: 1227409
P.P.S. scansize can simply be set to the width of the image in pixels.

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227410
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
ID: 1227411
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 64
javap bin 2 29
object oriented programming comparison 5 56
arguments to jar 5 15
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

867 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

17 Experts available now in Live!

Get 1:1 Help Now