❶ 用java怎麼做截屏工具

哎~要是你給我個分就好了!我前幾天剛剛做的!你拿去吧!

/*
作者:泡沫
地址:http://hi..com/av51
功能:用於截取圖片,方便快捷!
mail:yuhuidog#163.com (注意:其中#為@)
*/
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
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.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AWTpicture extends Frame implements MouseListener,MouseMotionListener,ActionListener{

private int firstX,firstY,frameWidth,frameHeight;
private int firstWith,firstHeight,firstPointx,firstPointy;
private BufferedImage bi,sbi,original;
private Robot robot;
private Rectangle rectangle;
private Rectangle rectangleCursor,rectangleCursorUp,rectangleCursorDown,rectangleCursorLeft,rectangleCursorRight;
private Rectangle rectangleCursorRU,rectangleCursorRD,rectangleCursorLU,rectangleCursorLD;
private Image bis;
private Dimension dimension;
private Button button,button2,clearButton;
private Point[] point=new Point[3];
private int width,height;
private int nPoints=5;
private Panel panel;
private boolean drawHasFinish=false,change=false;
private int changeFirstPointX,changeFirstPointY,changeWidth,changeHeight;
private boolean changeUP=false,changeDOWN=false,changeLEFT=false,changeRIGHT=false,changeRU=false,changeRD=false,changeLU=false,changeLD=false;
private boolean clearPicture=false,redraw=false;
private FileDialog fileDialog;
private AWTpicture(){
//取得屏幕大小
dimension=Toolkit.getDefaultToolkit().getScreenSize();
frameWidth=dimension.width;
frameHeight=dimension.height;

fileDialog=new FileDialog(this,"泡沫截圖",FileDialog.SAVE);
rectangle=new Rectangle(frameWidth,frameHeight);
panel=new Panel();
button=new Button("退出");
button.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.setBackground(Color.green);
button2=new Button("截取");
button2.setBackground(Color.darkGray);
button2.addActionListener(new MyTakePicture(this));
button2.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.addActionListener(this);
clearButton=new Button("重繪");
clearButton.setBackground(Color.green);
clearButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
clearButton.addActionListener(new MyClearPicture(this));
panel.setLayout(new BorderLayout());
panel.add(clearButton, BorderLayout.SOUTH);

panel.add(button, BorderLayout.NORTH);
panel.add(button2, BorderLayout.CENTER);
try {
robot=new Robot();
} catch (AWTException e) {

e.printStackTrace();
}

//截取全屏
bi=robot.createScreenCapture(rectangle);
original=bi;
this.setSize(frameWidth,frameHeight);
this.setUndecorated(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.add(panel,BorderLayout.EAST);
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
this.setVisible(true);
this.repaint();
}
public static void main(String[] args){
new AWTpicture();
}
public void paint(Graphics g) {

this.drawR(g);

}

//緩存圖片
public void update(Graphics g){
if(bis==null){
bis=this.createImage(frameWidth, frameHeight);
}
Graphics ga=bis.getGraphics();
Color c=ga.getColor();
ga.setColor(Color.black);
ga.fillRect(0, 0, frameWidth, frameHeight);
ga.setColor(c);
paint(ga);
g.drawImage(bis, 0, 0, frameWidth, frameHeight, null);
}
public void mouseClicked(MouseEvent e) {

}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}
public void mouseReleased(MouseEvent e) {
if(!drawHasFinish){
if(point[1].x<point[2].x && point[1].y<point[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].x>point[2].x && point[1].y<point[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].x<point[2].x && point[1].y>point[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].x>point[2].x && point[1].y>point[2].y){
firstPointx=point[2].x;
firstPointy=point[2].y;
}
changeFirstPointX=firstPointx;
changeFirstPointY=firstPointy;
if(point[1]!=null && point[2]!=null ){
rectangleCursorUp=new Rectangle(firstPointx+20,firstPointy-10,width-40,20);
rectangleCursorDown=new Rectangle(firstPointx+20,firstPointy+height-10,width-40,20);
rectangleCursorLeft=new Rectangle(firstPointx-10,firstPointy+10,20,height-20);
rectangleCursorRight=new Rectangle(firstPointx+width-10,firstPointy+10,20,height-20);
rectangleCursorLU=new Rectangle(firstPointx-10,firstPointy-10,30,20);
rectangleCursorLD=new Rectangle(firstPointx-10,firstPointy+height-10,30,20);
rectangleCursorRU=new Rectangle(firstPointx+width-10,firstPointy-10,20,20);
rectangleCursorRD=new Rectangle(firstPointx+width-10,firstPointy+height-10,20,20);
drawHasFinish=true;
}

}
//確定每邊能改變大小的矩形
if(drawHasFinish){
rectangleCursorUp=new Rectangle(changeFirstPointX+20,changeFirstPointY-10,changeWidth-40,20);
rectangleCursorDown=new Rectangle(changeFirstPointX+20,changeFirstPointY+changeHeight-10,changeWidth-40,20);
rectangleCursorLeft=new Rectangle(changeFirstPointX-10,changeFirstPointY+10,20,changeHeight-20);
rectangleCursorRight=new Rectangle(changeFirstPointX+changeWidth-10,changeFirstPointY+10,20,changeHeight-20);
rectangleCursorLU=new Rectangle(changeFirstPointX-2,changeFirstPointY-2,10,10);
rectangleCursorLD=new Rectangle(changeFirstPointX-2,changeFirstPointY+changeHeight-2,10,10);
rectangleCursorRU=new Rectangle(changeFirstPointX+changeWidth-2,changeFirstPointY-2,10,10);
rectangleCursorRD=new Rectangle(changeFirstPointX+changeWidth-2,changeFirstPointY+changeHeight-2,10,10);
}

}
public void mouseDragged(MouseEvent e) {
point[2]=e.getPoint();
//if(!drawHasFinish){
this.repaint();
// }

//托動滑鼠移動大小
if(change){
if(changeUP){
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointY=e.getPoint().y;

}
if(changeDOWN){
changeHeight=e.getPoint().y-changeFirstPointY;
}
if(changeLEFT){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeFirstPointX=e.getPoint().x;
}
if(changeRIGHT){
changeWidth=e.getPoint().x-changeFirstPointX;
}
if(changeLU){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointX=e.getPoint().x;
changeFirstPointY=e.getPoint().y;
}
if(changeLD){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeHeight=e.getPoint().y-changeFirstPointY;
changeFirstPointX=e.getPoint().x;

}
if(changeRU){
changeWidth=e.getPoint().x-changeFirstPointX;
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointY=e.getPoint().y;
}
if(changeRD){
changeWidth=e.getPoint().x-changeFirstPointX;
changeHeight=e.getPoint().y-changeFirstPointY;

}
this.repaint();
}

}
public void mouseMoved(MouseEvent e) {
point[1]=e.getPoint();
//改變滑鼠的形狀
if(rectangleCursorUp!=null && rectangleCursorUp.contains(point[1])){

this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
change=true;
changeUP=true;
}else if(rectangleCursorDown!=null && rectangleCursorDown.contains(point[1])){
this.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
change=true;
changeDOWN=true;
}else if(rectangleCursorLeft!=null && rectangleCursorLeft.contains(point[1])){
this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
change=true;
changeLEFT=true;
}else if(rectangleCursorRight!=null && rectangleCursorRight.contains(point[1]) ){
this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
change=true;
changeRIGHT=true;
}else if(rectangleCursorLU !=null && rectangleCursorLU.contains(point[1])){
this.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
change=true;
changeLU=true;
}else if(rectangleCursorLD !=null && rectangleCursorLD.contains(point[1])){
this.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
change=true;
changeLD=true;
}else if(rectangleCursorRU!=null && rectangleCursorRU.contains(point[1])){
this.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
change=true;
changeRU=true;
}else if(rectangleCursorRD!=null && rectangleCursorRD.contains(point[1])){
this.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
change=true;
changeRD=true;
}else{
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
changeUP=false;changeDOWN=false;changeRIGHT=false;changeLEFT=false;changeRU=false;
changeRD=false;changeLU=false;changeLD=false;
}
redraw=false;
}
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);

}
class MyTakePicture implements ActionListener{
AWTpicture aWTpicture;
MyTakePicture(AWTpicture aWTpicture){
this.aWTpicture=aWTpicture;
}
//保存圖片
public void actionPerformed(ActionEvent e) {
fileDialog.setVisible(true);
if(changeWidth>0){
sbi=bi.getSubimage(changeFirstPointX,changeFirstPointY,changeWidth,changeHeight);

File file=new File(fileDialog.getDirectory());
file.mkdir();

try {
ImageIO.write(sbi, "jpeg",new File(file,fileDialog.getFile()+".jpg") );
} catch (IOException e1) {

e1.printStackTrace();
}
}

}

}
class MyClearPicture implements ActionListener{
AWTpicture aWTpicture;
MyClearPicture(AWTpicture aWTpicture){
this.aWTpicture=aWTpicture;
}

public void actionPerformed(ActionEvent e) {
drawHasFinish=false;
change=false;
redraw=true;
rectangleCursorUp=null;
rectangleCursorDown=null;
rectangleCursorLeft=null;
rectangleCursorRight=null;
rectangleCursorRU=null;
rectangleCursorRD=null;
rectangleCursorLU=null;
rectangleCursorLD=null;
changeWidth=0;
changeHeight=0;

aWTpicture.repaint();

}

}
public void drawR(Graphics g){
g.drawImage(bi, 0,0,frameWidth,frameHeight, null);

if(point[1]!=null &&point[2]!=null &&!drawHasFinish && !redraw){
int[] xPoints={point[1].x,point[2].x,point[2].x,point[1].x,point[1].x};
int[] yPoints={point[1].y,point[1].y,point[2].y,point[2].y,point[1].y};
width=(point[2].x-point[1].x)>0?(point[2].x-point[1].x):(point[1].x-point[2].x);
height=(point[2].y-point[1].y)>0?(point[2].y-point[1].y):(point[1].y-point[2].y);
changeWidth=width;
changeHeight=height;
Color c=g.getColor();
g.setColor(Color.red);
g.drawString(width+"*"+height, point[1].x, point[1].y-5);
//畫點
/*int i;
if()*/
if(point[1].x<point[2].x && point[1].y<point[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].x>point[2].x && point[1].y<point[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].x<point[2].x && point[1].y>point[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].x>point[2].x && point[1].y>point[2].y){
firstPointx=point[2].x;
firstPointy=point[2].y;
}

g.fillRect(firstPointx-2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+(width)/2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+width-2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+width-2,firstPointy+ height/2-2, 5,5);
g.fillRect(firstPointx+width-2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx+(width)/2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx-2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx-2,firstPointy+ height/2-2, 5,5);
//畫矩形
//g.drawString("fafda", point[1].x-100, point[1].y-5);
g.drawPolyline(xPoints, yPoints, nPoints);

}

if(change){
g.setColor(Color.red);
g.drawString(changeWidth+"*"+changeHeight, changeFirstPointX, changeFirstPointY-5);

g.fillRect(changeFirstPointX-2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+(changeWidth)/2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY+ changeHeight/2-2, 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX+(changeWidth)/2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX-2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX-2,changeFirstPointY+ changeHeight/2-2, 5,5);

g.drawRect(changeFirstPointX, changeFirstPointY, changeWidth, changeHeight);
}
}

}

