Note that there are some explanatory texts on larger screens.

plurals
  1. POByteArrayOutput/InputStream on transparent image
    primarykey
    data
    text
    <p>I'm trying to send a <code>BufferedImage</code> over socket, I do this by converting the image to <code>byte[]</code> and then send it over after encoding it in Base64. I'm sending over 2 BufferedImages, one of them is "full", the other one is about 50% transparent. The problem I'm having, is that when they arrive, the second image is still visually transparent, but when I get the data array via Raster, it has been changed.</p> <p>I made a small test code to demonstrate the problem;</p> <pre><code> BufferedImage levelBufferedOriginal = ... BufferedImage backgroundBufferedOriginal = ... byte[] levelDataOriginal = ((DataBufferByte) levelBufferedOriginal.getRaster().getDataBuffer()).getData(); byte[] backgroundDataOriginal = ((DataBufferByte) backgroundBufferedOriginal.getRaster().getDataBuffer()).getData(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] temp = null, temp2=null; try { ImageIO.write(levelBufferedOriginal, "png", baos); baos.flush(); temp = baos.toByteArray(); baos.close(); baos=new ByteArrayOutputStream(); ImageIO.write(backgroundBufferedOriginal, "png", baos); baos.flush(); temp2 = baos.toByteArray(); baos.close(); } catch (IOException e1) { e1.printStackTrace(); } BufferedImage levelBufferedNew = null; BufferedImage backgroundBufferedNew = null; try { levelBufferedNew = ImageIO.read(new ByteArrayInputStream(temp)); backgroundBufferedNew = ImageIO.read(new ByteArrayInputStream(temp2)); } catch (IOException e) { e.printStackTrace(); } byte[] levelDataNew = ((DataBufferByte) levelBufferedNew.getRaster().getDataBuffer()).getData(); byte[] backgroundDataNew = ((DataBufferByte) backgroundBufferedNew.getRaster().getDataBuffer()).getData(); System.out.println("LEVEL: " + Arrays.equals(levelDataOriginal, levelDataNew)); System.out.println("BACKGROUND: " + Arrays.equals(backgroundDataOriginal, backgroundDataNew)); </code></pre> <p>All I do here, is simply transform the <code>BufferedImage</code> to <code>byte[]</code>, then back, and compare the data I get from <code>DataBufferByte</code>. The output is</p> <blockquote> <p>LEVEL: false </p> <p>BACKGROUND: true</p> </blockquote> <p>Background is the "full" image, and Level is the one with some transparent pixels.</p> <p>If the general idea is wrong, I would like to hear another, all I want is to be able to exactly recreate 2 bufferedImages.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload