我正在尝试创建徽标调整大小,该大小也可以根据用户输入将徽标背景的颜色更改为白色或透明。
这可以将图像的背景颜色从透明变成白色:
BufferedImage inputImage = ImageIO.read(new File(inputImagePath)); //reads the input image
BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.TYPE_INT_ARGB); //creates the output image
Graphics2D image = outputImage.createGraphics();
image.setBackground(Color.WHITE); //sets background color to white
image.fillRect(0, 0, newWidth, newHeight);
image.drawImage(inputImage, 0, 0, newWidth, newHeight, null);
image.dispose();
String formatName = outputImagePath.substring(outputImagePath.lastIndexOf(".") + 1); //Finds what type of image it is
ImageIO.write(outputImage, formatName, new File(outputImagePath)); //Creates the output image file in correct path
}
我想如果将其更改为此,它将图像的背景颜色从白色更改为透明,但保持白色。有帮助吗?
BufferedImage inputImage = ImageIO.read(new File(inputImagePath)); //reads the input image
BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.TYPE_INT_ARGB); //creates the output image
Graphics2D image = outputImage.createGraphics(); //Draws the actual output image
image.setComposite(AlphaComposite.Clear);
image.fillRect(0, 0, newWidth, newHeight);
image.setComposite(AlphaComposite.Src);
image.drawImage(inputImage, 0, 0, newWidth, newHeight, null); //Draws the actual output image
image.dispose();
String formatName = outputImagePath.substring(outputImagePath.lastIndexOf(".") + 1); //Finds what type of image it is
ImageIO.write(outputImage, formatName, new File(outputImagePath)); //Creates the output image file in correct path
}
我现在正在考虑可能需要跟踪徽标并为其赋予一个全新的背景,但这可能非常困难。有什么建议吗?