Solved

Image not serializable

Posted on 1998-11-13
5
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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