上传图片,对图片进行等比例缩放,及局部裁剪的工具类
- import java.awt.Container;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.RenderingHints;
- import java.awt.Toolkit;
- import java.awt.image.BufferedImage;
- import java.awt.image.CropImageFilter;
- import java.awt.image.FilteredImageSource;
- import java.awt.image.ImageFilter;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.UUID;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGEncodeParam;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- public class FileUploadUtils {
- /**
- * 裁剪图片
- *
- * @param input
- * @param basepath
- * @param uid
- * @param x
- * @param y
- * @param width
- * @param height
- * @return 绝对路径
- * @throws IOException
- */
- public static String cutImg(String input, String basepath, int x, int y,
- int width, int height) throws IOException {
- String path2 = basepath + "/" + ConstantUtils.USERFACETEMPPATH;
- String postfix = getPostfix(input);
- String dst = path2 + "/" + UUID.randomUUID().toString() + "." + postfix;
- createDir(path2);
- imgCut(basepath + input, dst, postfix, x, y, width, height);
- return dst;
- }
- /**
- * 裁剪图片
- *
- * @param input
- * @param src
- * @param x
- * @param y
- * @param width
- * @param height
- * @throws IOException
- */
- public static void imgCut(String input, String dst, String type, int x,
- int y, int width, int height) throws IOException {
- BufferedImage fromimg = ImageIO.read(new File(input));
- ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
- Image img = Toolkit.getDefaultToolkit().createImage(
- new FilteredImageSource(fromimg.getSource(), cropFilter));
- BufferedImage tag = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = tag.getGraphics();
- g.drawImage(img, 0, 0, null); // 绘制小图
- g.dispose();
- // 输出为文件
- // dir = "d:\\temp\\cut_image_" + i + "_" + j + ".jpg";
- File f = new File(dst);
- ImageIO.write(tag, type, f);
- }
- /**
- * 上传头像文件
- *
- * @param src
- * @param basepath
- * @param filename
- * @return
- * @throws Exception
- */
- public static String uploadImg(File src, String basepath, String filename)
- throws Exception {
- String daypath = DateTools.getYear() + "" + DateTools.getMonth() + ""
- + DateTools.getMonthWeek();
- String temppath = ConstantUtils.BASEUPLOADPATH + "/"
- + ConstantUtils.ORIGINALIMGPATH + "/" + daypath;
- String thumbnailpath = ConstantUtils.BASEUPLOADPATH + "/"
- + ConstantUtils.THUMBNAILIMGPATH + "/" + daypath;
- String postfix = getPostfix(filename);
- String uuid = UUID.randomUUID().toString();
- String dsttempname = uuid + "." + postfix;
- createDir(basepath + "/" + temppath);
- createDir(basepath + "/" + thumbnailpath);
- String dstallpath = basepath + "/" + temppath + "/" + dsttempname;
- String dstthumbnailpath = basepath + "/" + thumbnailpath + "/"
- + dsttempname;
- copy(src, new File(dstallpath));
- // 对上传的文件进行 等比例 裁剪。 按照前段要展现的 height width
- Thumbnail(dstallpath, dstthumbnailpath, 350, 300, 100);
- // 返回裁剪后的路径
- return thumbnailpath + "/" + dsttempname;
- }
- /**
- * 上传文件
- *
- * @param src
- * @param dst
- * @throws Exception
- */
- public static void copy(File src, File dst) throws Exception {
- try {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new BufferedInputStream(new FileInputStream(src),
- ConstantUtils.BUFFER_SIZE);
- out = new BufferedOutputStream(new FileOutputStream(dst),