❷ JAVA 截屏函數

有啊 核心提示:函數需要 uses Direct3D9,D3DX9; 偶然發現一個函數可以直接保存表面到文件1!所以修改了一下,函數為:...
procere CaptureScreen(Const FileName: string);
var
BitsPerPixel: Byte;
pD3D: IDirect3D9;
pSurface: IDirect3DSurface9;
g_pD3DDevice: IDirect3DDevice9;
D3DPP: TD3DPresentParameters;
ARect: TRect;
LockedRect: TD3DLockedRect;
BMP: TBitmap;
i, p: Integer;
begin
BitsPerPixel := GetDeviceCaps(Canvas.Handle, BITSPIXEL);
FillChar(d3dpp, SizeOf(d3dpp), 0);
D3DPP.Windowed := True;
D3DPP.Flags := D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
D3DPP.SwapEffect := D3DSWAPEFFECT_DISCARD;
D3DPP.BackBufferWidth := Screen.Width;
D3DPP.BackBufferHeight := Screen.Height;
D3DPP.BackBufferFormat := D3DFMT_X8R8G8B8;
pD3D := Direct3DCreate9(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, @D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateOffscreenPlainSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, pSurface, nil);
g_pD3DDevice.GetFrontBufferData(0, pSurface);
// use D3D to save surface. Notes: D3DX%ab.dll is required!
// D3DXSaveSurfaceToFile('Desktop.bmp', D3DXIFF_BMP, pSurface, nil, nil);
// use Bitmap to save surface
ARect := Screen.DesktopRect;
pSurface.LockRect(LockedRect, @ARect, D3DLOCK_NO_DIRTY_UPDATE or D3DLOCK_NOSYSLOCK or D3DLOCK_READONLY);
BMP := TBitmap.Create;
BMP.Width := Screen.Width;
BMP.Height := Screen.Height;
case BitsPerPixel of
8: BMP.PixelFormat := pf8bit;
16: BMP.PixelFormat := pf16bit;
24: BMP.PixelFormat := pf24bit;
32: BMP.PixelFormat := pf32bit;
end;
p := Cardinal(LockedRect.pBits);
for i := 0 to Screen.Height - 1 do
begin
CopyMemory(BMP.ScanLine[i], Ptr(p), Screen.Width * BitsPerPixel div 8);
p := p + LockedRect.Pitch;
end;
BMP.SaveToFile(FileName);
BMP.Free;
pSurface.UnlockRect;
end;
procere CaptureScreen(const FileName: string;Const FileType: TD3DXImageFileFormat);
var
pD3D: IDirect3D9;
pSurface: IDirect3DSurface9;
g_pD3DDevice: IDirect3DDevice9;
D3DPP: TD3DPresentParameters;
begin
FillChar(d3dpp, SizeOf(d3dpp), 0);
D3DPP.Windowed := True;
D3DPP.Flags := D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
D3DPP.SwapEffect := D3DSWAPEFFECT_DISCARD;
D3DPP.BackBufferWidth := Screen.Width;
D3DPP.BackBufferHeight := Screen.Height;
D3DPP.BackBufferFormat := D3DFMT_X8R8G8B8;
pD3D := Direct3DCreate9(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, @D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateOffscreenPlainSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, pSurface, nil);
g_pD3DDevice.GetFrontBufferData(0, pSurface);
D3DX9.D3DXSaveSurfaceToFile(PChar(FileName),FileType,pSurface,nil,nil);
pSurface := nil;
g_pD3DDevice := nil;
pD3D := nil;
end;
傳遞兩個參數,第一個參數指定為文件名,第二個參數指定為文件類型
支持7種類型
_D3DXIMAGE_FILEFORMAT = (
D3DXIFF_BMP {= 0},
D3DXIFF_JPG {= 1},
D3DXIFF_TGA {= 2},
D3DXIFF_PNG {= 3},
D3DXIFF_DDS {= 4},
D3DXIFF_PPM {= 5},
D3DXIFF_DIB {= 6}
);
再給一個使用dX8實現的,上面給的是用Dx9實現的函數
procere CaptureScreen(const FileName: string;Const FileType: TD3DXImageFileFormat);
var
pD3D: IDirect3D8;
pSurface: IDirect3DSurface8;
g_pD3DDevice: IDirect3DDevice8;
D3DPP: TD3DPresentParameters;
begin
FillChar(d3dpp, SizeOf(d3dpp), 0);
D3DPP.Windowed := True;
D3DPP.Flags := D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
D3DPP.SwapEffect := D3DSWAPEFFECT_DISCARD;
D3DPP.BackBufferWidth := Screen.Width;
D3DPP.BackBufferHeight := Screen.Height;
D3DPP.BackBufferFormat := D3DFMT_X8R8G8B8;
pD3D := Direct3DCreate8(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateImageSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, pSurface);
g_pD3DDevice.GetFrontBuffer(pSurface);
D3DX8.D3DXSaveSurfaceToFile(PChar(FileName),FileType,pSurface,nil,nil);
pSurface := nil;
g_pD3DDevice := nil;
pD3D := nil;
end;
再給一個直接使用系統GDI實現的抓屏
procere CaptureScreen(const FileName: string);overload;
var
nScreenWidth,nScreenHeight: integer;
ScreenDc,hCaptureDC: HDC;
bmp: TBitmap;
begin
nScreenWidth := GetSystemMetrics(SM_CXSCREEN);
nScreenHeight := GetSystemMetrics(SM_CYSCREEN);
ScreenDc := GetDC(GetDesktopWindow);
hCaptureDC := CreateCompatibleDC(ScreenDc);
bmp := TBitmap.Create;
bmp.Handle :=CreateCompatibleBitmap(ScreenDc,nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,bmp.Handle);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,ScreenDc,0,0,SRCCOPY);
bmp.SaveToFile(fileName);
ReleaseDC(GetDesktopWindow,ScreenDc);
DeleteDC(hCaptureDC);
bmp.Free;
end;
通過上面這一步,我們可以很容易聯想到用來抓取任何一個控制項的表面圖象,函數如下:
procere CaptureControl(Control: TWinControl;const FileName: string);overload;
var
ControlWidth,ControlHeight: integer;
ControlDc,hCaptureDC: HDC;
bmp: TBitmap;
begin
ControlWidth := Control.ClientWidth;
ControlHeight := Control.ClientHeight;
ControlDc := GetDC(Control.Handle);
hCaptureDC := CreateCompatibleDC(ControlDc);
bmp := TBitmap.Create;
bmp.Handle :=CreateCompatibleBitmap(ControlDc,ControlWidth,ControlHeight);
SelectObject(hCaptureDC,bmp.Handle);
BitBlt(hCaptureDC,0,0,ControlWidth,ControlHeight,ControlDc,0,0,SRCCOPY);
bmp.SaveToFile(fileName);
ReleaseDC(GetDesktopWindow,ControlDc);
DeleteDC(hCaptureDC);
bmp.Free;
end;

