简体   繁体   中英

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?

使用TYPE_INT_ARGB代替TYPE_INT_RGB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM