修改图片大小的一个示例

public class AlterUploadImage {
String inputDir; //源图片的绝对路径,不包含图片的文件名
String outputDir; //目标图片的绝对路径,不包含图片的文件名
String inputFileName; //源图片文件名
String outputFileName; //目标图片的文件名
int outputWidth; //目标图片的宽
int outputHeight; //目标图片的高
boolean proportion = true; //是否等比例缩放
private String inputFileUrl;//源图片的绝对路径,包含文件名
private String outputFileUrl;//目标图片的绝对路径,包含文件名

public AlterUploadImage(String inputDir, String outputDir,
String inputFileName, String outputFileName, int outputWidth,
int outputHeight, boolean proportion) {
this.inputDir=inputDir;
this.outputDir=outputDir;
this.inputFileName= inputFileName;
this.outputFileName=outputFileName;
this.outputWidth=outputWidth;
this.outputHeight=outputHeight;
this.proportion=proportion;
}

public AlterUploadImage() {
inputDir="";
outputDir="";
inputFileName="";
outputFileName="";
outputWidth=100;
outputHeight=100;
proportion=true;
}

public boolean alterImageSize() {
//构造源、目标图片的路径
inputFileUrl=inputDir+inputFileName;
outputFileUrl=outputDir+outputFileName;
File fileOut = new File(outputFileUrl);
File fileIn = new File(inputFileUrl);
FileOutputStream tempout = null;
try {
tempout = new FileOutputStream(fileOut);
} catch (Exception ex) {
System.out.println(ex.toString());
}
Image img = null;
Toolkit tk = new Toolkit();
Applet app = new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img = javax.imageio.ImageIO.read(fileIn);
mt.addImage(img, 0);
mt.waitForID(0);
} catch (Exception e) {
e.printStackTrace();
}
if (img.getWidth(null) == -1) {
return false;
} else {
int new_w;
int new_h;
if (this.proportion == true) {//判断是否等比例缩放
//计算比率
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
new_w = (int) (((double) img.getWidth(null)) / rate);
new_h = (int) (((double) img.getHeight(null)) / rate);
} else {
new_w = outputWidth; //宽
new_h = outputHeight; //高
}
BufferedImage buffImg = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);

Graphics g = buffImg.createGraphics();

g.setColor(Color.white);
g.fillRect(0, 0, new_w, new_h);

g.drawImage(img, 0, 0, new_w, new_h, null);
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
try {
encoder.encode(buffImg);
tempout.close();
} catch (IOException ex) {
System.out.println(ex.toString());
}
}
return true;
}

public String getInputDir() {
return inputDir;
}

public void setInputDir(String inputDir) {
this.inputDir = inputDir;
}

public String getOutputDir() {
return outputDir;
}

public void setOutputDir(String outputDir) {
this.outputDir = outputDir;
}

public String getInputFileName() {
return inputFileName;
}

public void setInputFileName(String inputFileName) {
this.inputFileName = inputFileName;
}

public String getOutputFileName() {
return outputFileName;
}

public void setOutputFileName(String outputFileName) {
this.outputFileName = outputFileName;
}

public int getOutputWidth() {
return outputWidth;
}

public void setOutputWidth(int outputWidth) {
this.outputWidth = outputWidth;
}

public int getOutputHeight() {
return outputHeight;
}

public void setOutputHeight(int outputHeight) {
this.outputHeight = outputHeight;
}

public boolean isProportion() {
return proportion;
}

public void setProportion(boolean proportion) {
this.proportion = proportion;
}

public String getInputFileUrl() {
return inputFileUrl;
}

public void setInputFileUrl(String inputFileUrl) {
this.inputFileUrl = inputFileUrl;
}

public String getOutputFileUrl() {
return outputFileUrl;
}

public void setOutputFileUrl(String outputFileUrl) {
this.outputFileUrl = outputFileUrl;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值