0

I'm using Java Advanced Imaging to read and write JPEG2000 images in java. When debugging in Intellij, I can view the BufferedImage and see that it's transparent. So I can stack layers on top of each other. But when I write it to a file, the background is always black. Is it something I'm doing wrong? Or is it the writer that's the problem? Do I need to be doing something specific with the J2KImageWriteParam regarding encoding etc? Unfortunately I'm fairly new to handling images programmatically, and there is little to no information or support for jpeg2000 that I can find.

BufferedImage image = ImageIO.read(new File("image.jp2"));
BufferedImage tmpImage = new BufferedImage(
    image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE
);
Graphics2D g = (Graphics2D) tmpImage.getGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setComposite(AlphaComposite.Src);
g.drawImage(image, 0, 0, null);
ImageIO.write(tmpImage, "jpeg2000", new File("outputImage.jp2"));
g.dispose();
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
jjjjj
  • 121
  • 4
  • 11
  • Do you need the file format to be JPEG2000? The JPEG2000 format does allow for alpha channel, but I'm not sure if the JAI ImageIO plugin supports it. Try using a different format, like PNG or TIFF, to see if that works. – Harald K Sep 09 '16 at 09:29
  • 1
    Yes, unfortunately it does need to JPEG2000. PNG, TIFF, GIF all work just fine. It seems from all the testing I've done, that the JAI writer writes the alpha channel in a way that only the JAI reader understands. All of the viewers I've used just see the black background. But when I read in a file I've written with a test program, the image shows transparent in the debugger. – jjjjj Sep 09 '16 at 13:36

0 Answers0