代码实现
BufferedImage sourceImage = ImageIO.read(imageFile);
int height = sourceImage.getHeight();
int width = sourceImage.getWidth();
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = newImage.createGraphics();
g2d.drawImage(sourceImage, 0, 0, null);
int alpha = 0;
for (int y = newImage.getMinY(); y < newImage.getHeight(); y++) {
for (int x = newImage.getMinX(); x < newImage.getWidth(); x++) {
int rgb = newImage.getRGB(x, y);
alpha = includeColor(rgb) ? 0 : 255;
rgb = (alpha << 24) | (rgb & 0X00FFFFFF);
newImage.setRGB(x, y, rgb);
}
}
g2d.dispose();
ByteArrayOutputStream imageOutPutStream = new ByteArrayOutputStream();
ImageIO.write(newImage, "png", imageOutPutStream);
/**
* 判断当前色块是否属于设定值的范围
* @param color 当前颜色
* @return false|true
*/
public static boolean includeColor(int color) {
int red = (color & 0xFF0000) >> 16;
int green = (color & 0x00FF00) >> 8;
int blue = (color & 0x0000FF);
int color_range = 215;
return (red >= color_range && green >= color_range && blue >= color_range);
}
``
标签:Java,int,背景,BufferedImage,color,range,sourceImage,newImage,PNG
From: https://www.cnblogs.com/IamHzc/p/17532979.html