同过百度网上现有代码,改造出了自己想要的代码
实现功能为填充图片到指定大小 填充部分为透明 png
public class Test {
/*** 填充图片为png格式,填充部分为透明色
* @param srcImage 源文件
* @param descFile 目标路径
* @param suffix_src 文件类型
* @param destWidth 设置图片宽度
* @param destHight 设置图片高度
* @return
*/
public static boolean reduceImage_scale(final BufferedImage srcImage,
File descFile, String suffix_src, int destWidth, int destHight) {
BufferedImage outImage = null;
try {
outImage = new BufferedImage(destWidth, destHight,
BufferedImage.TYPE_INT_RGB);// 2.jpg
Graphics2D graphics2D = outImage.createGraphics();
outImage = graphics2D.getDeviceConfiguration().createCompatibleImage(destWidth, destHight, Transparency.TRANSLUCENT);
graphics2D.dispose();
graphics2D = outImage.createGraphics();
int oldheight = srcImage.getHeight();
int oldwidth = srcImage.getWidth();
// 设置图片居中显示
graphics2D.drawImage(srcImage, (destWidth - oldwidth) / 2,
(destHight - oldheight) / 2, null);
ImageIO.write(outImage, suffix_src, descFile);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
// 方法调用
public static void main(final String[] args) {
BufferedImage srcImage;
try {
srcImage = ImageIO.read(new File("c:/aa.jpg"));
reduceImage_scale(srcImage, new File("c:/2.png"), "png", 388, 388);
} catch (IOException e) {
e.printStackTrace();
}
}
}