DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   How get Image byte size & transfer Image over Socket? (http://www.daniweb.com/forums/thread62191.html)

Joncamp Nov 20th, 2006 1:13 pm
How get Image byte size & transfer Image over Socket?
 
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

ede Nov 21st, 2006 7:01 am
Re: How get Image byte size & transfer Image over Socket?
 
ı 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);
}

Joncamp Nov 21st, 2006 2:51 pm
Re: How get Image byte size & transfer Image over Socket?
 
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

mifuentesb Dec 21st, 2006 6:21 am
Re: How get Image byte size & transfer Image over Socket?
 
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


sharmila.al Oct 17th, 2007 9:25 am
Re: How get Image byte size & transfer Image over Socket?
 
In client applet i have a code

 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

sharmila.al Oct 19th, 2007 2:56 am
Re: How get Image byte size & transfer Image over Socket?
 
anyone is available to help me:(

masijade Oct 19th, 2007 4:56 am
Re: How get Image byte size & transfer Image over Socket?
 
Why don't you Google "Java File Upload" and send a normal file upload requesst.

sharmila.al Oct 19th, 2007 5:03 am
Re: How get Image byte size & transfer Image over Socket?
 
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

            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

masijade Oct 19th, 2007 5:11 am
Re: How get Image byte size & transfer Image over Socket?
 
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.

sharmila.al Oct 19th, 2007 8:20 am
Re: How get Image byte size & transfer Image over Socket?
 
Thanks for ur reply.

I am new java and servlet.

If u know abt how to store image into server.

Please help me.

thanks


All times are GMT -4. The time now is 10:46 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC