Link to home
Start Free TrialLog in
Avatar of Knightley
Knightley

asked on

Send a image using streamconnection.

i have captured a image like:
BufferedImage image = new Robot().createScreenCapture(new Rectangle(0,0,400,400));

and would like to send it with DataOutputStream to send it and then then receive it
on my mobile phone (midp) with DataIutputStream.

Since midp could read png format image file. I wonder if there is a way to compress
my BufferedImage to png format before i send it. And then read it on my phone???

thanx in advance
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can write a png to a stream thus:

javax.image.ImageIO.write(bufferedImage, "PNG", out);
Sorry that should have been

javax.imageio.ImageIO.write(bufferedImage, "PNG", out);
Avatar of Knightley
Knightley

ASKER

I am using the following code to send data through my bluetooth device:

OR_conn = (StreamConnection) Connector.open(OR_conn_string);
OR_output = new DataOutputStream(new BufferedOutputStream(OR_conn.openOutputStream()));
OR_output.writeInt() etc.

How should i integrate your code into mine???

And could you also give me some code about receiving the png data with DataInputStream

thanx.
The problem is that is don't do J2ME as i think i've said before, although if you can point me to online docs for the api you're using it's possible that i could help anyway, but not otherwise
Thanx very much for your offer, CEHJ. Really appreciate it. But i think and hope you
do not need to read any doc.

I have checked in j2me lib after reading your first anwser.
>>javax.imageio.ImageIO.write(bufferedImage, "PNG", out);
Obviously, javax.imageio.ImageIO is also supported in MIDP.

Since i use DataStreamOutput/Iput to send the data
>>OR_conn = (StreamConnection) Connector.open(OR_conn_string);
>>OR_output = new DataOutputStream(new BufferedOutputStream(OR_conn.openOutputStream()));

What i am trying to do is:
1. write BufferedImage into PNG format. [javax.imageio.ImageIO.write(bufferedImage, "PNG", out);]
2. send it with DataOutputStream;
....
3. receive with DataInputStream;
4. Decode Input with javax.imageio.ImageIO.read to BufferedImage again.

5. Do something on my mobile phone... ...

I think step 1-4 are not J2ME programming, though step 3 and 4 are executed
on my phone, but they are just standard java coding.

So i hope you could help me. Or am i missing something?

thanx again.
Well 1&2 and 3&4 can be collapsed into one operation. If you've got a DataOutputStream, you can let 'out' stand for that
you mean like that:

javax.imageio.ImageIO.write(bufferedImage, "PNG", OR_output);

and what code do i need for reading the stream on my phone?
>> you mean like that:

Yes. If you're not sending any other data, you can get rid of the DataOutputStream and just use the BufferedOutputStream

>>and what code do i need for reading the stream on my phone?

BufferedImage bi = javax.imageio.ImageIO.read(in);

(if it can use ImageIO)
ohhh sorry, i was wrong, midp does not support ImageIO.

But it could read a "PNG" byteArray and render it.

So with:

javax.imageio.ImageIO.write(bufferedImage, "PNG", OR_output);

am i sending a byte Array???
ohhh sorry, i was wrong, midp does not support ImageIO.

But it could read a "PNG" byteArray and render it.

So i could turn a image or bufferedimage into png and send it
using:

output.writeByte():

By using:

javax.imageio.ImageIO.write(bufferedImage, "PNG", OR_output);

am i sending a byte Array???
>>am i sending a byte Array???

Yes, encoded in PNG format
Could i simply using

for(int i=0; i<ByteArray_length; i++){
input.readByte()
}

to read this byte array?

But how could i know how long this Byte array is?
You'd read the stream to eof - perhaps into a ByteArrayOutputStream
could i somehow use
javax.imageio.ImageIO.write(bufferedImage, "PNG", ...

to save the the png FISRT into a ByteArray, and then

send it per DataOutputStream.writeByte()?

Then it is easier to read it as in my last post?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
works out, thanx very much, CEHJ
:-)