Talk about the bug that ImageIO used argb to operate jpg.

  images

Order

This article mainly talks about using ImageIO to operate jpg format pictures in BufferedImage.TYPE_INT_ARGB mode to display black bugs.

bug

Recurring instance

public void testArgbOnJpg() throws IOException {
        int width = 400;
        int height = 400;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, width, height);
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        g2.setColor(Color.RED);
        Ellipse2D.Float shape = new Ellipse2D.Float(20, 20, 10, 10);
        g2.fill(shape);

        g2.dispose();
        image.flush();

        ImageIO.write(image,"jpg",new File("out.jpg"));
    }

The output picture can be found on a black screen.

Repair

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

In case of jpg, BufferedImage.TYPE_INT_RGB is used instead to display normally.

doc