歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java圖片縮放剪切處理

Java圖片縮放剪切處理

日期:2017/3/1 10:28:14   编辑:Linux編程

Java圖片縮放剪切處理:

  1. package action;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.CropImageFilter;
  7. import java.awt.image.FilteredImageSource;
  8. import java.awt.image.ImageFilter;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import javax.imageio.ImageIO;
  13. import com.sun.image.codec.jpeg.JPEGCodec;
  14. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  15. public class ImageProcess {
  16. /**
  17. * 對圖片進行縮放
  18. *
  19. * @param srcImgFileName
  20. * @throws IOException
  21. */
  22. public void zoomImage(String srcImgFileName) throws IOException {
  23. // 讀入文件
  24. File _file = new File(srcImgFileName);
  25. // 構造Image對象
  26. BufferedImage src = javax.imageio.ImageIO.read(_file);
  27. int width = src.getWidth();
  28. int height = src.getHeight();
  29. // 邊長縮小為二分之一
  30. BufferedImage tag = new BufferedImage(width / 2, height / 2, BufferedImage.TYPE_INT_RGB);
  31. // 繪制縮小後的圖
  32. tag.getGraphics().drawImage(src, 0, 0, width / 2, height / 2, null);
  33. FileOutputStream out = new FileOutputStream("D:\\test1\\targetIMG1-4.jpg");
  34. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  35. encoder.encode(tag);
  36. out.close();
  37. // 邊長擴大為2倍
  38. tag = new BufferedImage(width * 2, height * 2, BufferedImage.TYPE_INT_RGB);
  39. tag.getGraphics().drawImage(src, 0, 0, width * 2, height * 2, null);
  40. out = new FileOutputStream("D:\\test1\\targetIMGx2.jpg");
  41. encoder = JPEGCodec.createJPEGEncoder(out);
  42. encoder.encode(tag);
  43. out.close();
  44. }
  45. /**
  46. * 將圖片分成九塊
  47. *
  48. * @param srcImageFile
  49. * @throws IOException
  50. */
  51. public void cut(String srcImageFile) throws IOException {
  52. Image img;
  53. ImageFilter cropFilter;
  54. String dir = null;
  55. // 讀取源圖像
  56. BufferedImage src = ImageIO.read(new File(srcImageFile));
  57. int destWidth = src.getWidth() / 3;
  58. int destHeight = src.getHeight() / 3;
  59. // 循環
  60. for (int i = 0; i < 3; i++) {
  61. for (int j = 0; j < 3; j++) {
  62. // 四個參數分別為圖像起點坐標和寬高
  63. cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight);
  64. img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(src.getSource(), cropFilter));
  65. BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
  66. Graphics g = tag.getGraphics();
  67. g.drawImage(img, 0, 0, null); // 繪制小圖
  68. g.dispose();
  69. // 輸出為文件
  70. dir = "D:\\test1\\cut_image_" + i + "_" + j + ".jpg";
  71. File f = new File(dir);
  72. ImageIO.write(tag, "JPEG", f);
  73. }
  74. }
  75. }
  76. public static void main(String[] args) throws IOException {
  77. String imgFileName = "D:\\test\\test.png";
  78. ImageProcess iZoom = new ImageProcess();
  79. iZoom.zoomImage(imgFileName);
  80. iZoom.cut(imgFileName);
  81. }
  82. }
Copyright © Linux教程網 All Rights Reserved