十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
代码如下
创新互联建站是专业的淳安网站建设公司,淳安接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行淳安网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
[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("").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
有啊 核心提示:函数需要 uses Direct3D9,D3DX9; 偶然发现一个函数可以直接保存表面到文件1!所以修改了一下,函数为:...
procedure 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;
procedure 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实现的函数
procedure 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实现的抓屏
procedure 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;
通过上面这一步,我们可以很容易联想到用来抓取任何一个控件的表面图象,函数如下:
procedure 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;
在安卓ANDROID开发中,有些内容或文档是为防止知识产权,防信息外泄,需要做一些保护措施,Android中如何禁止用户截屏,防止截图java代码
在需要设置禁止截屏的Activity的onCreate()方法中调用setContentView()方法前添加一行代码即可:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
或:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
将以上代码,加入需要保护的ACTIVE中,以上 JAVA代码可以有效防止截图,截屏操作行为,这是一种很好的保护策略。
事实上,如果您想以Java实现网页截图,也就是“输入一段网址,几秒钟过后就能截取一张网页缩略图”的效果。那么,您至少有3种方式可以选择。
1、最直接的方式——使用Robot
方法详解:该方法利用Robat提供的强大桌面操作能力,硬性调用浏览器打开指定网页,并将网页信息保存到本地。
优势:简单易用,不需要任何第三方插件。
缺点:不能同时处理大量数据,技术含量过低,属于应急型技巧。
实现方法:使用如下代码即可。
[java] view plaincopy
public static void main(String[] args) throws MalformedURLException,
IOException, URISyntaxException, AWTException {
//此方法仅适用于JdK1.6及以上版本
Desktop.getDesktop().browse(
new URL("").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("google.jpg"));
}
2、最常规的方式——利用JNI,调用第三方C/C++组件
方法详解:目前来讲,Java领域对于网页截图组件的开发明显不足(商机?),当您需要完成此种操作时,算得上碰到了Java的软肋。但是,众所周知Java也拥有强大的JNI能力,可以轻易将C/C++开发的同类组件引为己用。不懂可以扣五七八零二四一四四
优势:实现简单,只需要封装对应的DLL文件,就可以让Java实现同类功能。
劣势:同其他JNI实现一样,在跨平台时存在隐患,而且您的程序将不再属于纯Java应用。
主要是利用java的几个先有的函数,如Robot这个类的一个方法createScreenCapture一个获得一个任意大小的屏幕图像(在这里是全屏图像),而所谓的截图就是在这个图像上画出一个矩形,再利用上面的方法获得这部分的图像,程序中的cf.setAlwaysOnTop(true)是必需的;看起来是在屏幕上截图,其实只是在一个在一个内镶有桌面背景的JFrame中截图。不知道还有没有其他的好方法~
附上代码:
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) {}
}
}