java编程实现屏幕截图(截屏)代码总结本⽂实例总结了常见的java编程实现屏幕截图⽅法。分享给⼤家供⼤家参考,具体如下:
⽅法⼀:
import java.awt.Desktop;
import java.awt.Dimension;
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 CaptureScreen {
public static void captureScreen(String fileName, String folder) throws Exception {
Dimension screenSize = DefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = ateScreenCapture(screenRectangle);
//保存路径
File screenFile = new File(fileName);
if (!ists()) {
screenFile.mkdir();
}
File f = new File(screenFile, folder);
ImageIO.write(image, "png", f);
//⾃动打开
if (Desktop.isDesktopSupported()
&& Desktop().isSupported(Desktop.Action.OPEN))
}
public static void main(String[] args) {
try {
captureScreen("e:\\你好","11.png");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⽅法⼆:
package com.qiu.util;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
/**
* ⼀个简单的屏幕抓图
*
**/
public class ScreenCapture {
// test main
public static void main(String[] args) throws Exception {
java爱心代码编程简单String userdir = Property("user.dir");
File tempFile = new File("d:", "temp.png");
ScreenCapture capture = Instance();
capture.captureImage();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel imagebox = new JLabel();
panel.add(BorderLayout.CENTER, imagebox);
imagebox.PickedIcon());
capture.saveToFile(tempFile);
capture.captureImage();
imagebox.PickedIcon());
frame.setContentPane(panel);
frame.setSize(400, 300);
frame.show();
System.out.println("Over");
}
private ScreenCapture() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
JPanel cp = (JPanel) ContentPane();
cp.setLayout(new BorderLayout());
labFullScreenImage.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evn) {
isFirstPoint = true;
pickedImage = Subimage(recX, recY, recW,
recH);
dialog.setVisible(false);
}
});
labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evn) {
if (isFirstPoint) {
x1 = X();
y1 = Y();
isFirstPoint = false;
} else {
x2 = X();
y2 = Y();
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.X(), e.getY());
}
});
cp.add(BorderLayout.CENTER, labFullScreenImage);
dialog.PredefinedCursor(Cursor.CROSSHAIR_CURSOR)); dialog.setAlwaysOnTop(true);
dialog.DefaultToolkit().getScreenSize());
dialog.setUndecorated(true);
dialog.MaximumSize());
dialog.setModal(true);
}
// Singleton Pattern
public static ScreenCapture getInstance() {
return defaultCapturer;
}
/** 捕捉全屏慕 */
public Icon captureFullScreen() {
fullScreenImage = ateScreenCapture(new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize()));
ImageIcon icon = new ImageIcon(fullScreenImage);
return icon;
}
/** 捕捉屏幕的⼀个矫形区域 */
public void captureImage() {
fullScreenImage = ateScreenCapture(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 ScreenCapture defaultCapturer = new ScreenCapture();
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 = String(w) + " * " + String(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;
}
⽅法三:
因为有最⼩化到系统托盘,还是需要⼀张名为bg.gif作为托盘图标,图⽚应放在同级⽬录下,否则会空指针异常。主要的截屏代码部分是:
Robot ro=new Robot();
Toolkit DefaultToolkit();
Dimension ScreenSize();
Rectangle rec=new Rectangle(0,0,di.width,di.height);
BufferedImage ateScreenCapture(rec);
(源代码来源于CSDN)
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
public class CaptureScreen extends JFrame implements ActionListener{
private JButton start,cancel,saveAll;
private JPanel c;
private BufferedImage get;
private JTabbedPane jtp;//⼀个放置很多份图⽚
private int index;//⼀个⼀直会递增的索引,⽤于标认图⽚
private JRadioButton java,system;//JAVA界⾯,系统界⾯
/** Creates a new instance of CaptureScreen */
public CaptureScreen() {
super("屏幕截取软件(第三版)");
try{
UIManager.SystemLookAndFeelClassName());
}catch(Exception exe){
exe.printStackTrace();
}
initWindow();
initOther();
initTrayIcon();
}
private void initOther(){
jtp=new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT); }
private void initWindow(){
start=new JButton("开始截取");
saveAll=new JButton("保存所有");
cancel=new JButton("退出");
start.addActionListener(this);
saveAll.addActionListener(this);
cancel.addActionListener(this);
JPanel buttonJP=new JPanel();
c=new JPanel(new BorderLayout());
c.setBackground(Color.BLACK);
JLabel jl=new JLabel("屏幕截取",JLabel.CENTER);
jl.setFont(new Font("⿊体",Font.BOLD,40));
jl.setForeground(Color.RED);
c.add(jl,BorderLayout.CENTER);
buttonJP.add(start);
buttonJP.add(saveAll);
buttonJP.add(cancel);
buttonJP.ateTitledBorder("公共操作区"));
JPanel jp=new JPanel();//放两个单选按钮的⾯板
jp.add(java=new JRadioButton("java界⾯"));
jp.add(system=new JRadioButton("系统界⾯",true));
java.addActionListener(this);
system.addActionListener(this);
jp.ateTitledBorder("界⾯风格"));
ButtonGroup bg=new ButtonGroup();
bg.add(java);
bg.add(system);
JPanel all=new JPanel();
all.add(jp);
all.add(buttonJP);
this.setSize(500,400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
CaptureScreen.this.setVisible(false);
}
});
}
private void initTrayIcon(){
try{
SystemTray SystemTray();
Image Class().getResource("bg.gif"));
PopupMenu pm=new PopupMenu("弹出菜单");
pm.add(new MenuItem("关于")).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(CaptureScreen.this,"<html><Font color=red><center><h2>关于</h2></center></Font>" + "这是⼀款纯JAVA的屏幕截取程序<br>在以前的基础上增加了⼀些常⽤的功能,<br>" +
"⽐如,批量保存,多幅截取,复制到系统粘帖板...<br>" +
"在使⽤过程中有任何问题,欢迎联系.<br>" +
"<Font size=5 color=blue>作者:千⾥冰封<br>" +
"<i>QQ:24325142</i><br></Font></html>");
}
});
pm.addSeparator();
pm.add(new MenuItem("显⽰主窗⼝")).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
CaptureScreen.this.setVisible(true);
}
});
pm.add(new MenuItem("开始截取")).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
doStart();
}
});
pm.add(new MenuItem("退出程序")).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
}
});
TrayIcon ti=new TrayIcon(im,"JAVA屏幕截取",pm);
st.add(ti);
ti.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
CaptureScreen.this.setVisible(true);
}
});
}catch(Exception exe){
exe.printStackTrace();
}
}
private void updates(){
this.setVisible(true);
if(get!=null){
//如果索引是0,则表⽰⼀张图⽚都没有被加⼊过,
/
/则要清除当前的东西,重新把tabpane放进来
if(index==0){
c.add(jtp,BorderLayout.CENTER);
}else{//否则的话,直接对tabpane添加⾯板就可以了
//就什么都不⽤做了
}
PicPanel pic=new PicPanel(get);
jtp.addTab("图⽚"+(++index),pic);
jtp.setSelectedComponent(pic);
SwingUtilities.updateComponentTreeUI(c);
}
}
private void doStart(){
try{
this.setVisible(false);
Thread.sleep(500);//睡500毫秒是为了让主窗完全不见
Robot ro=new Robot();
Toolkit DefaultToolkit();
Dimension ScreenSize();
Rectangle rec=new Rectangle(0,0,di.width,di.height);
BufferedImage ateScreenCapture(rec);
JFrame jf=new JFrame();
Temp temp=new Temp(jf,bi,di.width,di.height);
jf.setUndecorated(true);
jf.setSize(di);
jf.setVisible(true);
jf.setAlwaysOnTop(true);
} catch(Exception exe){
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论