How get Image byte size & transfer Image over Socket?

Reply

Join Date: Apr 2006
Posts: 34
Reputation: Joncamp is an unknown quantity at this point 
Solved Threads: 0
Joncamp Joncamp is offline Offline
Light Poster

How get Image byte size & transfer Image over Socket?

 
0
  #1
Nov 20th, 2006
I am trying to transmit an Image object through a Socket connection in Java. The only thing I may be missing is how to get an image size in bytes so I know how many bytes are being transferred over the Socket.

I have the ServerSocket and Client working transferring data, and I have a cut-paste image working from a client Applet, and I have a Java program that saves Images to disk as JPEG/PNG.

How do you get the byte size of an Image object in Java?... or do you know of source code to a program that transferes images through Sockets?

Thanks,
Jon
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: ede is an unknown quantity at this point 
Solved Threads: 2
ede's Avatar
ede ede is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #2
Nov 21st, 2006
ı couldn't understand why you wanna get image byte size. as a result
images are also files. so you can trnsfer you images as a byte streams.
just read image file with an inputstream and write it to socket outputstream.


InputStream input=new FileInputStream("image.png");
byte[] buffer=new byte[1024];
int readData;
while((readData=input.read(buffer))!=-1){
socketOutput.write(buffer,0,readData);
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 34
Reputation: Joncamp is an unknown quantity at this point 
Solved Threads: 0
Joncamp Joncamp is offline Offline
Light Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #3
Nov 21st, 2006
I cant read from a file stream, because the client application is an Applet (browser applet) in this client/server image transrfer program. You can't access files from an Applet in Java.

I can paste an Image into the Applet and cast it to a BufferedImage object, but I don't know how to send the BufferedImage object over a Socket. I need the size of the BufferedImage object so I can notify the server Java application of how many bytes it is to expect on the receiving end.

Jon
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1
Reputation: mifuentesb is an unknown quantity at this point 
Solved Threads: 0
mifuentesb mifuentesb is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #4
Dec 21st, 2006
I've had the same problem as you, and finally my solution was the next one:

BufferedImage bIResult = null;
// Fill your bufferedImage
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean resultWrite = ImageIO.write(bIResult, "PNG", bos);
byte[] imageInBytes = bos.toByteArray();
int length = imageInBytes.length;

Sincerily,

Isabel

Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: sharmila.al is an unknown quantity at this point 
Solved Threads: 0
sharmila.al sharmila.al is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #5
Oct 17th, 2007
In client applet i have a code

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import java.io.*;
  6. import java.net.*;
  7. import javax.imageio.*;
  8. public class test_client extends Applet {
  9. public void init() {
  10. Button b = new Button("submit6");
  11. b.addActionListener(new ActionListener() {
  12. public void actionPerformed(ActionEvent ae) {
  13. showStatus("button pressed");
  14. try {
  15. BufferedImage bi =new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
  16. Graphics2D g2 = bi.createGraphics();
  17. Graphics g = getGraphics();
  18. g.setColor(Color.BLUE);
  19. g.drawLine(100,100,200,200);
  20. g2=(Graphics2D)g;
  21. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  22. ImageIO.write(bi,"JPEG",baos);
  23. byte[] buf = baos.toByteArray();
  24. URL url = new URL(http://10.70.70.1:8080/servlet/send_testclient");
  25. HttpURLConnection con =(HttpURLConnection)url.openConnection();
  26.  
  27. con.setDoInput(true);
  28. con.setDoOutput(true);
  29. con.setRequestMethod("POST");
  30. con.setRequestProperty("Content-Type", "application/octet-stream");
  31. OutputStream os = con.getOutputStream();
  32. os.write(buf);
  33. os.flush();
  34. os.close();
  35. InputStream is = con.getInputStream();
  36. int c;
  37. while ((c = is.read()) != -1)
  38. System.out.write(c);
  39. System.out.println();
  40. is.close();
  41. //os.close();
  42. con.disconnect();
  43. } catch (Exception e) {
  44. showStatus(e.toString());
  45. // System.out.println(e);
  46. }
  47. }
  48. });
  49. add(b);
  50. }
  51. }


I dont know how to get the image data in server side( servlet) and store as jpg pr png file.

please help me regarding this.

i need server side code.

Thanks in advance for helping me
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: sharmila.al is an unknown quantity at this point 
Solved Threads: 0
sharmila.al sharmila.al is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #6
Oct 19th, 2007
anyone is available to help me
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,278
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How get Image byte size & transfer Image over Socket?

 
0
  #7
Oct 19th, 2007
Why don't you Google "Java File Upload" and send a normal file upload requesst.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: sharmila.al is an unknown quantity at this point 
Solved Threads: 0
sharmila.al sharmila.al is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #8
Oct 19th, 2007
In client side I couldnt have image file.

Only I have Byte Array. I need to send to server( servlet) then i ll store it to jpeg file.

I tried the servlet code like this

  1. InputStream in = req.getInputStream();
  2. int count;
  3. byte[] buffer = new byte[8192];
  4. FileOutputStream fis = new FileOutputStream("new.jpg");
  5. while ((count = in.read(buffer)) > 0){
  6. fis.write(buffer, 0, count);
  7. fis.flush();
  8. }
  9. fis.close();

But i didnt find any file name as new.jpg in my server.

Please help me
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,278
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How get Image byte size & transfer Image over Socket?

 
0
  #9
Oct 19th, 2007
Do what I suggested in the post. On a fileupload the server gets an inputstream. How that inputstream originates doesn't matter. Investigate "File Upload" streams. Both how to receive one in a Servlet, and how to initiate one using an HttpUrlConnection, and your problem is solved.
Last edited by masijade; Oct 19th, 2007 at 5:11 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: sharmila.al is an unknown quantity at this point 
Solved Threads: 0
sharmila.al sharmila.al is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #10
Oct 19th, 2007
Thanks for ur reply.

I am new java and servlet.

If u know abt how to store image into server.

Please help me.

thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC