beek
beek

Reputation: 3750

BufferedImage with Transparent PNG

I'm uploading a PNG with a transparent background to a Java server using the below code

        byte[] imageData = Base64.decodeBase64(encodedImage);

        ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
        BufferedImage bufferedImage = ImageIO.read(bais);
        BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
                bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.OPAQUE, null);

        Scene scene = sceneService.getScene(sceneId);

        java.io.File file = new java.io.File(Constants.TEMP_DIR_PATH
                +  UUID.randomUUID().toString() +".png");

        ImageIO.write(newBufferedImage, "PNG", file);

I can't seem to set the background as transparent, it has to have a color?

Anyway to have a transparent background?

Upvotes: 2

Views: 3885

Answers (1)

Maurice Perry
Maurice Perry

Reputation: 9650

Use TYPE_INT_ARGB instead of TYPE_INT_RGB

Upvotes: 4

Related Questions