ImageIOがargbを使ってjpgを操作するバグについて話します.

1384 ワード

順序
本論文では主にImageIOを使ってBufferedImage.TYPE_INT_ARGBモードでjpgを操作すると、黒いbugが表示されます.
bug
  • JDK-471277:ImageIO fails to decode YCbCr JPEGs that do not have a JFIF maker
  • JDK-4776576:REG:ImageIO reader produces an incorectイメージwhen read as a RenderedImage
  • 実例をふくげんする
    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"));
        }
    出力された画像のブラックスクリーンが見つかります.
    修復
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    jpgの場合、BufferedImage.TYPE_に変更します.INT_RGB、正常に表示できます.
    doc
  • Colors appar wrong in image file type is TYPEINT_ARGB and writing with ImageIO
  • jpegイメージカラートレッキングchanged after just ImageIO.read()and ImageIO.write()