?
Solved

Send a image using streamconnection.

Posted on 2005-05-05
17
Medium Priority
?
513 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Knightley
  • 9
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13937906
You can write a png to a stream thus:

javax.image.ImageIO.write(bufferedImage, "PNG", out);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13937912
Sorry that should have been

javax.imageio.ImageIO.write(bufferedImage, "PNG", out);
0
 
LVL 1

Author Comment

by:Knightley
ID: 13941106
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 13943338
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
 
LVL 1

Author Comment

by:Knightley
ID: 13947881
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13948482
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
 
LVL 1

Author Comment

by:Knightley
ID: 13949129
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13950317
>> 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
 
LVL 1

Author Comment

by:Knightley
ID: 13951240
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
 
LVL 1

Author Comment

by:Knightley
ID: 13951252
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13951621
>>am i sending a byte Array???

Yes, encoded in PNG format
0
 
LVL 1

Author Comment

by:Knightley
ID: 13951993
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13952021
You'd read the stream to eof - perhaps into a ByteArrayOutputStream
0
 
LVL 1

Author Comment

by:Knightley
ID: 13952138
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 180 total points
ID: 13953369
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
 
LVL 1

Author Comment

by:Knightley
ID: 13956972
works out, thanx very much, CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13958102
:-)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

571 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