| | |
How get Image byte size & transfer Image over Socket?
![]() |
•
•
Join Date: Apr 2006
Posts: 34
Reputation:
Solved Threads: 0
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
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
ı 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);
}
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);
}
•
•
Join Date: Apr 2006
Posts: 34
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2006
Posts: 1
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
In client applet i have a code
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
Java Syntax (Toggle Plain Text)
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import java.net.*; import javax.imageio.*; public class test_client extends Applet { public void init() { Button b = new Button("submit6"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { showStatus("button pressed"); try { BufferedImage bi =new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bi.createGraphics(); Graphics g = getGraphics(); g.setColor(Color.BLUE); g.drawLine(100,100,200,200); g2=(Graphics2D)g; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi,"JPEG",baos); byte[] buf = baos.toByteArray(); URL url = new URL(http://10.70.70.1:8080/servlet/send_testclient"); HttpURLConnection con =(HttpURLConnection)url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/octet-stream"); OutputStream os = con.getOutputStream(); os.write(buf); os.flush(); os.close(); InputStream is = con.getInputStream(); int c; while ((c = is.read()) != -1) System.out.write(c); System.out.println(); is.close(); //os.close(); con.disconnect(); } catch (Exception e) { showStatus(e.toString()); // System.out.println(e); } } }); add(b); } }
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
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
----------------------------------------------
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
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
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
But i didnt find any file name as new.jpg in my server.
Please help me
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
Java Syntax (Toggle Plain Text)
InputStream in = req.getInputStream(); int count; byte[] buffer = new byte[8192]; FileOutputStream fis = new FileOutputStream("new.jpg"); while ((count = in.read(buffer)) > 0){ fis.write(buffer, 0, count); fis.flush(); } fis.close();
But i didnt find any file name as new.jpg in my server.
Please help me
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
----------------------------------------------
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
![]() |
Similar Threads
- Image in DataGridView (C#)
- Image read and write operations in turbo C (C)
- Working with SQL server's Image data type (ASP.NET)
- How to reduce the size of an image to a standard size say 40 kb (C#)
- Padding and margins (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: Using a method of an object - cannot find variable
- Next Thread: Need Help!
| Thread Tools | Search this Thread |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework homework html if_statement image integer j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keytool keyword linux list macintosh method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql sqlite swing terminal testautomation threads transforms tree ui unicode validation web windows






