我使用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);
}有没有人对此有任何线索?
谢谢,Abhishek
发布于 2012-09-05 20:29:45
我不知道为什么会发生这种情况,但假设图像中没有其他颜色是黑色的,您可以使用以下方法使黑色透明:
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值的透明图像。
我希望这对你有所帮助。
发布于 2014-12-30 14:40:20
Scalr在创建新的BufferedImage时可能没有使用BufferedImage.TYPE_INT_ARGB --也许你可以要求它使用?
https://stackoverflow.com/questions/12281135
复制相似问题