以下是我写的java截屏小程序,不过我还有点小疑问
以下是我写的java截屏小程序,不过我还有点小疑问
package com.hbyooge.cutScreen;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CutScreen extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel jp;
private Point p0;
private Point p1;
private Image Background;
private Point mousePress;
private Point mouseRelease;
private Graphics g;
// 构造方法
public CutScreen(){
jp = new JPanel();
this.getContentPane().add(jp);
initCutScreen();
// 窗口关闭事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 鼠标点击事件
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
mouseRelease = new Point(e.getXOnScreen(), e.getYOnScreen());
System.out.println("mouseReleased = " + e.getID());
System.out.println("mouseReleased = " + e.getPoint());
// 截图
// 获取整个屏幕图像
Image cut = null;
try {
Robot r = new Robot();
cut = r.createScreenCapture(new Rectangle(mousePress.x, mousePress.y, mouseRelease.x-mousePress.x, mouseRelease.y-mousePress.y));
} catch (Exception ex) {
// e.printStackTrace();
}
// int width = mouseRelease.x-mousePress.x;
// int height = mouseRelease.y-mousePress.y;
//BufferedImage bb = ((BufferedImage)cut);
//jp.paint(cut.getGraphics());
// JLabel pp = new JLabel(new ImageIcon(cut));
// pp.setSize(width, height);
//
// //Rectangle rect = pp.getBounds();
// BufferedImage bufImage = new BufferedImage(width,
// height,
// BufferedImage.TYPE_INT_RGB);
//
//
// g = bufImage.getGraphics();
// g.translate(-width, -height);
// pp.paint(g);
// g.dispose();
try {
// 问题出在此处,一下是问题描述
//如果没有此try-catch,且你将此程序Run As Java Application,你不会得到你想要的结果(即,正确截图)
// 如果没有此try-catch,且你在setClipboardImage(cut)之前设置断点,然后Debug As Java Application,你会得到正确结果
// 如果有此try-catch,则能得出正确结果。(如果你不能得到正确结果,请将sleep的中的数值增大)
//
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 将cut复制到剪贴板
setClipboardImage(cut);
// 释放
mousePress = new Point();
mouseRelease = new Point();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
mousePress = new Point(e.getXOnScreen(), e.getYOnScreen());
System.out.println("mousePressed = " + e.getID());
System.out.println("mousePressed = " + e.getPoint());
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
});
// 鼠标拖拽事件
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// System.out.println("mouseMoved = " + e.getID());
// System.out.println("mouseMoved = " + e.getPoint());
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseDragged=" + e.getID());
System.out.println("mouseDragged=" + e.getPoint());
//if(isPressing){
g.setColor(Color.black);
// 本想画矩形框的,但是没有画出来
g.drawRect(mousePress.x, mousePress.y, e.getXOnScreen()-mousePress.x, e.getYOnScreen()-mousePress.y);
//CutScreen.this.setVisible(true);
//}
CutScreen.this.repaint();
}
});
}
// 初始化
public void initCutScreen(){
mousePress = new Point();
mouseRelease = new Point();
Background = getFullScreen();
//Point screenSize = getScreenSize();
//this.setSize(screenSize.x, screenSize.y);
this.setSize(600, 600);
this.setVisible(true);
}
/**
* 获取屏幕,image格式
*/
public Image getFullScreen(){
// 获取整个屏幕图像
Point screen = getScreenSize();
try {
Robot r = new Robot();
Background = r.createScreenCapture(new Rectangle(0, 0, screen.x, screen.y));
} catch (Exception e) {
// e.printStackTrace();
Background = null;
}
return Background;
}
/**
* 获取屏幕大小
* @return
*/
public Point getScreenSize(){
Dimension dm = null;
try {
Toolkit tk = Toolkit.getDefaultToolkit();
dm = tk.getScreenSize();
} catch (Exception e) {
// e.printStackTrace();
}
return new Point((int)dm.getWidth(),(int)dm.getHeight());
}
/**
* 绘画窗口
*/
public void paint(Graphics g) {
this.g = g;
p0 = this.getLocationOnScreen();
p1 = new Point(-p0.x, -p0.y);
g.drawImage(Background, p1.x, p1.y, null);
repaint();
}
public static void main(String[] args) {
new CutScreen();
}
/**
* 把截图复制到系统剪贴板
* @param image
*/
protected void setClipboardImage(final Image image) {
Transferable trans = new Transferable(){
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(isDataFlavorSupported(flavor))
return image;
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
}
}
截图操作:在窗体内,按下鼠标左键不放,拖拽鼠标,松开鼠标左键。
问题描述已经在代码注释中写清楚了,求高手解答。
为什么try--catch处会出现这样的问题,为什么即时复制到剪贴板不行,而延迟复制到剪贴板就可以? java 截屏 截图 截屏 截图 --------------------编程问答-------------------- --------------------编程问答-------------------- 或许是需要时间才能获取到
截取图片需要时间
---猜测 --------------------编程问答-------------------- 你的问题应该是线程问题,但是我试了下,在我的机子上没有遇到你说的问题,我的JDK是 1.6.0_37,你用的JDK版本是哪个? --------------------编程问答-------------------- 还发现你的代码有个问题:
cut = r.createScreenCapture(new Rectangle(mousePress.x, mousePress.y, mouseRelease.x-mousePress.x, mouseRelease.y-mousePress.y));
主要截取的区域的宽度和高度不能是负值。
--------------------编程问答--------------------
这个跟时间没有关系吧,我是释放鼠标的时候才开始截图的 --------------------编程问答--------------------
我的也是1.6的 --------------------编程问答--------------------
这个……不应该吧。三楼都可以运行,我也可以运行 --------------------编程问答-------------------- 你可以参考这里 - http://blog.csdn.net/jia20003/article/details/7656728
补充:Java , Java SE