❸ java 怎麼實現網頁截圖

代碼如下

[java]
<span style="font-size:18px;">package com.util;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.imageio.ImageIO;

public class CutPicture {
public static void main(String[] args) throws Exception,
IOException, URISyntaxException, AWTException {
// 此方法僅適用於JdK1.6及以上版本
Desktop.getDesktop().browse(new URL("http://java.662p.com/").toURI());
Robot robot = new Robot();
robot.delay(10000);
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
int width = (int) d.getWidth();
int height = (int) d.getHeight();
// 最大化瀏覽器
robot.keyRelease(KeyEvent.VK_F11);
robot.delay(2000);
Image image = robot.createScreenCapture(new Rectangle(0, 0, width,height));
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
// 保存圖片
ImageIO.write(bi, "jpg", new File("c:/open.jpg"));

}
}</span>

❹ java 實現截圖並且 保存在本地

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;

/**
* java截屏
* 運行後將當前屏幕截取,並最大化顯示。
* 拖拽滑鼠,選擇自己需要的部分。
* 按Esc鍵保存圖片到桌面,並退出程序。
* 點擊右上角(沒有可見的按鈕),退出程序,不保存圖片。
*
* @author JinCeon
*/
public class SnapshotTest {
public static void main(String[] args) {
// 全屏運行
RectD rd = new RectD();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
gd.setFullScreenWindow(rd);
}
}

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);
int width = Math.abs(endx - orgx)+1;
int height = Math.abs(endy - orgy)+1;
// 加上1,防止width或height為0
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();
}
}
}

