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

时间: 2024-10-01 04:52:08

Java_Win屏幕截图的相关文章

iOS开发- OpenGL ES屏幕截图

之前写过一个常规的屏幕截图:http://blog.csdn.net/hitwhylz/article/details/17189351 可是发现这个办法对于OpenGL 无用.  获取到的数据为空. 所以这里介绍下OpenGL ES屏幕截图. 1.初始化. CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer; eaglLayer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBack

Java制作屏幕截图软件(还可以保存到剪切板内)

package com.kaige123;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;i

WinSnap屏幕截图 V4.5.6 官方最新版

软件名称: WinSnap屏幕截图软件语言: 多国语言授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 2.7MB图片预览: 软件简介:WinSnap 是一款完全免费的屏幕捕获软件,能够让您得心应手地捕获到需要的屏幕截图 捕捉图像方式灵活,主要可以捕捉整个屏幕.活动窗口.选定区域等,图像输出方式多样 主要包括文件.剪贴板.和邮件.软件具有设置捕捉前延时.自定义捕捉热键.图像文件自动按时间 或模板命名.捕捉成功声音提示.预览捕捉图片.图像打印

获取像素密度、屏幕高度、状态栏、标题栏,屏幕截图

演示效果 奇酷1080P 华为1080P 华为720P 屏幕像素参数相关信息表格    像素密度  每英寸像素数  分辨率      分辨率别称      默认图标大小 xxhdpi   3        480   1080*1920   1080P    144*144   重点关注 xhdpi    2        320    720*1280     720P      96*96      适配基准 hdpi     1.5      240    480*800      WVG

winform屏幕截图

屏幕截图是一个比较常用的功能,在项目中出现的比例也比较高,至少我做过的每个项目都有屏幕截图这个功能,从全屏截图到区域截图都有出现过.当然区域截图已然包含了全屏截图. 全屏截图方式有好几种,调用API截图.调用操作系统的截图然后到剪切板去取(当然这种方式几乎没人会去用).用Graphics去画屏幕等等. 下面上Graphics画屏幕的代码,毕竟这种方式代码量最少. ? 1 2 3 4 5 //截取屏幕 Bitmap myImage = new Bitmap(Screen.PrimaryScreen

C#屏幕截图

今天通过C#来实现一个简单的屏幕截图功能.实现思路,获取鼠标按下去的位置和鼠标左键释放的位置,计算这个区域的宽度和高度.然后通过 Graphics.CopyFromScreen 方法便可以获取到屏幕截图了. 下面通过C#+WPF来实现这个功能,代码如下: XAML: <Window x:Class="WPFCaptureScreenShot.CaptureWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xam

cocos2dx[3.2](18)——屏幕截图ScreenShot

[唠叨] 屏幕截图有两种方式: (1)使用RenderTexture          :这是在2.x版本时最常用的的截图方法. (2)使用utils::captureScreen() :在3.2版本中新增的截图方法. [致谢] http://blog.csdn.net/ganpengjin1/article/details/19088921 http://cn.cocos2d-x.org/article/index?type=wiki&url=/doc/cocos-docs-master/ma

Java屏幕截图及剪切

Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能.这里只展示其屏幕截图. 截图的关键方法createScreenCapture(Rectangle rect) ,该方法需要一个Rectangle对象,Rectangle就是定义屏幕的一块矩形区域,构造Rectangle也相当容易: new Rectangle(int x, int y, int width, int height),四个参数分别是矩形左上角x坐标,矩形左上角y坐标,矩形宽度,矩形高度.截图方法返

asp.net获取屏幕截图、活动窗口截图

Rectangle R = System.Windows.Forms.Screen.PrimaryScreen.Bounds;//获取活动窗口截图 //Rectangle R = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;//获取整个屏幕截图 System.Drawing.Image img = new Bitmap(R.Width, R.Height); Graphics G = Graphics.FromImage(img);