欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)區(qū)域內(nèi)屏幕截圖示例

 更新時(shí)間:2014年04月14日 06:40:24   作者:  
這篇文章主要介紹了java截圖示例,需要的朋友可以參考下

這是一個(gè)java版的截圖程序

復(fù)制代碼 代碼如下:

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);
 }
}



相關(guān)文章

  • IDEA中沒(méi)有Mapper.xml模板選項(xiàng)的處理方法

    IDEA中沒(méi)有Mapper.xml模板選項(xiàng)的處理方法

    這篇文章主要介紹了IDEA中沒(méi)有Mapper.xml模板選項(xiàng)的處理方法,需其實(shí)解決方法很簡(jiǎn)單,只需要在idea中導(dǎo)入模板即可,本文圖文的形式給大家分享解決方法,需要的朋友可以參考下
    2021-04-04
  • Java自學(xué)書(shū)籍Top 10

    Java自學(xué)書(shū)籍Top 10

    這篇文章主要為大家推薦了Java書(shū)籍Top 10,是由Java Inside推薦的十本不錯(cuò)的Java書(shū)籍,感興趣的小伙伴們可以參考一下
    2016-09-09
  • springboot實(shí)現(xiàn)啟動(dòng)直接訪問(wèn)項(xiàng)目地址

    springboot實(shí)現(xiàn)啟動(dòng)直接訪問(wèn)項(xiàng)目地址

    這篇文章主要介紹了springboot實(shí)現(xiàn)啟動(dòng)直接訪問(wèn)項(xiàng)目地址,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java線程組與未處理異常實(shí)例分析

    Java線程組與未處理異常實(shí)例分析

    這篇文章主要介紹了Java線程組與未處理異常,結(jié)合實(shí)例形式分析了java線程組處理異常的相關(guān)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • java自定義實(shí)現(xiàn)base64編碼轉(zhuǎn)換

    java自定義實(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操作的方法

    這篇文章主要介紹了在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法,CRUD是指在做計(jì)算處理時(shí)的增加(Create)、重新取得數(shù)據(jù)(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫(xiě),需要的朋友可以參考下
    2016-04-04
  • 淺談Mybatis #和$區(qū)別以及原理

    淺談Mybatis #和$區(qū)別以及原理

    這篇文章主要介紹了淺談Mybatis #和$區(qū)別以及原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java8接口的默認(rèn)方法

    Java8接口的默認(rèn)方法

    這篇文章主要為大家介紹了Java8接口的默認(rèn)方法,還為大家默認(rèn)方法的多重繼承,感興趣的朋友可以參考一下
    2016-01-01
  • Java你告訴我 fail-fast 是什么鬼

    Java你告訴我 fail-fast 是什么鬼

    這篇文章主要介紹了Java你告訴我 fail-fast 是什么鬼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • mybatis類型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密

    mybatis類型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密

    這篇文章主要介紹了mybatis類型轉(zhuǎn)換器如何實(shí)現(xiàn)數(shù)據(jù)加解密,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論