java图片转换格式并设定大小

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 支持转换成BMP, JPG,PNG, JPEG, WBMP, GIF格式
*
*/
public class ImageConvertJPEG {
private String inPath; //输入路径
private String outPath; //输出路径,不包含文件名,如C:\\Users\\Administrator\\Desktop或C:/Users/Administrator/Desktop
private String outFileName; //输出文件名,如myPicture
private int width=200; //转换后的图片宽度
private int heigth=200; //轮换后的图片高度

public void beginConvert() throws IOException {
File inFile
= new File(inPath);
if (!inFile.exists() || inFile.isDirectory()) {
throw new IOException("文件不存在!");
}
File outFile
= new File(outPath + "/" + outFileName + ".jpeg");
try {
BufferedImage bufferedImage
= ImageIO.read(inFile);
BufferedImage bufferedChange
=new BufferedImage(width, heigth, BufferedImage.TYPE_INT_BGR);
Graphics2D g
= (Graphics2D) bufferedChange.getGraphics();
g.drawImage(bufferedImage,
0, 0, 256, 256, null);
g.dispose();
bufferedChange.flush();
ImageIO.write(bufferedChange,
"JPEG", outFile);
}
catch (Exception e) {
throw new IOException("输入文件不是允许图片!");
}
}
public ImageConvertJPEG() {
}

public ImageConvertJPEG(String inPath, String outPath, String outFileName) {
this.inPath = inPath;
this.outPath = outPath;
this.outFileName = outFileName;
}
}

转载于:https://www.cnblogs.com/live365wang/archive/2011/08/23/2150131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值