zoukankan      html  css  js  c++  java
  • 自动截屏

     
    
    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();
            }
        }
        
        
    }
  • 相关阅读:
    Google 的开源技术protobuf 简介与例子(转)
    set 学习笔记
    map 学习笔记
    网络编程-socket学习笔记
    POSIX线程_学习笔记
    shell 脚本练习
    vector 学习笔记
    用archlinux作为日常开发机的感受
    python中获取上一个月一号的方法
    golang在linux下的开发环境部署[未完]
  • 原文地址:https://www.cnblogs.com/fuyuanming/p/4953525.html
Copyright © 2011-2022 走看看