❺ Java如何實現截圖功能

下面給你介紹將全屏幕截圖保存到指定目錄下的代碼,希望能對你有幫助:
核心代碼為:snapShot方法中的相關邏輯
package Jietu;
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 CameraTest {
private String filePreStr; // 默認前綴(選擇存儲路徑例如: D:\\)
private String defName = "cameraImg"; // 默認截圖名稱
static int serialNum = 0; //截圖名稱後面的數字累加
private String imageFormat; // 圖像文件的格式
private String defaultImageFormat = "png"; //截圖後綴
Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //獲取全屏幕的寬高尺寸等數據
public CameraTest() {
filePreStr = defName;
imageFormat = defaultImageFormat;
}
public CameraTest(String s, String format) {
filePreStr = 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 = filePreStr + String.valueOf(serialNum) + "." + imageFormat;
File f = new File(name);
System.out.print("Save File " + name);
// 將screenshot對象寫入圖像文件
ImageIO.write(screenshot, imageFormat, f);
System.out.print("..Finished!\n");
} catch (Exception ex) {
System.out.println(ex);
}
}
// 運行之後,即可將全屏幕截圖保存到指定的目錄下面<br> // 配合前端頁面上面的選擇尺寸等邏輯,傳到後台,即可實現自由選擇截圖區域和大小的截圖<br>
public static void main(String[] args) {
CameraTest cam = new CameraTest("d:\\Hello", "png");//
cam.snapShot();
}
}

