java 截屏 类似于 QQ截屏

package com.sun.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) {
//鼠标松开时记录结束点坐标,并隐藏操作窗口
orgx = e.getX();
orgy = e.getY();

if(tools!=null){
tools.setVisible(false);
}
}
@Override
public void mouseReleased(MouseEvent e) {
//鼠标松开时,显示操作窗口
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) {
//鼠标拖动时,记录坐标并重绘窗口
endx = e.getX();
endy = e.getY();

//临时图像,用于缓冲屏幕区域放置屏幕闪烁
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("保存");

//文件过滤器,用户过滤可选择文件
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
jfc.setFileFilter(filter);

//初始化一个默认文件(此文件会生成到桌面上)
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";
}
//写入文件
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);

//关闭按钮
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);
}

}

时间: 2024-08-06 10:47:44

java 截屏 类似于 QQ截屏的相关文章

Java邮件发送与屏幕截屏

前几天七夕情人节孤独寂寞的程序猿闲来没事,花了一两个小时写了个小Demo主要实现Java的Mail发送功能和桌面截屏功能. 首先让我们先看看Java sendMail邮件发送和桌面屏幕截屏功能是怎么实现的基础知识. 一.Java  SendMail邮件发送 首先让我们来看看邮件发送的原理图: JavaMail 是一套sun 提供开发邮件收发程序API,JavaMail编写程序就是邮件客户端程序(和outlook.foxmail功能类似) * JavaMail开发需要类库 javamail API

Android系统自带录屏(动态截屏)功能

 Android系统(Android 4.4 或以上)自带有录屏(动态截屏)功能,基于命令行. 具体方法是: adb shell screenrecord /sdcard/myrecord.mp4 这段命令在控制台执行后,Android系统将立即对设备的屏幕录制屏幕的视频.adb shell screenrecord 是执行录屏:sdcard/myrecord.mp4 是视频文件存放的目录,录制后的视频文件存放在sdcard的根目录下,myrecord.mp4即是视频文件. Ctrl + C

iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)

iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏) 2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2 1. 截取屏幕尺寸大小的图片并保存至相册 保存至相册只需将方法saveImage中的代码替换即可 UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0.0); [self.view.layer renderInContext:UIGraphicsGetCur

java 模拟触屏版QQ空间上传图片

模拟触屏版QQ空间上传图片问题. 第一步:上传图片. http://up.qzone.com/cgi-bin/upload/cgi_upload_pic_v2 参数: picture:[图片的Base64编码] base64:1 hd_height:480[图片高度] hd_width:320[图片宽度] hd_quality:96[图片质量,好像只有70&96 ,数字大是高质量] output_type:json preupload:1 charset:utf-8 output_charset

模仿QQ截图片

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.OleDb; 9 using System.Xml; 10 namespace Test 11 { 1

仿QQ黑屏、锁屏、程序切换等手势密码

仿QQ黑屏.锁屏.程序切换等手势密码 仿九宫格绘制手势锁屏,设置手势时,两次一样才成功,黑屏后即锁屏,需绘制手势解锁 下载地址:http://www.devstore.cn/code/info/1056.html 运行截图:     版权声明:本文为博主原创文章,未经博主允许不得转载.

类似于QQ的右滑删除效果的实现方法

类似于QQ的右滑删除效果的实现方法 原理:删除的div在窗口的外面,用户看不到,用户右滑,显示次div ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 6

一个关于如何创建类似于QQ客户端聊天窗口的模拟小项目

对于不久之前学习到的一个有关的类似于QQ聊天框的模拟项目,对其中涉及到的知识在这里做一下总结. 首先,你要先创建一个客户端聊天框(取名为:ChatClient,它是你创建的类),这个类继承了Frame.而这个Frame是java.awt里的一个子类,它是带有标题和边框的顶层窗口,它里面有很多方法(具体查手册).下面是一段示例代码: 1 import java.awt.*; 2 3 public class ChatClient extends Frame{//你创建的类继承了Frame 4 5

android开发:全屏和退出全屏

android开发:全屏和退出全屏 from://http://blog.csdn.net/dyllove98/article/details/8831933 2013-04-21 20:31 413人阅读 评论(0) 收藏 举报 xml代码: <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_conten