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

来源:互联网 发布:众划算上返现淘宝抓吗 编辑:程序博客网 时间:2024/06/07 13:47

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

程序分为2部分:

  1. 实现标准封装ImageScale功能
  2. 代码块去测试和使用ImageScale类的效果,以及对其中RGB元素通过移位手段的提取
package com.han;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;public class ImageScale {/** * It is designed for scaling images. Developed by Gaowen HAN. * @param in the source image * @param sx the width scale ratio * @param sy the height scale ratio * @param interpolationType "1" represents <code>AffineTransformOp.TYPE_NEAREST_NEIGHBOR</code>;  *                          "2" represents <code>AffineTransformOp.TYPE_BILINEAR</code>;  *                          "3" represents <code>AffineTransformOp.TYPE_BICUBIC</code>;  * @return the scaled image */public static BufferedImage scale(BufferedImage in, double sx, double sy, int interpolationType){AffineTransform matrix=new AffineTransform(); //仿射变换matrix.scale(sx,sy);AffineTransformOp op = null;if (interpolationType==1){op=new AffineTransformOp(matrix, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);}else if (interpolationType==2){op=new AffineTransformOp(matrix, AffineTransformOp.TYPE_BILINEAR);}else if (interpolationType==3){op=new AffineTransformOp(matrix, AffineTransformOp.TYPE_BICUBIC);}else{try {throw new Exception("input interpolation type from 1-3 !");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}int width = (int)((double)in.getWidth() * sx);int height = (int)((double)in.getHeight() * sy);BufferedImage dstImage = new BufferedImage(width, height, in.getType());//System.out.println(in.getType());//BufferedImage dstImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);//BufferedImage out=op.filter(in, dstImage);op.filter(in, dstImage);//注意下面的代码不同, 结果背景颜色不同//BufferedImage out=op.filter(in, null);return dstImage;}}


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

package com.han;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class Test_ImageScale {public static void main(String[] args){BufferedImage in;try {in = ImageIO.read(new File("C:/Users/HAN/Pictures/1_gaowen_han.jpg"));long   t1   =   System.currentTimeMillis(); //设定时间,以便可以比较各种不同插值的效率与系统开销BufferedImage out=ImageScale.scale(in, 4, 4, 3);//进行图像缩放long   t2   =   System.currentTimeMillis(); //写出结果图片到电脑硬盘boolean b=ImageIO.write(out, "jpg", new File("C:/Users/HAN/Pictures/scale_gaowen_han.jpg"));System.out.println(b);System.out.println(t2-t1);System.out.println("Width : "+out.getWidth()+"\n"+"Height : "+out.getHeight());/*int pixel=out.getRGB(0, 0);int R=(pixel & 0xff0000)>>16;int G=(pixel & 0xff00)>>8;int B=(pixel & 0xff);System.out.println("pixel R : "+R);System.out.println("pixel G : "+G);System.out.println("pixel B : "+B);*//**********对缩放后的图像的像素RGB的提取********************/// 1. 直接进行单个元素的提取// 2. 进行图像中一块特定区域的像素提取// 3. 分别输出,进行比较以验证数学运算公式的正确性int startX=0;int startY=0;int offset=0;int scansize=10;int[] rgbArray=out.getRGB(startX, startY, 10, 10, null, offset, scansize);int x=8;int y=8;int pixel=out.getRGB(x, y);int pixel_prime  = rgbArray[offset + (y-startY)*scansize + (x-startX)]; int R,G,B;R=(pixel & 0xff0000)>>16;G=(pixel & 0xff00)>>8;B=(pixel & 0xff);/*反之,由RGB分量也可以得到包装的颜色值int rbg = (255 << 24) | (r << 16) | (g << 8 )| b;*/System.out.println("pixel R : "+R);System.out.println("pixel G : "+G);System.out.println("pixel B : "+B);R=(pixel_prime & 0xff0000)>>16;G=(pixel_prime & 0xff00)>>8;B=(pixel_prime & 0xff);System.out.println("pixel R : "+R);System.out.println("pixel G : "+G);System.out.println("pixel B : "+B);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

有了像素的提取,使得能够像Matlab,Mathematica,IDL那样自己编写卫星照片处理等程序。当然JAVA中有自己的很多优秀封装库可以直接引用,比如JAVA Advanced Image Processing等。

原创粉丝点击