⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auto.~jav

📁 自动截取屏幕图像处理并保存的java源程序
💻 ~JAV
字号:

//Title:       auto
//Version:     1.0
//Copyright:   Copyright (c) 2003
//Author:      youngfan
//Description: 

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.swing.ImageIcon;
import java.net.*;
import java.awt.event.*;


public class auto extends Frame{

 public auto() {

  memScreenCapture(3*1000, "ScreenCapture.jpg");
  //ScreenCapture(1*1000, "ScreenCapture.jpg");
  //System.exit(0);
  }
/********************************************************************************/
  public static void main(String[] args) {
    Frame frame=new auto();
  }
/*******************************截取屏幕成数组**********************************/
  public void memScreenCapture(int seconds, String filename) {
  Robot robot = null;
  BufferedImage bufImage=null;
  int pixels[];
  int w,h;
  int i,j;
  ImageProducer dproducer;
  Image img_dark;
  ImageFilter darkf;
  w=Toolkit.getDefaultToolkit().getScreenSize().width;
  h=Toolkit.getDefaultToolkit().getScreenSize().height;
  pixels=new int[w*h];
  try {
      robot =new Robot();
      robot.delay(seconds);
      bufImage=robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
      darkf=new BrightnessFilter(1.0f);//可调节颜色深浅
      dproducer=new FilteredImageSource(bufImage.getSource(),darkf);
      img_dark=createImage(dproducer);
      PixelGrabber pg=new PixelGrabber(img_dark,0,0,w,h,pixels,0,w);

      try {pg.grabPixels();
           analyse(pixels,w,h);
           SaveImgFile(pixels,w,h,w,h);
           }
      catch(InterruptedException e)
       { System.out.println("grabber error"+e);
         //return;
       }
       if ((pg.status() & ImageObserver.ABORT) != 0)
       { System.out.println("grabber error");
         //return;
       }
       
      }
  catch(Exception e) {System.out.println("memScreenCapture error");}
  
  }//end memScreenCapture
/*******************************简单截屏存档**************************************/
  public void ScreenCapture(int seconds, String filename) {
  Robot robot = null;
  try {
      robot =new Robot();
      robot.delay(seconds);
      OutputStream f = new FileOutputStream(filename);
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);
      encoder.encode(robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())));
      f.close();
      }
  catch(AWTException e1) {System.out.println("AWTException\n");}
  catch(IOException e2) {System.out.println("IOException\n");}
  }//end ScreenCapture
/**********************************分析处理图象数组********************************/
  public void analyse(int[] pixels,int w,int h)
  { 
    for(int i=0;i<h;i++)
       for(int j=0;j<w;j++)
         {if(pixels[j+i*w]<>-1)
          pixels[j+i*w]=0xffff0000;
         }
  }
/**********************************将图象数组存档**********************************/
  public void SaveImgFile(int[] pixels_save,int w1,int h1,int w2,int h2)
  {//System.out.println(pixels_save);
   try{
   Image img_save=null;
   BufferedImage bufImage_save=null;
   img_save=createImage(new MemoryImageSource(w1,h1,pixels_save,0,w1));
   bufImage_save=toBufferedImage(img_save,w2,h2);
   File f1=new File("save.jpg");
   FileOutputStream os1=new FileOutputStream(f1);
   com.sun.image.codec.jpeg.JPEGImageEncoder en1=com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(os1);
   en1.encode(bufImage_save);
   os1.close();
   }
   catch(Exception e) {System.out.println("SaveImgFile error");}

  }

/*******************************转换Image格式为BufferedImage格式************************/
 public static BufferedImage toBufferedImage(Image image,int width,int height) {
        if (image instanceof BufferedImage) {
            return (BufferedImage)image;
        }

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Determine if the image has transparent pixels; for this method's
        // implementation, see e665 Determining If an Image Has Transparent Pixels
        //boolean hasAlpha = hasAlpha(image);

        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;

            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            /*//原始//
            bimage = gc.createCompatibleImage(
                image.getWidth(null), image.getHeight(null), transparency);
            */
            bimage = gc.createCompatibleImage(width,height,transparency);
        } catch (Exception e) {
            // The system does not have a screen
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
  //原始//bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
            bimage = new BufferedImage(width, height, type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();

        return bimage;
    }
    /******************************************************************************/
}//end class auto

/**************************像素色彩过滤器********************************/
class BrightnessFilter extends RGBImageFilter
{ float adjustment;
  float hsb_value[]=new float[3];
  ColorModel dcm=ColorModel.getRGBdefault();

public BrightnessFilter(float adjustment)
{ canFilterIndexColorModel=true;
  this.adjustment =adjustment;
}

public int filterRGB(int x,int y,int rgb)
{ int a=rgb & 0xff000000;
  int pixcolor= rgb & 0x00ffffff;
  Color.RGBtoHSB(dcm.getRed(rgb),dcm.getGreen(rgb),dcm.getBlue(rgb),hsb_value);
  hsb_value[0]=0;//色调为0
  hsb_value[1]=0;//饱和为0
  //hsb_value[2]*=adjustment;
  if (hsb_value[2]>0.5)
  hsb_value[2]=1f;
  else
  hsb_value[2]=0f;

  hsb_value[2]=Math.max(0.0f,Math.min(hsb_value[2],1.0f));
  return (a|Color.HSBtoRGB(hsb_value[0],hsb_value[1],hsb_value[2]));
}

}//end class BrightnessFilter

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -