JAVA实现图像缩放


JAVA实现图像缩放(通过 java.awt.geom的仿射变换结合java.awt.image的各种插值方法实现)。

程序分为2部分:

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

对上面标准封装的使用和测试,以及通过移位来提取单个像素或者图像某一特定区域的经过包装过的RGB值。

  1. package com.han;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6.   
  7. import javax.imageio.ImageIO;  
  8.   
  9. public class Test_ImageScale {  
  10.     public static void main(String[] args){  
  11.         BufferedImage in;  
  12.         try {  
  13.             in = ImageIO.read(new File("C:/Users/HAN/Pictures/1_gaowen_han.jpg"));  
  14.             long   t1   =   System.currentTimeMillis(); //设定时间,以便可以比较各种不同插值的效率与系统开销   
  15.             BufferedImage out=ImageScale.scale(in, 443);//进行图像缩放   
  16.             long   t2   =   System.currentTimeMillis();   
  17.             //写出结果图片到电脑硬盘   
  18.             boolean b=ImageIO.write(out, "jpg"new File("C:/Users/HAN/Pictures/scale_gaowen_han.jpg"));  
  19.             System.out.println(b);  
  20.             System.out.println(t2-t1);  
  21.             System.out.println("Width : "+out.getWidth()+"\n"+"Height : "+out.getHeight());   
  22.           
  23. /*          int pixel=out.getRGB(0, 0); 
  24.             int R=(pixel & 0xff0000)>>16; 
  25.             int G=(pixel & 0xff00)>>8; 
  26.             int B=(pixel & 0xff); 
  27.             System.out.println("pixel R : "+R); 
  28.             System.out.println("pixel G : "+G); 
  29.             System.out.println("pixel B : "+B);*/  
  30.             /**********对缩放后的图像的像素RGB的提取********************/  
  31.             // 1. 直接进行单个元素的提取   
  32.             // 2. 进行图像中一块特定区域的像素提取   
  33.             // 3. 分别输出,进行比较以验证数学运算公式的正确性   
  34.             int startX=0;  
  35.             int startY=0;  
  36.             int offset=0;  
  37.             int scansize=10;  
  38.             int[] rgbArray=out.getRGB(startX, startY, 1010null, offset, scansize);  
  39.             int x=8;  
  40.             int y=8;  
  41.             int pixel=out.getRGB(x, y);  
  42.             int pixel_prime  = rgbArray[offset + (y-startY)*scansize + (x-startX)];   
  43.             int R,G,B;  
  44.             R=(pixel & 0xff0000)>>16;  
  45.             G=(pixel & 0xff00)>>8;  
  46.             B=(pixel & 0xff);  
  47.             /*反之,由RGB分量也可以得到包装的颜色值 
  48.             int rbg = (255 << 24) | (r << 16) | (g << 8 )| b;*/  
  49.             System.out.println("pixel R : "+R);  
  50.             System.out.println("pixel G : "+G);  
  51.             System.out.println("pixel B : "+B);  
  52.             R=(pixel_prime & 0xff0000)>>16;  
  53.             G=(pixel_prime & 0xff00)>>8;  
  54.             B=(pixel_prime & 0xff);  
  55.             System.out.println("pixel R : "+R);  
  56.             System.out.println("pixel G : "+G);  
  57.             System.out.println("pixel B : "+B);  
  58.         } catch (IOException e) {  
  59.             // TODO Auto-generated catch block   
  60.             e.printStackTrace();  
  61.         }  
  62.           
  63.     }  
  64. }  
有了像素的提取,使得能够像Matlab,Mathematica,IDL那样自己编写卫星照片处理等程序。当然JAVA中有自己的很多优秀封装库可以直接引用,比如JAVA Advanced Image Processing等。

相关内容