本文主要來聊一下使用ImageIO在BufferedImage.TYPE_INT_ARGB模式下操做jpg格式圖片顯示黑色的bug。java
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")); }
能夠發現輸出的圖片黑屏。app
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
jpg狀況下,改成使用BufferedImage.TYPE_INT_RGB,便可正常顯示。code