java实现截图并保存到本地

时间:2022-11-01 12:14:47
1.java实现截图并保存到本地
提供给大家三个方法,随意整理的

2.方法一:

package com.credream.ocr;

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 GuiCamera {
private String fileName;
private String defaultName="GuiCamera";
static int serialNum=0;
private String imageFormat;//图像文件的格式
private String defaultImageFormat="jpg";
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();

public GuiCamera(){
fileName=defaultName;
imageFormat=defaultImageFormat;
}

public GuiCamera(String s,String format) {
fileName=s;
imageFormat=format;
}
/**
* 对屏幕进行拍照
*
* **/
public void snapshot(){
try {
//拷贝屏幕到一个BufferedImage对象screenshot
BufferedImage screenshot=(new Robot()).createScreenCapture(
new Rectangle(0,0,(int)d.getWidth(),(int)d.getHeight()));
serialNum++;
//根据文件前缀变量和文件格式变量,自动生成文件名
String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
System.out.println(name);
File f=new File(name);
System.out.println("Save File-"+name);
//将screenshot对象写入图像文件
ImageIO.write(screenshot, imageFormat, f);
System.out.println("..Finished");

} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
//"C:\\sally\\bootstrap栅格"是根据自己随意找的一个图片形式,"png"是图片的格式
GuiCamera cam=new GuiCamera("C:\\sally\\bootstrap栅格","png");
cam.snapshot();
}
}

----------------------------------------------------------------------------------------------------------------------------------------

3.方法二:

package com.credream.ocr;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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.JFrame;
import javax.swing.filechooser.FileSystemView;

class RectD extends JFrame{

private static final long serialVersionUID = 1L;
int orgx,orgy,endx,endy;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image;
BufferedImage tempImage;
BufferedImage saveImage;
Graphics g;

@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 RectD(){
snapshot();
setVisible(true);
//setSize(d);//最大化窗口
setDefaultCloseOperation(EXIT_ON_CLOSE);

this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
orgx=e.getX();
orgy=e.getY();
}
});
//鼠标运动监听器
this.addMouseMotionListener(new MouseMotionAdapter() {
//鼠标拖拽事件
public void mouseDragged(MouseEvent e) {
endx=e.getX();
endy=e.getY();
g=getGraphics();
g.drawImage(tempImage, 0, 0, RectD.this);
int x=Math.min(orgx, endx);
int y=Math.min(orgy,endy);
//加上1,防止width,height为0
int width=Math.abs(endx-orgx)+1;
int height=Math.abs(endy-orgy)+1;
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,RectD.this);
}
});

this.addKeyListener(new KeyAdapter() {
@Override
//按键释放
public void keyReleased(KeyEvent e){
//按Esc键退出
if(e.getKeyCode()==27){
saveToFile();
System.exit(0);
}
}
});
}
public void saveToFile(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyymmddHHmmss");
String name=sdf.format(new Date());
File path=FileSystemView.getFileSystemView().getHomeDirectory();
String format="jpg";
File f=new File(path+File.separator+name+"."+format);
try {
ImageIO.write(saveImage, format, f);
} catch (IOException e) {
e.printStackTrace();
}
}

public void snapshot(){

try {
Robot robot= new Robot();
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
image=robot.createScreenCapture(new Rectangle(0,0,d.width,d.height));

} catch (AWTException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
//全屏运行
RectD rd = new RectD();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
gd.setFullScreenWindow(rd);
}
}
----------------------------------------------------------------------------------------------------------------------------------------

4.方法三:

package com.credream.ocr;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class RobotExp {
public static void main(String[] args) {
try {
Robot robot=new Robot();
//根据指定的区域抓取屏幕的指定区域,1300是长度,800是宽度。
BufferedImage bi=robot.createScreenCapture(new Rectangle(1300,800));
//把抓取到的内容写到一个jpg文件中
ImageIO.write(bi, "jpg", new File("C:\\sally\\bootstrap栅格1.png"));

} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------

三种方法,大致相同,只不过有的加了一些额外的方法。