/**
* 图片基本操作
* @author
*/
public class image_operation_format {
/**
* 缩小并转换格式
*
* @param srcPath
* @param destPath
* @param height
* @param width
* @param formate
* @return
*/
public boolean imageformat(String srcPath, String destPath, int height, int width, String formate) {
boolean flag = false;
try {
File file = new File(srcPath);
File destFile = new File(destPath);
//校验文件是否存在
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdir();
}
// 读入文件
BufferedImage src = ImageIO.read(file);
Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
// 绘制缩小后的图
g.drawImage(image, 0, 0, null);
g.dispose();
// 输出到文件流
flag = ImageIO.write(bufferedImage, formate, new FileOutputStream(destFile));
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static void main(String[] args) {
try {
image_operation_format iof = new image_operation_format();
boolean flag = iof.imageformat("E:\\potos\\原文件\\11.jpg", "E:\\potos\\新文件\\11.png", 400, 400, "png");
System.out.println(flag);
} catch (Exception e) {
System.out.println(e);
}
}
}
图片转化操作
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。