在Java中,图形使用BuffereImage生成缩略图,无需反转颜色和增益alpha
我使用“BuffereImage”生成缩略图
try {
BufferedImage bi = new BufferedImage(thumWidth, thumHeight, TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
Image ii = (new ImageIcon(orgFile.getAbsolutePath())).getImage();
g.drawImage(ii, 0, 0, thumWidth, thumHeight, null);
String thumbFileDir = prefixPath + "/" + thumWidth + "/" + afterPath;
File file = this.createPathIfnotexist(thumbFileDir);
String fullPathToSave = this.genPath(file.getAbsolutePath(), fileName);
File thumbFile = new File(fullPathToSave);
ImageIO.write(bi, ext, thumbFile);
} catch (IOException var22) {
var22.printStackTrace();
return;
} catch (Exception var23) {
var23.printStackTrace();
}
我的问题是
当我使用
TYPE_INT_RGB
获得BuffereImage实例时,在发送PNG文件时丢失alpha,在发送JPG文件时也可以Original,Converted当我使用
TYPE_INT_ARGB
获取BuffereImage实例时,在发送PNG文件时获得alpha,但在发送JPG文件时颜色相反Original,Converted
所以,我想在不反转颜色的情况下创建缩略图,并保持alpha。我该怎么办
# 1 楼答案
作为我持续研究的结果,我认为使用外部库比按照问题中建议的方式尝试更方便
因此,我决定使用java-image-scaling生成缩略图
对于将来访问此页面的人,我会留下一些代码。(实际上,这个问题是用Java编写的,但答案是用Kotlin编写的。)
imgLocation
是原始图像的上传路径,width
是参考点,例如100、240、480、720、1080