站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个简单的用JAVA实现的屏幕抓图(源代码)

一个简单的用JAVA实现的屏幕抓图(源代码)

摘要: 一个简单的用JAVA实现的屏幕抓图(源代码)
内容:  

主要使用的是java.util.Robot类来捕获屏幕,可以实现对屏幕一个矩形区域的捕获,通过这个类,我们也可以实现一个远程桌面控制的程序


<table style="TABLE-LAYOUT: fixed" cellSpacing=0 borderColorDark=#ffffff cellPadding=4 width="98%" align=center bgColor=#e6e6e6 borderColorLight=#009ace border=1>

<tr>
<td style="WORD-WRAP: break-word">


package com.qiu.util;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;


/**@Author Qiu_BaiChao
&nbsp;*一个简单的屏幕抓图
&nbsp;*
&nbsp;**/
&nbsp;
public class ScreenCapture {
&nbsp;//test main
&nbsp;public static void main(String[] args) throws Exception{
&nbsp;&nbsp;String userdir = System.getProperty("user.dir");
&nbsp;&nbsp;File tempFile = new File("d:","temp.png");
&nbsp;&nbsp;ScreenCapture capture = ScreenCapture.getInstance();
&nbsp;&nbsp;capture.captureImage();&nbsp;&nbsp;
&nbsp;&nbsp;JFrame frame = new JFrame();
&nbsp;&nbsp;JPanel panel = new JPanel();
&nbsp;&nbsp;panel.setLayout(new BorderLayout());
&nbsp;&nbsp;JLabel imagebox = new JLabel();
&nbsp;&nbsp;panel.add(BorderLayout.CENTER,imagebox);&nbsp;&nbsp;
&nbsp;&nbsp;imagebox.setIcon(capture.getPickedIcon());
&nbsp;&nbsp;capture.saveToFile(tempFile);
&nbsp;&nbsp;capture.captureImage();
&nbsp;&nbsp;imagebox.setIcon(capture.getPickedIcon());
&nbsp;&nbsp;frame.setContentPane(panel);
&nbsp;&nbsp;frame.setSize(400,300);
&nbsp;&nbsp;frame.show();
&nbsp;&nbsp;System.out.println("Over");
&nbsp;}
&nbsp;
&nbsp;private ScreenCapture() {
&nbsp;&nbsp;
&nbsp;&nbsp;try{
&nbsp;&nbsp;&nbsp;robot = new Robot();
&nbsp;&nbsp;}
&nbsp;&nbsp;catch(AWTException e) {
&nbsp;&nbsp;&nbsp;System.err.println("Internal Error: " + e);
&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;JPanel cp = (JPanel)dialog.getContentPane();
&nbsp;&nbsp;cp.setLayout(new BorderLayout());&nbsp;&nbsp;
&nbsp;&nbsp;labFullScreenImage.addMouseListener(new MouseAdapter() {
&nbsp;&nbsp;&nbsp;&nbsp;public void mouseReleased(MouseEvent evn) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isFirstPoint = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pickedImage = fullScreenImage.getSubimage(recX,recY,recW,recH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dialog.setVisible(false);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
&nbsp;&nbsp;&nbsp;&nbsp;public void mouseDragged(MouseEvent evn) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isFirstPoint) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x1 = evn.getX();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y1 = evn.getY();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isFirstPoint = false;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x2 = evn.getX();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y2 = evn.getY();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int maxX = Math.max(x1,x2);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int maxY = Math.max(y1,y2);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int minX = Math.min(x1,x2);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int minY = Math.min(y1,y2);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recX = minX;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recY = minY;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recW = maxX-minX;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recH = maxY-minY;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;labFullScreenImage.drawRectangle(recX,recY,recW,recH);&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;public void mouseMoved(MouseEvent e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;labFullScreenImage.drawCross(e.getX(),e.getY());
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;cp.add(BorderLayout.CENTER,labFullScreenImage);
&nbsp;&nbsp;dialog.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
&nbsp;&nbsp;dialog.setAlwaysOnTop(true);
&nbsp;&nbsp;dialog.setMaximumSize(
&nbsp;&nbsp;&nbsp;Toolkit.getDefaultToolkit().getScreenSize());
&nbsp;&nbsp;dialog.setUndecorated(true);
&nbsp;&nbsp;dialog.setSize(dialog.getMaximumSize());
&nbsp;&nbsp;dialog.setModal(true);
&nbsp;}
&nbsp;//Singleton Pattern
&nbsp;public static ScreenCapture getInstance() {
&nbsp;&nbsp;return defaultCapturer;
&nbsp;}
&nbsp;/**捕捉全屏慕*/
&nbsp;public Icon captureFullScreen() {
&nbsp;&nbsp;fullScreenImage = robot.createScreenCapture(new Rectangle(
&nbsp;&nbsp;&nbsp;Toolkit.getDefaultToolkit().getScreenSize()));
&nbsp;&nbsp;ImageIcon icon = new ImageIcon(fullScreenImage);
&nbsp;&nbsp;return icon;
&nbsp;}
&nbsp;/**捕捉屏幕的一个矫形区域
&nbsp; */
&nbsp;public void captureImage() {
&nbsp;&nbsp;fullScreenImage = robot.createScreenCapture(new Rectangle(
&nbsp;&nbsp;&nbsp;Toolkit.getDefaultToolkit().getScreenSize()));
&nbsp;&nbsp;ImageIcon icon = new ImageIcon(fullScreenImage);
&nbsp;&nbsp;labFullScreenImage.setIcon(icon);&nbsp;&nbsp;
&nbsp;&nbsp;dialog.setVisible(true);&nbsp;
&nbsp;}
&nbsp;/**得到捕捉后的BufferedImage*/&nbsp;
&nbsp;public BufferedImage getPickedImage() {
&nbsp;&nbsp;return pickedImage;
&nbsp;}
&nbsp;/**得到捕捉后的Icon*/&nbsp;
&nbsp;public ImageIcon getPickedIcon() {
&nbsp;&nbsp;return new ImageIcon(getPickedImage());
&nbsp;}
&nbsp;/**储存为一个文件,为PNG格式
&nbsp;
*@deprecated
&nbsp; *replaced by saveAsPNG(File file)
&nbsp; **/
&nbsp;@Deprecated
&nbsp;public void saveToFile(File file) throws IOException{
&nbsp;&nbsp;ImageIO.write(getPickedImage(),defaultImageFormater,file);
&nbsp;}
&nbsp;/**储存为一个文件,为PNG格式*/
&nbsp;public void saveAsPNG(File file) throws IOException {
&nbsp;&nbsp;ImageIO.write(getPickedImage(),"png",file);
&nbsp;}
&nbsp;/**储存为一个JPEG格式图像文件*/
&nbsp;public void saveAsJPEG(File file) throws IOException {
&nbsp;&nbsp;ImageIO.write(getPickedImage(),"JPEG",file);
&nbsp;}
&nbsp;
&nbsp;/**写入一个OutputStream*/
&nbsp;public void write(OutputStream out) throws IOException{
&nbsp;&nbsp;ImageIO.write(getPickedImage(),defaultImageFormater,out);
&nbsp;}
&nbsp;
&nbsp;//singleton design pattern
&nbsp;private static ScreenCapture defaultCapturer = new ScreenCapture();&nbsp;
&nbsp;private int x1,y1,x2,y2;
&nbsp;private int recX,recY,recH,recW;&nbsp;//截取的图像
&nbsp;private boolean isFirstPoint&nbsp; = true;
&nbsp;private BackgroundImage labFullScreenImage = new BackgroundImage();
&nbsp;private Robot robot;
&nbsp;private BufferedImage fullScreenImage;
&nbsp;private BufferedImage pickedImage;
&nbsp;private String defaultImageFormater = "png";
&nbsp;private JDialog dialog = new JDialog();
}


/**显示图片的Label*/
class BackgroundImage extends JLabel{
&nbsp;public void paintComponent(Graphics g) {
&nbsp;&nbsp;super.paintComponent(g);
&nbsp;&nbsp;g.drawRect(x,y,w,h);
&nbsp;&nbsp;String area = Integer.toString(w)+" * "+ Integer.toString(h);
&nbsp;&nbsp;g.drawString(area,x+(int)w/2-15,y+(int)h/2);
&nbsp;&nbsp;g.drawLine(lineX,0,lineX,getHeight());
&nbsp;&nbsp;g.drawLine(0,lineY,getWidth(),lineY);
&nbsp;}
&nbsp;
&nbsp;public void drawRectangle(int x,int y,int width,int height) {
&nbsp;&nbsp;this.x = x;
&nbsp;&nbsp;this.y = y;
&nbsp;&nbsp;h = height;
&nbsp;&nbsp;w = width;
&nbsp;&nbsp;repaint();
&nbsp;}
&nbsp;
&nbsp;public void drawCross(int x,int y) {
&nbsp;&nbsp;lineX = x;
&nbsp;&nbsp;lineY = y;
&nbsp;&nbsp;repaint();
&nbsp;}
&nbsp;
&nbsp;int lineX,lineY;
&nbsp;int x,y,h,w;
}


</td></tr></table>

来自Blog:blog.csdn.net/badqiu

Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd &nbsp;

主要使用的是java.util.Robot类来捕获屏幕,可以实现对屏幕一个矩形区域的捕获,通过
↑返回目录
前一篇: 菜鸟初学Java的备忘录(一)
后一篇: 你该学什么程序语言?