歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> JAVA實現圖像縮放

JAVA實現圖像縮放

日期:2017/3/1 10:41:58   编辑:Linux編程

JAVA實現圖像縮放(通過 java.awt.geom的仿射變換結合java.awt.image的各種插值方法實現)。

程序分為2部分:

  1. 實現標准封裝ImageScale功能
  2. 代碼塊去測試和使用ImageScale類的效果,以及對其中RGB元素通過移位手段的提取
  1. package com.han;
  2. import java.awt.geom.AffineTransform;
  3. import java.awt.image.AffineTransformOp;
  4. import java.awt.image.BufferedImage;
  5. public class ImageScale {
  6. /**
  7. * It is designed for scaling images. Developed by Gaowen HAN.
  8. * @param in the source image
  9. * @param sx the width scale ratio
  10. * @param sy the height scale ratio
  11. * @param interpolationType "1" represents <code>AffineTransformOp.TYPE_NEAREST_NEIGHBOR</code>;
  12. * "2" represents <code>AffineTransformOp.TYPE_BILINEAR</code>;
  13. * "3" represents <code>AffineTransformOp.TYPE_BICUBIC</code>;
  14. * @return the scaled image
  15. */
  16. public static BufferedImage scale(BufferedImage in,
  17. double sx,
  18. double sy,
  19. int interpolationType){
  20. AffineTransform matrix=new AffineTransform(); //仿射變換
  21. matrix.scale(sx,sy);
  22. AffineTransformOp op = null;
  23. if (interpolationType==1){
  24. op=new AffineTransformOp(matrix, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
  25. }else if (interpolationType==2){
  26. op=new AffineTransformOp(matrix, AffineTransformOp.TYPE_BILINEAR);
  27. }else if (interpolationType==3){
  28. op=new AffineTransformOp(matrix, AffineTransformOp.TYPE_BICUBIC);
  29. }else{
  30. try {
  31. throw new Exception("input interpolation type from 1-3 !");
  32. } catch (Exception e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. }
  37. int width = (int)((double)in.getWidth() * sx);
  38. int height = (int)((double)in.getHeight() * sy);
  39. BufferedImage dstImage = new BufferedImage(width, height, in.getType());
  40. //System.out.println(in.getType());
  41. //BufferedImage dstImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  42. //BufferedImage out=op.filter(in, dstImage);
  43. op.filter(in, dstImage);
  44. //注意下面的代碼不同, 結果背景顏色不同
  45. //BufferedImage out=op.filter(in, null);
  46. return dstImage;
  47. }
  48. }

對上面標准封裝的使用和測試,以及通過移位來提取單個像素或者圖像某一特定區域的經過包裝過的RGB值。

  1. package com.han;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6. public class Test_ImageScale {
  7. public static void main(String[] args){
  8. BufferedImage in;
  9. try {
  10. in = ImageIO.read(new File("C:/Users/HAN/Pictures/1_gaowen_han.jpg"));
  11. long t1 = System.currentTimeMillis(); //設定時間,以便可以比較各種不同插值的效率與系統開銷
  12. BufferedImage out=ImageScale.scale(in, 4, 4, 3);//進行圖像縮放
  13. long t2 = System.currentTimeMillis();
  14. //寫出結果圖片到電腦硬盤
  15. boolean b=ImageIO.write(out, "jpg", new File("C:/Users/HAN/Pictures/scale_gaowen_han.jpg"));
  16. System.out.println(b);
  17. System.out.println(t2-t1);
  18. System.out.println("Width : "+out.getWidth()+"\n"+"Height : "+out.getHeight());
  19. /* int pixel=out.getRGB(0, 0);
  20. int R=(pixel & 0xff0000)>>16;
  21. int G=(pixel & 0xff00)>>8;
  22. int B=(pixel & 0xff);
  23. System.out.println("pixel R : "+R);
  24. System.out.println("pixel G : "+G);
  25. System.out.println("pixel B : "+B);*/
  26. /**********對縮放後的圖像的像素RGB的提取********************/
  27. // 1. 直接進行單個元素的提取
  28. // 2. 進行圖像中一塊特定區域的像素提取
  29. // 3. 分別輸出,進行比較以驗證數學運算公式的正確性
  30. int startX=0;
  31. int startY=0;
  32. int offset=0;
  33. int scansize=10;
  34. int[] rgbArray=out.getRGB(startX, startY, 10, 10, null, offset, scansize);
  35. int x=8;
  36. int y=8;
  37. int pixel=out.getRGB(x, y);
  38. int pixel_prime = rgbArray[offset + (y-startY)*scansize + (x-startX)];
  39. int R,G,B;
  40. R=(pixel & 0xff0000)>>16;
  41. G=(pixel & 0xff00)>>8;
  42. B=(pixel & 0xff);
  43. /*反之,由RGB分量也可以得到包裝的顏色值
  44. int rbg = (255 << 24) | (r << 16) | (g << 8 )| b;*/
  45. System.out.println("pixel R : "+R);
  46. System.out.println("pixel G : "+G);
  47. System.out.println("pixel B : "+B);
  48. R=(pixel_prime & 0xff0000)>>16;
  49. G=(pixel_prime & 0xff00)>>8;
  50. B=(pixel_prime & 0xff);
  51. System.out.println("pixel R : "+R);
  52. System.out.println("pixel G : "+G);
  53. System.out.println("pixel B : "+B);
  54. } catch (IOException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. }
  59. }
有了像素的提取,使得能夠像Matlab,Mathematica,IDL那樣自己編寫衛星照片處理等程序。當然JAVA中有自己的很多優秀封裝庫可以直接引用,比如JAVA Advanced Image Processing等。
Copyright © Linux教程網 All Rights Reserved