• Java_Win屏幕截图


    参考网址:

    百度 关键字 “java 截屏”
    http://blog.csdn.net/caoyuan10036/article/details/7199505
    http://www.open-open.com/lib/view/open1363155962718.html
    http://www.jb51.net/article/44614.htm

    1、

    网页上的代码:

    package sc;
    
    import java.io.*;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.imageio.*;
    
    public class TScreenCapture
    {
        public static void main(String[] args) throws Exception
        {
            String userdir = System.getProperty("user.dir");
            File tempFile = new File("d:", "temp.png");
            TScreenCapture capture = TScreenCapture.getInstance();
            capture.captureImage();
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            JLabel imagebox = new JLabel();
            panel.add(BorderLayout.CENTER, imagebox);
            imagebox.setIcon(capture.getPickedIcon());
            capture.saveToFile(tempFile);
            capture.captureImage();
            imagebox.setIcon(capture.getPickedIcon());
            frame.setContentPane(panel);
            frame.setSize(400, 300);
            frame.show();
            System.out.println("Over");
        }
    
        
        private TScreenCapture()
        {
            try {
                robot = new Robot();
            } catch (AWTException e) {
                System.err.println("Internal Error: " + e);
                e.printStackTrace();
            }
            JPanel cp = (JPanel) dialog.getContentPane();
            cp.setLayout(new BorderLayout());
            labFullScreenImage.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent evn) {
                    isFirstPoint = true;
                    pickedImage = fullScreenImage.getSubimage(recX, recY, recW,
                            recH);
                    dialog.setVisible(false);
                }
            });
    
            labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent evn) {
                    if (isFirstPoint) {
                        x1 = evn.getX();
                        y1 = evn.getY();
                        isFirstPoint = false;
                    } else {
                        x2 = evn.getX();
                        y2 = evn.getY();
                        int maxX = Math.max(x1, x2);
                        int maxY = Math.max(y1, y2);
                        int minX = Math.min(x1, x2);
                        int minY = Math.min(y1, y2);
                        recX = minX;
                        recY = minY;
                        recW = maxX - minX;
                        recH = maxY - minY;
                        labFullScreenImage.drawRectangle(recX, recY, recW, recH);
                    }
                }
    
                public void mouseMoved(MouseEvent e) {
                    labFullScreenImage.drawCross(e.getX(), e.getY());
                }
            });
    
            cp.add(BorderLayout.CENTER, labFullScreenImage);
            dialog.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            dialog.setAlwaysOnTop(true);
            dialog.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
            dialog.setUndecorated(true);
            dialog.setSize(dialog.getMaximumSize());
            dialog.setModal(true);
        }
    
        // Singleton Pattern
        public static TScreenCapture getInstance()
        {
            return defaultCapturer;
        }
    
        /** 捕捉全屏慕 */
        public Icon captureFullScreen()
        {
            fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            ImageIcon icon = new ImageIcon(fullScreenImage);
            return icon;
        }
    
        /** 捕捉屏幕的一个矫形区域 */
        public void captureImage()
        {
            fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            ImageIcon icon = new ImageIcon(fullScreenImage);
            labFullScreenImage.setIcon(icon);
            dialog.setVisible(true);
        }
    
        /** 得到捕捉后的BufferedImage */
        public BufferedImage getPickedImage()
        {
            return pickedImage;
        }
    
        /** 得到捕捉后的Icon */
        public ImageIcon getPickedIcon() {
            return new ImageIcon(getPickedImage());
        }
    
        /**
         * 储存为一个文件,为PNG格式
         * 
         * @deprecated replaced by saveAsPNG(File file)
         **/
        @Deprecated
        public void saveToFile(File file) throws IOException {
            ImageIO.write(getPickedImage(), defaultImageFormater, file);
        }
    
        /** 储存为一个文件,为PNG格式 */
        public void saveAsPNG(File file) throws IOException {
            ImageIO.write(getPickedImage(), "png", file);
        }
    
        /** 储存为一个JPEG格式图像文件 */
        public void saveAsJPEG(File file) throws IOException {
            ImageIO.write(getPickedImage(), "JPEG", file);
        }
    
        /** 写入一个OutputStream */
        public void write(OutputStream out) throws IOException {
            ImageIO.write(getPickedImage(), defaultImageFormater, out);
        }
    
        // singleton design pattern
        private static TScreenCapture defaultCapturer = new TScreenCapture();
        private int x1, y1, x2, y2;
        private int recX, recY, recH, recW; // 截取的图像
        private boolean isFirstPoint = true;
        private BackgroundImage labFullScreenImage = new BackgroundImage();
        private Robot robot;
        private BufferedImage fullScreenImage;
        private BufferedImage pickedImage;
        private String defaultImageFormater = "png";
        private JDialog dialog = new JDialog();
        
    }
    
    /** 显示图片的Label */
    class BackgroundImage extends JLabel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawRect(x, y, w, h);
            String area = Integer.toString(w) + " * " + Integer.toString(h);
            g.drawString(area, x + (int) w / 2 - 15, y + (int) h / 2);
            g.drawLine(lineX, 0, lineX, getHeight());
    
            g.drawLine(0, lineY, getWidth(), lineY);
        }
    
        public void drawRectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            h = height;
            w = width;
            repaint();
        }
    
        public void drawCross(int x, int y) {
            lineX = x;
            lineY = y;
            repaint();
        }
    
        int lineX, lineY;
        int x, y, h, w;
    }

    2、

    我的测试代码:

    package sc;
    
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    
    public class TScreenCaptureZ
    {
        
        public static void main(String[] args) throws Exception
        {
            Robot robot = new Robot();
            BufferedImage fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            
            File fileSave = new File("d:", "temp_z.jpg");
            ImageIO.write(fullScreenImage, "JPEG", fileSave);
        }
    
    }

    3、

    网页中使用:

    3.1、类 TScreenCaptureZ :

     1 package screenCapture;
     2 
     3 
     4 import java.awt.Rectangle;
     5 import java.awt.Robot;
     6 import java.awt.Toolkit;
     7 import java.awt.image.BufferedImage;
     8 import java.io.File;
     9 import java.text.SimpleDateFormat;
    10 import java.util.Date;
    11 
    12 import javax.imageio.ImageIO;
    13 
    14 public class TScreenCaptureZ
    15 {
    16     
    17     public static void main(String[] args) throws Exception
    18     {
    19         TScreenCaptureZ.Capture_FullScreen("d:/");
    20     }
    21     
    22     public static String Capture_FullScreen(String _strPath) throws Exception
    23     {
    24         // 返回的是 截屏的图形的文件名
    25         
    26         String strDir = _strPath+"CaptureJPGs/";
    27         DeleteFilesInDir(strDir);
    28         
    29         String strTime = TimeZ();
    30         //String strJPG = "d:/"+strTime+".jpg";
    31         //File fileSave = new File("d:", strJPG);
    32         String strJpgSave = strDir+strTime+".jpg";
    33         String strJpgRtn = strTime+".jpg";
    34         
    35     // ***
    36         Robot robot = new Robot();
    37         BufferedImage fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    38         
    39         File fileSave = new File(strJpgSave);
    40         ImageIO.write(fullScreenImage, "JPEG", fileSave);
    41         //*/
    42         return strJpgRtn;
    43     }
    44     
    45     public static void DeleteFilesInDir(String _strDir)
    46     {
    47         File fileDir = new File(_strDir);
    48         if (fileDir.exists() && fileDir.isDirectory())
    49         {
    50             File[] files = fileDir.listFiles();  
    51             for (int i = 0; i < files.length; i++)
    52             {
    53                 File fileDelete = files[i];
    54                 if (fileDelete.exists() && fileDelete.isFile())
    55                 {
    56                     if (! fileDelete.delete())
    57                         System.out.println(fileDelete.getAbsolutePath()+" 删除失败 .");
    58                 }
    59                 else
    60                 {
    61                     System.out.println(fileDelete.getAbsolutePath()+" 不存在 或 不是文件 .");
    62                 }
    63             }
    64         }
    65     }
    66     
    67     public static String TimeZ()
    68     {
    69         Date nowTime=new Date();
    70         //System.out.println(nowTime);
    71         SimpleDateFormat time=new SimpleDateFormat("yyyyMMdd_hhmmss_SSS");// 年月日_时分秒_毫秒
    72         String str = time.format(nowTime);
    73         //System.out.println(str);
    74         
    75         return str;
    76     }
    77 
    78 }

    3.2、

    JSP 内容:

     1 <%@ page language="java" import="java.util.*, screenCapture.*" pageEncoding="utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'index.jsp' starting page</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17     <meta http-equiv="description" content="This is my page">
    18     <!--
    19     <link rel="stylesheet" type="text/css" href="styles.css">
    20     -->
    21   </head>
    22   
    23   <body>
    24     <%
    25     String strPath = request.getServletContext().getRealPath("/");
    26     String strFileName = TScreenCaptureZ.Capture_FullScreen(strPath);
    27     %>
    28     
    29     <!-- <img src="20160115_094016_304.jpg"/> -->
    30     <img src="CaptureJPGs/<%=strFileName%>"/>
    31   </body>
    32 </html>

    C

  • 相关阅读:
    opendressinghash //use resize array
    ChainingHash
    Hash function
    stack && queue
    random_select
    counting sort
    master theorem
    各排序算法及其比较
    视图中添加主键的方法
    oracle表空间的扩展
  • 原文地址:https://www.cnblogs.com/codeskilla/p/5133226.html
Copyright © 2020-2023  润新知