Generated styles
When writing this, I encountered a problem: the generated image was covered with a layer of red,
The solution is to replace the ImageIO.read method with Toolkit.getDefaultToolkit().getImage
First, Image src=Toolkit.getDefaultToolkit().getImage(file.getPath()); BufferedImage image=BufferedImageBuilder.toBufferedImage(src);//Image to BufferedImage //Two. Image imageTookit = Toolkit.getDefaultToolkit().createImage(bytes); BufferedImage cutImage = BufferedImageBuilder.toBufferedImage(imageTookit); BufferedImageBuilder Source code public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } // This code ensures that all the pixels in the image are loaded image = new ImageIcon(image).getImage(); BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); try { int transparency = Transparency.OPAQUE; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { // The system does not have a screen } if (bimage == null) { // Create a buffered image using the default color model int type = BufferedImage.TYPE_INT_RGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
But it didn't work after I replaced it here,
My image is red because the background image is set with transparency. It may be because the jpg does not support transparent color that the image is covered with red
1, ImageIO.write(bg, "png", new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\image\\\\1a.jpg")); //Normal output 2, ImageIO.write(bg, "jpg", new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\image\\\\1a.jpg")); //Output red picture
The source code address is in the QR code in the figure above. Scan the code and send it to "mingpian" to get it