`
ruantongsheng
  • 浏览: 21141 次
  • 来自: ...
社区版块
存档分类
最新评论

screen capture (hot key+system tray)

 
阅读更多
calss1=====================
package com.screenCapture;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

public class HookKeyTest extends JFrame implements HotkeyListener {
public static final String sourcePath="C:/screenCaptureDll/";//JIntellitype.dll";
private final SystemTray sysTray = SystemTray.getSystemTray();
private final int identifier=10000;
private final TrayIcon trayIcon = new TrayIcon(new ImageIcon(sourcePath+"sc.PNG").getImage(),"ScreenCapture",getPopMenu());
public HookKeyTest() {
this.setTitle("ScreenCapture");
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
unInstallHotKey();
System.out.println("closing");

}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try {
//JIntellitype
setSize(new Dimension(330, 220));

JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);

JButton btnRegCtrlalta = new JButton("reg Ctrl+Alt+A");
btnRegCtrlalta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
panel.add(btnRegCtrlalta);

JButton btnNewButton = new JButton("unreg Ctrl+Alt+A");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
panel.add(btnNewButton);
installHotKey();
//setVisible(true);
sysTray.add(trayIcon);
} catch (Exception e) {
e.printStackTrace();
}

}

@Override
public void onHotKey(int identifier) {
System.out.println("indentifier==="+identifier);
if(identifier==this.identifier&&!ScreenCapture.isCapture){
new ScreenCapture();
}
}

public static void main(String[] args) {
try {
//if(!isStartUp())
{
new HookKeyTest();
}
//else
{
// JOptionPane.showMessageDialog(null, "An Instance is running!!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public PopupMenu getPopMenu()
{
PopupMenu popupMenu = new PopupMenu();// 弹出菜单  
        //MenuItem mi = new MenuItem("open");  
        MenuItem exit = new MenuItem("exit"); 
        popupMenu.add(exit);  
        exit.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            unInstallHotKey();
                System.exit(0);   
            }  
        });
        return popupMenu;
}

private void installHotKey(){
JIntellitype t=JIntellitype.getInstance();
t.getInstance().addHotKeyListener(this);
t.registerHotKey(identifier,JIntellitype.MOD_CONTROL+JIntellitype.MOD_ALT,'A');
System.out.println("install hot key!");
}
private void unInstallHotKey(){
JIntellitype t=JIntellitype.getInstance();
t.unregisterHotKey(identifier);
System.out.println("unInstall hot key!");
}
private static boolean isStartUp(){
JIntellitype t=JIntellitype.getInstance();
return t.checkInstanceAlreadyRunning("ScreenCapture");
}
}







calss2=====================


package com.screenCapture;

import java.awt.AWTEvent;

public class ScreenCapture extends JDialog{
public static boolean isCapture=false;
ImageLabel imgLabel;
int xStart=0;
int yStart=0;
int subimageX=0;
int subimageY=0;
int subimageW=0;
int subimageH=0;
private BufferedImage fullScreenImage=null;
public static void main(String[] args) {
ScreenCapture sc=null;
try {
Thread.sleep(300);
sc=new ScreenCapture();

} catch (Exception e) {
JOptionPane.showMessageDialog(sc, e.getMessage());
e.printStackTrace();
}
}
public ScreenCapture() {
isCapture=true;
setUndecorated(true);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setAlwaysOnTop(true);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setModal(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
JPanel imgPanel = new JPanel();
getContentPane().add(imgPanel, BorderLayout.CENTER);
imgPanel.setLayout(new BorderLayout(0, 0));

  imgLabel = new ImageLabel();
imgPanel.add(imgLabel, BorderLayout.CENTER);

//===============
imgLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent paramMouseEvent) {
xStart=paramMouseEvent.getX();
yStart=paramMouseEvent.getY();
}
public void mouseReleased(MouseEvent paramMouseEvent) {
System.out.println("DDDDDDD");
if(paramMouseEvent.getButton()==3){
//System.exit(0);
destroyMe();
}
if(subimageY>0&&subimageX>0){
BufferedImage pickedImage = fullScreenImage.getSubimage(subimageX, subimageY, subimageW,
subimageH);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
MyImageTransferable it = new MyImageTransferable(new ImageIcon(pickedImage));
cb.setContents(it, it);
//System.exit(0);
destroyMe();
}
}
});
imgLabel.addMouseMotionListener(new MouseMotionListener() {

public void mouseMoved(MouseEvent paramMouseEvent) {
}
public void mouseDragged(MouseEvent paramMouseEvent) {
//paintRectOnImglabel(imgLabel.getGraphics(), paramMouseEvent.getX(),paramMouseEvent.getY());

int w=Math.abs(paramMouseEvent.getX()-xStart);
int h=Math.abs(paramMouseEvent.getY()-yStart);
int x=Math.min(xStart,paramMouseEvent.getX());
int y=Math.min(yStart,paramMouseEvent.getY());
subimageX=x;
subimageY=y;
subimageW=w;
subimageH=h;
imgLabel.drawRectangle(x, y, w, h);

}
});
setLabelIcon(imgLabel);
setVisible(true);
}
private void destroyMe(){
this.setVisible(false);
this.dispose();
isCapture=false;
}
private void setLabelIcon(JLabel label){
try {
if(fullScreenImage==null){
Robot robot =new Robot();
fullScreenImage = robot.createScreenCapture(new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize()));
}
ImageIcon icon = new ImageIcon(fullScreenImage);
label.setIcon(icon);
} catch (Exception e) {
e.printStackTrace();
}
}

}
class ImageLabel extends JLabel {
int lineX, lineY;
int x, y, h, w;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(x, y, w, h);
String area = Integer.toString(w) + " * " + Integer.toString(h);
g.setColor(Color.blue);
g.drawString(area, x + (int) w / 2 - 15, y + (int) h / 2);
}
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();
}
}
class MyImageTransferable implements Transferable,ClipboardOwner{

private ImageIcon image=null;
public MyImageTransferable(ImageIcon img) {
this.image=img;
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("lostOwnership");
}
public Object getTransferData(DataFlavor arg0)
throws UnsupportedFlavorException, IOException {
return this.image.getImage();
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor []flavor=new DataFlavor[1];
flavor[0]=DataFlavor.imageFlavor;
return flavor;
}
public boolean isDataFlavorSupported(DataFlavor arg0) {
if(arg0.equals(DataFlavor.imageFlavor)){
System.out.println("support impageFlavor");
return true;
}
return false;
}


}
class MyAWTEvent implements AWTEventListener{
@Override
public void eventDispatched(AWTEvent paramAWTEvent) {
System.out.println("MMMMMMMMMMMMSSSSSSSS");

}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics