Java中图片处理工具类,含等比缩放、图片裁剪



上传图片,对图片进行等比例缩放,及局部裁剪的工具类

[java]  view plain copy
  1. import java.awt.Container;  
  2. import java.awt.Graphics;  
  3. import java.awt.Graphics2D;  
  4. import java.awt.Image;  
  5. import java.awt.MediaTracker;  
  6. import java.awt.RenderingHints;  
  7. import java.awt.Toolkit;  
  8. import java.awt.image.BufferedImage;  
  9. import java.awt.image.CropImageFilter;  
  10. import java.awt.image.FilteredImageSource;  
  11. import java.awt.image.ImageFilter;  
  12. import java.io.BufferedInputStream;  
  13. import java.io.BufferedOutputStream;  
  14. import java.io.File;  
  15. import java.io.FileInputStream;  
  16. import java.io.FileOutputStream;  
  17. import java.io.IOException;  
  18. import java.io.InputStream;  
  19. import java.io.OutputStream;  
  20. import java.util.UUID;  
  21.   
  22. import javax.imageio.ImageIO;  
  23.   
  24. import com.sun.image.codec.jpeg.JPEGCodec;  
  25. import com.sun.image.codec.jpeg.JPEGEncodeParam;  
  26. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  27.   
  28. public class FileUploadUtils {  
  29.   
  30.     /** 
  31.      * 裁剪图片 
  32.      *  
  33.      * @param input 
  34.      * @param basepath 
  35.      * @param uid 
  36.      * @param x 
  37.      * @param y 
  38.      * @param width 
  39.      * @param height 
  40.      * @return 绝对路径 
  41.      * @throws IOException 
  42.      */  
  43.     public static String cutImg(String input, String basepath, int x, int y,  
  44.             int width, int height) throws IOException {  
  45.         String path2 = basepath + "/" + ConstantUtils.USERFACETEMPPATH;  
  46.         String postfix = getPostfix(input);  
  47.         String dst = path2 + "/" + UUID.randomUUID().toString() + "." + postfix;  
  48.   
  49.         createDir(path2);  
  50.         imgCut(basepath + input, dst, postfix, x, y, width, height);  
  51.   
  52.         return dst;  
  53.     }  
  54.   
  55.     /** 
  56.      * 裁剪图片 
  57.      *  
  58.      * @param input 
  59.      * @param src 
  60.      * @param x 
  61.      * @param y 
  62.      * @param width 
  63.      * @param height 
  64.      * @throws IOException 
  65.      */  
  66.     public static void imgCut(String input, String dst, String type, int x,  
  67.             int y, int width, int height) throws IOException {  
  68.         BufferedImage fromimg = ImageIO.read(new File(input));  
  69.         ImageFilter cropFilter = new CropImageFilter(x, y, width, height);  
  70.         Image img = Toolkit.getDefaultToolkit().createImage(  
  71.                 new FilteredImageSource(fromimg.getSource(), cropFilter));  
  72.         BufferedImage tag = new BufferedImage(width, height,  
  73.                 BufferedImage.TYPE_INT_RGB);  
  74.   
  75.         Graphics g = tag.getGraphics();  
  76.         g.drawImage(img, 00null); // 绘制小图  
  77.         g.dispose();  
  78.         // 输出为文件  
  79.         // dir = "d:\\temp\\cut_image_" + i + "_" + j + ".jpg";  
  80.         File f = new File(dst);  
  81.         ImageIO.write(tag, type, f);  
  82.   
  83.     }  
  84.   
  85.     /** 
  86.      * 上传头像文件 
  87.      *  
  88.      * @param src 
  89.      * @param basepath 
  90.      * @param filename 
  91.      * @return 
  92.      * @throws Exception 
  93.      */  
  94.     public static String uploadImg(File src, String basepath, String filename)  
  95.             throws Exception {  
  96.         String daypath = DateTools.getYear() + "" + DateTools.getMonth() + ""  
  97.                 + DateTools.getMonthWeek();  
  98.   
  99.         String temppath = ConstantUtils.BASEUPLOADPATH + "/"  
  100.                 + ConstantUtils.ORIGINALIMGPATH + "/" + daypath;  
  101.         String thumbnailpath = ConstantUtils.BASEUPLOADPATH + "/"  
  102.                 + ConstantUtils.THUMBNAILIMGPATH + "/" + daypath;  
  103.   
  104.         String postfix = getPostfix(filename);  
  105.         String uuid = UUID.randomUUID().toString();  
  106.         String dsttempname = uuid + "." + postfix;  
  107.   
  108.         createDir(basepath + "/" + temppath);  
  109.         createDir(basepath + "/" + thumbnailpath);  
  110.   
  111.         String dstallpath = basepath + "/" + temppath + "/" + dsttempname;  
  112.         String dstthumbnailpath = basepath + "/" + thumbnailpath + "/"  
  113.                 + dsttempname;  
  114.   
  115.         copy(src, new File(dstallpath));  
  116.   
  117.         // 对上传的文件进行 等比例 裁剪。 按照前段要展现的 height width  
  118.         Thumbnail(dstallpath, dstthumbnailpath, 350300100);  
  119.   
  120.         // 返回裁剪后的路径  
  121.   
  122.         return thumbnailpath + "/" + dsttempname;  
  123.     }  
  124.   
  125.     /** 
  126.      * 上传文件 
  127.      *  
  128.      * @param src 
  129.      * @param dst 
  130.      * @throws Exception 
  131.      */  
  132.     public static void copy(File src, File dst) throws Exception {  
  133.         try {  
  134.             InputStream in = null;  
  135.             OutputStream out = null;  
  136.             try {  
  137.                 in = new BufferedInputStream(new FileInputStream(src),  
  138.                         ConstantUtils.BUFFER_SIZE);  
  139.                 out = new BufferedOutputStream(new FileOutputStream(dst),  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值