Java圖片縮放剪下處理:

來源:互聯網
上載者:User

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(newFilteredImageSource(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. }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.