我正在使用 org.imgscalr.Scalr(http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/#solve) 库来调整图像大小,但具有透明背景的 PNG 变成黑色调整大小时。我的代码如下:
public void scaleImage(String originalImage, int width, int height) throws IOException {
File imgFl = new File(originalImage);
BufferedImage originalBufImage = ImageIO.read(imgFl);
BufferedImage scaledImage = Scalr.resize(originalBufImage, Scalr.Method.ULTRA_QUALITY, width, height, Scalr.OP_BRIGHTER);
File outputfile = new File(originalImage);
String fileExtension = originalImage.substring(originalImage.indexOf(".") + 1, originalImage.length());
ImageIO.write(scaledImage, fileExtension, outputfile);
}
有人知道这件事吗?
谢谢阿布舍克
最佳答案
我不知道为什么会这样,但假设图像中没有其他东西是黑色的,你可以用这种方法使黑色透明:
private static Image mct(Image im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
}
else {
// nothing to do
return rgb;
}
}
};
它返回一个图像,所有 RGB 值都匹配透明的颜色。
希望对您有所帮助。
关于java - 使用 Scale 调整大小时,具有透明背景的 PNG 变为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12281135/