0

I have an Applet which has an image view placed. I have a 'Connect' button in the Applet. Clicking on this button, is connecting with a java socket program. This is working fine. The socket returns an image data (Bytes format), whenever Applet is connected with it. The image data is coming to the Applet properly without any issues. But, I do not know, how to convert this image data and place as an image in that image view?

Can please help to solve this?

My Apple code is below:

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == connectBtn)
        //Create a socket
    try
    {
        localSocket = new Socket(this.getCodeBase().getHost(), 8080);
        input = localSocket.getInputStream();
        outStream = new PrintStream(localSocket.getOutputStream());

        byte[] data = new byte[0];
        byte[] buffer = new byte[1024];
        int bytesRead;

        try {
            do {

                bytesRead = input.read(buffer);

                byte[] newData = new byte[data.length + bytesRead];

                System.arraycopy(data, 0, newData, 0, data.length);

                System.arraycopy(buffer, 0, newData, data.length, bytesRead);
                // set data equal to newData in prep for next block of data
                data = newData;
            } while (input.available() != 0);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("data length: " + data.length);

        // STEP 1: Convert this "data" to an Image
        // STEP 2: Need to update this image with the existing image. Should I have to repaint?


    }
    catch(UnknownHostException unc)
    {
        System.out.println("Connection why not connected");
    }
    catch(IOException ioe)
    {
        System.out.println(ioe.getMessage());
    }
}
public void paint (final Graphics g)
{
    super.paint(g);
    g.drawString(str, 50, 50);
    sharedImage = getImage(getDocumentBase(), "/Users/ScreenShare/testImage.png");
    g.drawImage(sharedImage, 100, 100, this);
}
Stella
  • 1,728
  • 5
  • 41
  • 95
  • http://stackoverflow.com/questions/18079754/convert-and-display-image-from-byte-array – HectorLector Feb 10 '14 at 10:49
  • I tried this, but it is not working, giving error as "Data array too small (should be > 29999" when trying to create RGB Image at run time. – Stella Feb 10 '14 at 11:00
  • Are you uploading the bytes of the image file, or the bytes of a zip archive? – HectorLector Feb 10 '14 at 11:28
  • bytes of the image file. I just need to convert this byte data into an image and update in g.drawImage(sharedImage, 100, 100, this); – Stella Feb 10 '14 at 11:36

2 Answers2

0

Here is a trivial example how to load an image to an applet:
http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

If you want only the image itself here is another example:
http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

ViToni
  • 882
  • 9
  • 12
  • The second link is storing image file etc. But i want to just convert byte to image and directly display in the image view. I should not store the image anywhere in the system. – Stella Feb 10 '14 at 11:09
  • The relevant part is below "//convert byte array back to BufferedImage". The resulting BufferedImage can be used for "drawImage()" – ViToni Feb 10 '14 at 11:48
  • OK..I tried it, it is giving error like "Error reading PNG image data" when this line "InputStream in = new ByteArrayInputStream(data); BufferedImage bImageFromConvert = ImageIO.read(in)" executed. – Stella Feb 10 '14 at 12:25
  • Now, this is working. But another issue. As per the below code you mentioned in that link, I'm storing it in a folder path for testing. It is giving only 16 kb of image instead of 50 kb data. I am sure that Applet is reading all (50 kb data), as the data length showing it perfect in the log before putting into this BufferedImage bImageFromConvert = ImageIO.read(in). – Stella Feb 10 '14 at 12:32
  • It is working fine. The last issue was, i was converting as jpg, instead of png, since the actual byte is coming for png format. – Stella Feb 10 '14 at 13:01
0

Your array of bytes is b

image = new ImageIcon(b).getImage();

Then set it in your code:

 g.drawImage(image, 100, 100, this);
Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
  • It looks simple..But it gives error as "java.util.zip.ZipException: invalid window size" at this place "sharedImage = new ImageIcon(data).getImage(); repaint(); " Any idea why? – Stella Feb 10 '14 at 11:13