ここに来て、コードを提醒します。Javaは写真の圧縮方法を実現します。
昼はちょっと用事があります。食事はちょっと遅いです。暇なまま、コードの断片を収集し続けます。
/**
*
*
* @param oldFile
*
* @param width
*
* @param height
*
* @param uqualityality
* <b> 1.0</b>
* @param smallIcon
* ,
*/
public static void imageZip(File oldFile, File destFile, String format,
int maxWidth, int maxHeight, float uqualityality)
throws IOException {
FileOutputStream out = null;
try {
//
if (!oldFile.exists())
return;
/** */
Image srcFile = ImageIO.read(oldFile);
int new_w = 0, new_h = 0;
//
int h = (int) srcFile.getHeight(null);
//
int w = (int) srcFile.getWidth(null);
//
if ((((double) w) > (double) maxWidth)
|| (((double) h) > (double) maxHeight)) {
//
double rateW = ((double) srcFile.getWidth(null))
/ (double) maxWidth * uqualityality;
double rateH = ((double) srcFile.getHeight(null))
/ (double) maxHeight * uqualityality;
//
double rate;
char zipType;
if (rateW > rateH) {
rate = rateW;
zipType = 'W';
} else {
rate = rateH;
zipType = 'H';
}
new_w = (int) (((double) srcFile.getWidth(null)) / rate);
new_h = (int) (((double) srcFile.getHeight(null)) / rate);
double rate2 = 0;
if (zipType == 'W' && new_h > maxHeight) {
rate = (double) new_h / (double) maxHeight * uqualityality;
} else if (zipType == 'H' && new_w > maxWidth) {
rate = (double) new_w / (double) maxWidth * uqualityality;
}
if (rate2 != 0) {
new_w = (int) (((double) new_w) / rate);
new_h = (int) (((double) new_h) / rate);
System.out.println("2 。");
}
} else {
new_w = w;
new_h = h;
}
if (new_w < 1)
throw new IllegalArgumentException("image width " + new_w
+ " is out of range");
if (new_h < 1)
throw new IllegalArgumentException("image height " + new_h
+ " is out of range");
/** , */
BufferedImage tag = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);
out = new FileOutputStream(destFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** */
jep.setQuality(uqualityality, true);
encoder.encode(tag, jep);
out.close();
srcFile.flush();
} finally {
if (out != null)
out.close();
}
}
ユニットテスト: public static void main(String[] args) {
File oldFile = new File("F:\\temp\\020130726140555MBV2.jpg");
File destFile = new File("F:\\temp\\zipimages\\020130726140555MBV2.jpg");
String format = "jpg";//
int maxWidth = 286;//
int maxHeight = 400;//
float uqualityality = (float) 0.5;//
try {
imageZip(oldFile, destFile, format, maxWidth, maxHeight,
uqualityality);
} catch (IOException e) {
e.printStackTrace();
}
}
本を読む時になって恨むことが少ない。紙のノートがなくなりました。ここで記録できます。もっと採取してください。