Java图片缩放剪切处理


Java图片缩放剪切处理:

  1. package action;     
  2.     
  3. import java.awt.Graphics;     
  4. import java.awt.Image;     
  5. import java.awt.Toolkit;     
  6. import java.awt.image.BufferedImage;     
  7. import java.awt.image.CropImageFilter;     
  8. import java.awt.image.FilteredImageSource;     
  9. import java.awt.image.ImageFilter;     
  10. import java.io.File;     
  11. import java.io.FileOutputStream;     
  12. import java.io.IOException;     
  13.     
  14. import javax.imageio.ImageIO;     
  15.     
  16. import com.sun.image.codec.jpeg.JPEGCodec;     
  17. import com.sun.image.codec.jpeg.JPEGImageEncoder;     
  18.     
  19. public class ImageProcess {     
  20.     
  21.     /**   
  22.      * 对图片进行缩放   
  23.      *    
  24.      * @param srcImgFileName   
  25.      * @throws IOException   
  26.      */    
  27.     public void zoomImage(String srcImgFileName) throws IOException {     
  28.         // 读入文件      
  29.         File _file = new File(srcImgFileName);     
  30.         // 构造Image对象      
  31.         BufferedImage src = javax.imageio.ImageIO.read(_file);     
  32.         int width = src.getWidth();     
  33.         int height = src.getHeight();     
  34.     
  35.         // 边长缩小为二分之一      
  36.         BufferedImage tag = new BufferedImage(width / 2, height / 2, BufferedImage.TYPE_INT_RGB);     
  37.         // 绘制缩小后的图      
  38.         tag.getGraphics().drawImage(src, 00, width / 2, height / 2null);     
  39.         FileOutputStream out = new FileOutputStream("D:\\test1\\targetIMG1-4.jpg");     
  40.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);     
  41.         encoder.encode(tag);     
  42.         out.close();     
  43.     
  44.         // 边长扩大为2倍      
  45.         tag = new BufferedImage(width * 2, height * 2, BufferedImage.TYPE_INT_RGB);     
  46.         tag.getGraphics().drawImage(src, 00, width * 2, height * 2null);     
  47.         out = new FileOutputStream("D:\\test1\\targetIMGx2.jpg");     
  48.         encoder = JPEGCodec.createJPEGEncoder(out);     
  49.         encoder.encode(tag);     
  50.         out.close();     
  51.     
  52.     }     
  53.     
  54.     /**   
  55.      * 将图片分成九块   
  56.      *    
  57.      * @param srcImageFile   
  58.      * @throws IOException   
  59.      */    
  60.     public void cut(String srcImageFile) throws IOException {     
  61.         Image img;     
  62.         ImageFilter cropFilter;     
  63.         String dir = null;     
  64.         // 读取源图像      
  65.         BufferedImage src = ImageIO.read(new File(srcImageFile));     
  66.         int destWidth = src.getWidth() / 3;     
  67.         int destHeight = src.getHeight() / 3;     
  68.         // 循环      
  69.         for (int i = 0; i < 3; i++) {     
  70.             for (int j = 0; j < 3; j++) {     
  71.                 // 四个参数分别为图像起点坐标和宽高      
  72.                 cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight);     
  73.                 img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(src.getSource(), cropFilter));     
  74.                 BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);     
  75.                 Graphics g = tag.getGraphics();     
  76.                 g.drawImage(img, 00null); // 绘制小图      
  77.                 g.dispose();     
  78.                 // 输出为文件      
  79.                 dir = "D:\\test1\\cut_image_" + i + "_" + j + ".jpg";     
  80.                 File f = new File(dir);     
  81.                 ImageIO.write(tag, "JPEG", f);     
  82.             }     
  83.         }     
  84.     }     
  85.     
  86.     public static void main(String[] args) throws IOException {     
  87.         String imgFileName = "D:\\test\\test.png";     
  88.         ImageProcess iZoom = new ImageProcess();     
  89.     
  90.         iZoom.zoomImage(imgFileName);     
  91.         iZoom.cut(imgFileName);     
  92.     }     
  93. }    

相关内容