高级Java代码截图 java截屏代码( 三 )


附上代码:
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Camera {
/**
* @param args
*/
public static void main(String[] args) {
CameraJFrame cf=new CameraJFrame();
cf.setAlwaysOnTop(true);
cf.setUndecorated(true);
cf.setVisible(true);
}
}
class CameraJFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
Dimension di=Toolkit.getDefaultToolkit().getScreenSize();
public CameraJFrame()
{
setSize(di);
getContentPane().add(new CameraJPanel());
}
class CameraJPanel extends JPanel implements MouseListener,MouseMotionListener
{
/**
* flag主要是用来判别状态 。
* 文件的格式名是unname+数字编号,格式是png
*/
private static final long serialVersionUID = 1L;
BufferedImage bi,get;
int startx,starty,endx,endy;
int flag=1;
String filename="unname";
String fileformat="png";
int count=1;
public CameraJPanel()
{
try
{
Robot ro=new Robot();
bi=ro.createScreenCapture(new Rectangle(0,0,di.width,di.height));
}
catch(Exception e)
{
e.printStackTrace();
}
addMouseListener(this);
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{
g.drawImage(bi,0,0,di.width,di.height,this);
g.setColor(Color.red);
g.drawRect(startx, starty, endx-startx, endy-starty);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==MouseEvent.BUTTON3)
{
System.exit(0);
}
else if(e.getClickCount()==2)
{
try
{
Robot ro=new Robot();
get=ro.createScreenCapture(new Rectangle(startx,starty,endx-startx,endy-starty));
String name=filename+String.valueOf(count++)+"."+fileformat;
File f=new File(name);
ImageIO.write(get, fileformat, f);
}
catch(Exception ex)
{
ex.printStackTrace();
}
flag=1; //置flag为1,以便重新开始截图 。
startx=starty=endx=endy=0;
repaint();
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if(flag==1)
{
startx=e.getX();
starty=e.getY();
}
}
public void mouseReleased(MouseEvent e) {
flag=0;
}
public void mouseDragged(MouseEvent e) {
flag=1;
endx=e.getX();
endy=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {}
}
}
JAVA 如何实现对已有图片的部分截图import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Test95 {
// 图片源
private static final String SRC_FILE = "c://1.jpg";
// 目标图片
private static final String DEST_FILE = "c://2.jpg";
public void readUsingImageReader() throws Exception{
// 取得图片读入器
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
System.out.println(readers);
ImageReader reader = (ImageReader) readers.next();
System.out.println(reader);
// 取得图片读入流
InputStream source = new FileInputStream(SRC_FILE);
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);

推荐阅读