JAVA 将图片生成缩略图


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.File;


public class Test {


public static void main(String[] args) {
try {
// 参数1(from),参数2(to),参数3(宽),参数4(高)
Test.saveImageAsJpg("D:/usr/1.jpg", "D:/usr1/1.jpg", 100, 100);
} catch (Exception e) {
e.printStackTrace();
}
}


public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
// 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
// 则将下面的if else语句注释即可
if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) {
// handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else {
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
}
return target;
}


public static void saveImageAsJpg(String fromFileStr, String saveToFileStr, int width, int hight) throws Exception {
BufferedImage srcImage;
// String imgType = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPG";
// if (fromFileStr.toLowerCase().endsWith(".png")) {
// imgType = "PNG";
// }
File saveFile = new File(saveToFileStr);
File fromFile = new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if (width > 0 || hight > 0) {
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);
System.out.println("ok");
}

}





import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
 * Out logo.
 * 生成缩略图
 */
public  void outLogo_backup(File file, String url, OutputStream out, int dwidth, int dheight) throws IOException {
//把得到的文件以字节形式转成输入流
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file), 4096);
//读取转成输入流的图片
Image src =ImageIO.read(stream);
//获取图片原宽
int width = src.getWidth(null);
//获取图片原高
int height = src.getHeight(null);
//缩略图所需高宽
int towidth, toheight;
//判断原宽高是否大于所得到现有需要显示图片宽高
if (width > dwidth || height > dheight) {//原图宽高大于缩略宽高
if (((float) width / dwidth) >= ((float) height / dheight)) {//当宽大于高时
//生成新高宽
towidth = dwidth;
toheight = (height * dwidth) / width;
} else {//当高大于宽时
//生成新高宽
toheight = dheight;
towidth = (width * dheight) / height;
}
} else {//不大于
towidth = width;
toheight = height;
}
//创建一个不带透明色的BufferedImage对象
BufferedImage tag = new BufferedImage(towidth, toheight, BufferedImage.TYPE_INT_RGB);
//新生成结果图片
tag.getGraphics().drawImage(src, 0, 0, towidth, toheight, null);
//以jpeg格式输入新图片
ImageIO.write(tag, "jpeg", out);
stream.close();
out.flush();
out.close();

//判断文件路径
if (url != null) {
//写到文件里去
log.debug("generate small image url  {}", url);
File parentPath = new File(url.substring(0, url.lastIndexOf("/")));
if (!parentPath.exists()) {
parentPath.mkdirs();
}
FileOutputStream smallImage = null;
try {
smallImage = new FileOutputStream(url); // 输出到文件流
//将文件保存的路径下指定文件
ImageIO.write(tag, "jpeg", smallImage);
} catch (Exception e) {
log.error("Generate small image error",e);
} finally {
if (smallImage != null)
//关闭流
smallImage.close();
}
}
src = null;
}



你可能感兴趣的:(工作小例)