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
LVL 1
KnightleyAsked:
Who is Participating?
 
CEHJCommented:
You could yes:

ByteArrayOutputStream bout = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(bufferedImage, "PNG", bout);
dataOutputStream.write(bout.toByteArray());

but it wouldn't be any easier to read and is certainly harder to write
0
 
CEHJCommented:
You can write a png to a stream thus:

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

javax.imageio.ImageIO.write(bufferedImage, "PNG", out);
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
KnightleyAuthor Commented:
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.
0
 
CEHJCommented:
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
0
 
KnightleyAuthor Commented:
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.
0
 
CEHJCommented:
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
0
 
KnightleyAuthor Commented:
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?
0
 
CEHJCommented:
>> 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)
0
 
KnightleyAuthor Commented:
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???
0
 
KnightleyAuthor Commented:
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???
0
 
CEHJCommented:
>>am i sending a byte Array???

Yes, encoded in PNG format
0
 
KnightleyAuthor Commented:
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?
0
 
CEHJCommented:
You'd read the stream to eof - perhaps into a ByteArrayOutputStream
0
 
KnightleyAuthor Commented:
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?
0
 
KnightleyAuthor Commented:
works out, thanx very much, CEHJ
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.