歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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. }    
Copyright © Linux教程網 All Rights Reserved