自动截屏

 

import java.awt.AWTException; 
import java.awt.Canvas; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D;
import java.awt.Rectangle; 
import java.awt.Robot; 
import java.awt.Toolkit; 
import java.awt.image.BufferedImage; 
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 
import javax.swing.GroupLayout.Alignment;

/**
 * 自动截屏
 * 
 *
 */
public class Capture extends JFrame implements Runnable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Canvas screenCanvas; 
    private volatile boolean stop; 
    private Robot robot;  
    private Dimension dim;  
    public Capture() {  
           initComponents();  
           try {  
            robot = new Robot();  
           } catch (AWTException ex) {  
            ex.printStackTrace();  
           }  
           dim = Toolkit.getDefaultToolkit().getScreenSize(); 
    }
    
    private void initComponents() {  
        screenCanvas = new java.awt.Canvas();  
           setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
           stop = true;  
           setResizable(false);  
           GroupLayout layout = new GroupLayout(getContentPane());
           getContentPane().setLayout(layout);  
            layout.setHorizontalGroup(  
            layout.createParallelGroup(Alignment.LEADING).addComponent(screenCanvas,GroupLayout.PREFERRED_SIZE, 519,GroupLayout.PREFERRED_SIZE) );  
            layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(screenCanvas,GroupLayout.PREFERRED_SIZE, 434,GroupLayout.PREFERRED_SIZE));  
           pack();
    }
    
    public static void main(String args[]) {  

           final Capture capture = new Capture();  

           EventQueue.invokeLater(new Runnable() {  
               public void run() {  
                  capture.setVisible(true);  
               }  
           });  

           Thread cutThread = new Thread(capture);  
           cutThread.start(); 
        } 
    
    public void run() {
        
        stop = false; 
         
        
        // while(!stop) {  
            BufferedImage bImage = robot.createScreenCapture(new 
                    Rectangle(dim.width, dim.height));  
            Graphics g = this.screenCanvas.getGraphics();  
            g.drawImage(bImage, 0, 0, this);  
            
            
            
            try {  
                Thread.sleep(5000);  
            } catch (InterruptedException ex) {  
                ex.printStackTrace();  
            }  
            
            try {
                ImageIO.write(bImage, "jpg", new File("e:/456.jpg"));
            } catch (IOException e) {
                e.printStackTrace();
            } 

            
             
             
         //}  
        
    } 
    
    private synchronized void stop() {  
           stop = true;  
    } 
    
    //本来就没错呀
    public static void targetZoomOut(String sourcePath) { //将目标图片缩小成256*256并保存
        File file1 = new File(sourcePath); //用file1取得图片名字
        String name = file1.getName();
 
        try {
            BufferedImage input = ImageIO.read(file1);
            BufferedImage inputbig = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR);
            Graphics2D g = (Graphics2D) inputbig.getGraphics();
            g.drawImage(input, 0, 0,256,256,null); //画图
            g.dispose();
            inputbig.flush();
 
            File file2 = new File("e:/"); //此目录保存缩小后的关键图
            if (file2.exists()) {
                System.out.println("多级目录已经存在不需要创建!!");
            } else {
                //如果要创建的多级目录不存在才需要创建。
                file2.mkdirs();
            }
            String fname = name.substring(0, name.lastIndexOf("."));//新名字
            ImageIO.write(inputbig, "jpg", new File("e:/" + fname + ".jpg")); //将其保存在C:/imageSort/targetPIC/下
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
}

 

posted on 2015-11-10 16:48  悄悄的来,匆匆的走  阅读(227)  评论(0编辑  收藏  举报

导航