❻ 簡單的Java截屏功能

一點分值都沒有,真心難以下筆。。。

❼ java實現QQ截圖

一般的截圖,是沒有問題的。參考

http://wenku..com/view/1abf3c5cbe23482fb4da4c6b.html

❽ java 截屏程序,該怎麼處理

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class sa extends JFrame {

private static final long serialVersionUID = -267804510087895906L;

private JButton button = null;

private JLabel imgLabel = null;

public sa() {
button = new JButton("模擬屏幕(點右鍵退出)");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
new ScreenWindow(imgLabel);
} catch (Exception e1) {
JOptionPane.showConfirmDialog(null, "出現意外錯誤!", "系統提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
}
});
JPanel pane = new JPanel();
pane.setBackground(Color.WHITE);
imgLabel = new JLabel();
pane.add(imgLabel);
JScrollPane spane = new JScrollPane(pane);
this.getContentPane().add(button, BorderLayout.NORTH);
this.getContentPane().add(spane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}

public static void main(String[] args) {
new sa();
}
}

class ScreenWindow extends JFrame {

private static final long serialVersionUID = -3758062802950480258L;

private boolean isDrag = false;

private int x = 0;

private int y = 0;

private int xEnd = 0;

private int yEnd = 0;

public ScreenWindow(final JLabel imgLabel) throws AWTException, InterruptedException {
Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0, 0, screenDims.width, screenDims.height)));
label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
dispose();
}
}

