/*以下代码用于连续捕获桌面,但是用setRGB()更新之后,再重绘窗口,没有什么反应。*/import java.awt.*;import java.awt.image.BufferedImage;public class Image extends Frame {static BufferedImage im;public static void main(String[] args){ int i;Image image=new Image(800,600);im=image.capSc();//获取第一桢BufferedImage temp;for(i=0;i<1000;i++){temp=image.capSc();//捕获桌面图像image.getdata(temp);//与原图像逐相素对比更新image.repaint();//重绘}}public void getdata(BufferedImage temp){int i,j,height,width,pix;width=temp.getWidth();height=temp.getHeight();for(i=0;i<width-1;i+=2)for(j=0;j<height-1;j+=2){ pix=temp.getRGB(i,j);if(pix!=im.getRGB(i,j))//每个相素进行比较如果不等{im.setRGB(i,j,pix);//则更新为现在的像素值。}}}public BufferedImage capSc()//这个函数可略过不看,捕获桌面,并返回图像{ BufferedImage image=null;try{Robot robot=new Robot();Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Rectangle screenRectangle = new Rectangle(screenSize);image = robot.createScreenCapture(screenRectangle);return image;} catch(Exception exe){exe.printStackTrace();}return image;}public Image(int width,int height){setSize(width,height);setVisible(true);}public void paint(Graphics g){g.drawImage(im,0,0,800,600,this);}}
1 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
不会啊,我试了一下图像是变化的,不过下边这段代码没太懂,为什么i和j要递增2呢?递增1的话感觉效果更有趣一些:)
for(i=0;i<width-1;i+=2)
for(j=0;j<height-1;j+=2)
{ pix=temp.getRGB(i,j);
if(pix!=im.getRGB(i,j))//每个相素进行比较如果不等
{im.setRGB(i,j,pix);//则更新为现在的像素值。
}
}
添加回答
举报
0/150
提交
取消
