1.图片处理工具类
import java.awt.image.BufferedImage;
public class ImageZoom{
private static final long serialVersionUID = 1L;
private ImageZoom()
{}
public static BufferedImage createZoomImage(BufferedImage image,float zoom)
{
return createZoomImage(image,zoom,zoom);
}
public static BufferedImage createZoomImage(BufferedImage image,float xZoom,float yZoom)
{
BufferedImage createImage=null;
if(image!=null)
{
int width=0;
int height=0;
int forX=0;
int forY=0;
int tempZoomStartX=0;
int tempZoomStartY=0;
int tempZoomEndX=0;
int tempZoomEndY=0;
int rgb=0;
int imageRGB[] = null;
int imageChangeRGB[] = null;
width=(int)(image.getWidth()*xZoom);
height=(int)(image.getHeight()*yZoom);
forX=image.getWidth();
forY=image.getHeight();
imageChangeRGB=new int[height*width];
imageRGB=new int[forX*forY];
image.getRGB(0, 0, forX, forY, imageRGB, 0, forX);
createImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int y=0;y<forY;y++)
{
for(int x=0;x<forX;x++)
{
tempZoomStartX=(int)(x*xZoom);
tempZoomStartY=(int)(y*yZoom);
tempZoomEndX=(int)((x+1)*xZoom);
tempZoomEndY=(int)((y+1)*yZoom);
rgb=imageRGB[y*forX+x];
for(int yy=tempZoomStartY;yy<tempZoomEndY;yy++)
{
for(int xx=tempZoomStartX;xx<tempZoomEndX;xx++)
{
imageChangeRGB[yy*width+xx]=rgb;
}
}
}
}
createImage.setRGB(0, 0, width, height, imageChangeRGB, 0, width);
imageRGB=null;
imageChangeRGB=null;
}
return createImage;
}
public static BufferedImage inverse(BufferedImage image)
{
BufferedImage createImage=null;
if(image!=null)
{
int width=0;
int height=0;
width=image.getWidth();
height=image.getHeight();
int imageRGB[]=new int[width*height];
image.getRGB(0, 0, width, height, imageRGB, 0, width);
createImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int i=0;i<imageRGB.length;i++)
{
imageRGB[i]=imageRGB[i]^0xffffffff;
}
createImage.setRGB(0, 0, width, height, imageRGB, 0, width);
imageRGB=null;
}
return createImage;
}
}
2.test
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class test extends JFrame{
private static final long serialVersionUID = 1L;
BufferedImage image2=null;
public test()
{
File file=new File("d:/2.jpg");
File file2=new File("d:/2-1.jpg");
Date d;
long startTime;
long endTime;
float xZoom=20f;
float yZoom=20f;
try {
BufferedImage image=ImageIO.read(file);
d=new Date();
startTime=d.getTime();
image2=ImageZoom.createZoomImage(ImageZoom.inverse(image),xZoom,yZoom);
d=new Date();
endTime=d.getTime();
System.out.println("使用时间:"+(endTime-startTime)+"毫秒");
ImageIO.write(image2, "JPEG", file2);
} catch (IOException e) {
e.printStackTrace();
}
this.repaint();
this.setBounds(0,0,600,500);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public void paint(Graphics g){
g.drawImage(image2, 0, 30, this);
}
public static void main(String[] arg)
{
new test();
}
}
|