java實(shí)現(xiàn)區(qū)域內(nèi)屏幕截圖示例
這是一個(gè)java版的截圖程序
package com.hongyuan.test;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
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.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JToolBar;
import javax.swing.JWindow;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
public class ScreenShotTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
ScreenShotWindow ssw=new ScreenShotWindow();
ssw.setVisible(true);
} catch (AWTException e) {
e.printStackTrace();
}
}
});
}
}
/*
* 截圖窗口
*/
class ScreenShotWindow extends JWindow
{
private int orgx, orgy, endx, endy;
private BufferedImage image=null;
private BufferedImage tempImage=null;
private BufferedImage saveImage=null;
private ToolsWindow tools=null;
public ScreenShotWindow() throws AWTException{
//獲取屏幕尺寸
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(0, 0, d.width, d.height);
//截取屏幕
Robot robot = new Robot();
image = robot.createScreenCapture(new Rectangle(0, 0, d.width,d.height));
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
//鼠標(biāo)松開(kāi)時(shí)記錄結(jié)束點(diǎn)坐標(biāo),并隱藏操作窗口
orgx = e.getX();
orgy = e.getY();
if(tools!=null){
tools.setVisible(false);
}
}
@Override
public void mouseReleased(MouseEvent e) {
//鼠標(biāo)松開(kāi)時(shí),顯示操作窗口
if(tools==null){
tools=new ToolsWindow(ScreenShotWindow.this,e.getX(),e.getY());
}else{
tools.setLocation(e.getX(),e.getY());
}
tools.setVisible(true);
tools.toFront();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
//鼠標(biāo)拖動(dòng)時(shí),記錄坐標(biāo)并重繪窗口
endx = e.getX();
endy = e.getY();
//臨時(shí)圖像,用于緩沖屏幕區(qū)域放置屏幕閃爍
Image tempImage2=createImage(ScreenShotWindow.this.getWidth(),ScreenShotWindow.this.getHeight());
Graphics g =tempImage2.getGraphics();
g.drawImage(tempImage, 0, 0, null);
int x = Math.min(orgx, endx);
int y = Math.min(orgy, endy);
int width = Math.abs(endx - orgx)+1;
int height = Math.abs(endy - orgy)+1;
// 加上1防止width或height0
g.setColor(Color.BLUE);
g.drawRect(x-1, y-1, width+1, height+1);
//減1加1都了防止圖片矩形框覆蓋掉
saveImage = image.getSubimage(x, y, width, height);
g.drawImage(saveImage, x, y, null);
ScreenShotWindow.this.getGraphics().drawImage(tempImage2,0,0,ScreenShotWindow.this);
}
});
}
@Override
public void paint(Graphics g) {
RescaleOp ro = new RescaleOp(0.8f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
}
//保存圖像到文件
public void saveImage() throws IOException {
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("保存");
//文件過(guò)濾器,用戶過(guò)濾可選擇文件
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
jfc.setFileFilter(filter);
//初始化一個(gè)默認(rèn)文件(此文件會(huì)生成到桌面上)
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String fileName = sdf.format(new Date());
File filePath = FileSystemView.getFileSystemView().getHomeDirectory();
File defaultFile = new File(filePath + File.separator + fileName + ".jpg");
jfc.setSelectedFile(defaultFile);
int flag = jfc.showSaveDialog(this);
if(flag==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
String path=file.getPath();
//檢查文件后綴,放置用戶忘記輸入后綴或者輸入不正確的后綴
if(!(path.endsWith(".jpg")||path.endsWith(".JPG"))){
path+=".jpg";
}
//寫(xiě)入文件
ImageIO.write(saveImage,"jpg",new File(path));
System.exit(0);
}
}
}
/*
* 操作窗口
*/
class ToolsWindow extends JWindow
{
private ScreenShotWindow parent;
public ToolsWindow(ScreenShotWindow parent,int x,int y) {
this.parent=parent;
this.init();
this.setLocation(x, y);
this.pack();
this.setVisible(true);
}
private void init(){
this.setLayout(new BorderLayout());
JToolBar toolBar=new JToolBar("Java 截圖");
//保存按鈕
JButton saveButton=new JButton(new ImageIcon("images/save.gif"));
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
parent.saveImage();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
toolBar.add(saveButton);
//關(guān)閉按鈕
JButton closeButton=new JButton(new ImageIcon("images/close.gif"));
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
toolBar.add(closeButton);
this.add(toolBar,BorderLayout.NORTH);
}
}

- java利用phantomjs進(jìn)行截圖實(shí)例教程
- java實(shí)現(xiàn)pdf文件截圖的方法【附PDFRenderer.jar下載】
- Java實(shí)現(xiàn)對(duì)視頻進(jìn)行截圖的方法【附ffmpeg下載】
- spring(java,js,html) 截圖上傳圖片實(shí)例詳解
- java編程實(shí)現(xiàn)屏幕截圖(截屏)代碼總結(jié)
- Java模擬QQ桌面截圖功能實(shí)現(xiàn)方法
- Java實(shí)現(xiàn)屏幕截圖及剪裁
- java代碼實(shí)現(xiàn)截圖功能(屏幕截圖)
- 使用Java實(shí)現(xiàn)系統(tǒng)托盤功能的介紹(附源碼以及截圖)
- java實(shí)現(xiàn)攝像頭截圖功能
相關(guān)文章
IDEA中沒(méi)有Mapper.xml模板選項(xiàng)的處理方法
這篇文章主要介紹了IDEA中沒(méi)有Mapper.xml模板選項(xiàng)的處理方法,需其實(shí)解決方法很簡(jiǎn)單,只需要在idea中導(dǎo)入模板即可,本文圖文的形式給大家分享解決方法,需要的朋友可以參考下2021-04-04springboot實(shí)現(xiàn)啟動(dòng)直接訪問(wèn)項(xiàng)目地址
這篇文章主要介紹了springboot實(shí)現(xiàn)啟動(dòng)直接訪問(wèn)項(xiàng)目地址,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java自定義實(shí)現(xiàn)base64編碼轉(zhuǎn)換
本文主要介紹了java 自定義實(shí)現(xiàn)base64編碼轉(zhuǎn)換的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法
這篇文章主要介紹了在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法,CRUD是指在做計(jì)算處理時(shí)的增加(Create)、重新取得數(shù)據(jù)(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫(xiě),需要的朋友可以參考下2016-04-04mybatis類型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密
這篇文章主要介紹了mybatis類型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09