public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}

public void mouseReleased(MouseEvent e) {
if (isDrag) {
xEnd = e.getX();
yEnd = e.getY();
if(x > xEnd){
int temp = x;
x = xEnd;
xEnd = temp;
}
if(y > yEnd){
int temp = y;
y = yEnd;
yEnd = temp;
}
try {
imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x, y, xEnd - x, yEnd - y)));
} catch (Exception ex) {
JOptionPane.showConfirmDialog(null, "出現意外錯誤!", "系統提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
dispose();
}
}
});
label.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
if(!isDrag)
isDrag = true;
}

public void mouseMoved(MouseEvent e) {
/** 拖動過程的虛線選取框需自己實現 */
}
});
this.setUndecorated(true);
this.getContentPane().add(label);
this.setSize(screenDims.width, screenDims.height);
this.setVisible(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}

class ScreenImage {

public static Image getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {
Robot robot = new Robot();
Image screen = robot.createScreenCapture(new Rectangle(x, y, w, h)).getScaledInstance(w, h, Image.SCALE_SMOOTH);
MediaTracker tracker = new MediaTracker(new Label());
tracker.addImage(screen, 1);
tracker.waitForID(0);
return screen;
}
}

❾ java截屏有哪幾種方法

前台方式
1. 利用html2canvas實現前台截屏
html2canvas項目的gitHub地址
html2canvas示例
html2canvas可以將HTML代碼塊進行截取,並生成快照形式的canvas,然後可以利用html5的下載功能提供圖片的下載介面,供用戶下載。
優點:前台技術,實現比較容易。
缺點:此項目使用了html5的技術,因此在對IE的兼容上,只可以在IE9+的版本上使用。
使用時,需要引用jQuery.js和html2canvas.js。(html2canvas.js可以在github上下載)
具體JavaScript代碼如下:
var canvas = function () {
html2canvas($("#chart"), {
onrendered: function (canvas) {
$("#class11").html(canvas);//將id為「class11」部分的代碼轉換為canvas
var type = 'png';//圖片類型為png
var imgData = canvas.toDataURL('png');//將圖片轉換為png類型的流

var _fixType = function (type) {
type = type.toLowerCase().replace(/jpg/i, 'jpeg'); var r = type.match(/png|jpeg|bmp|gif/)[0]; return 'image/' + r;
}; var saveFile = function (data, filename) {
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename; var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
}; //下載時的文件名
var filename = 'fe_' + (new Date()).getTime() + '.' + type; // 下載文件
saveFile(imgData, filename);
}
});

後台方式(java)
1. 利用html2image實現
html2image的地址(google)
html2image是可以識別html標簽並將html轉換成圖片的java項目。
優點:後台轉換,故對瀏覽器的版本基本沒有要求。
缺點:對樣式的識別不是很好,轉換出來的圖片比較簡單,基本沒有可以兼容的樣式。
使用方式:maven中引用如下依賴:
<dependency>
<groupId>com.github.xuwei-k</groupId>
<artifactId>html2image</artifactId>
<version>0.1.0</version></dependency>12345

2. 利用DJNativeSwing項目實現
純java代碼實現。使用DJNativeSwing這個項目,構建一個swing瀏覽器,打開網址,之後截取圖片。(需要eclipse的swt庫支持)
優點:純java實現。
缺點:需要多線程的支持,只在控制台方式實現過(由於本人對於多線程的掌握較弱,目前沒有在spring項目中實現這個方法,求大神指導~~)
3. 利用phantomJs實現
phantomJs的GitHub地址
phantomJs官網
phantom利用的是webKit內核,全面支持web而不需瀏覽器支持,快速,原生支持各種Web標准。
優點:速度快,使用簡單。
缺點:需要在項目的伺服器端引用exe文件進行截圖,並發性能有待考證。

❿ java手機可以截屏么

每個牌子可能不一樣,而且你沒說手機牌子,有些手機系統不帶截屏功能需要手機安軟體才可以截屏。
所以最好還是網上找個截屏軟體安裝下吧