图形截图java代码下载 图形截图java代码下载安装
java 截图程序
你把框以外的图片或图像的不透明度alpha值(0-127)设到大概50左右就暗了,而且会显出底下的内容。 框本身不透明度是0,就是完全透明。
成都创新互联公司2013年至今,是专业互联网技术服务公司,拥有项目网站制作、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元古雷港做网站,已为上家服务,为古雷港各地企业和个人服务,联系电话:18980820575
也就是Color(int r, int g, int b, int a) 最后一个a的值。
用JAVA怎么做截屏工具?
哎~要是你给我个分就好了!我前几天刚刚做的!你拿去吧!
/*
作者:泡沫
地址:
功能:用于截取图片,方便快捷!
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].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].xpoint[2].x point[1].ypoint[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(changeWidth0){
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].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].xpoint[2].x point[1].ypoint[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截取图片
呵呵,很明确的告诉你:可以!
代码半小时后出来!!!
……
终于出来了(呵呵,好像超过了半小时哈)且看代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
public class Test extends JApplet{
String addrs="F:\\images\\mm.bmp";//改成自己的图片路径
BufferedImage mm,child;
CutImage ci;
public Test(){
try {
mm=ImageIO.read(new File(addrs));
} catch (IOException e) {
System.out.println("图片读取失败!");
e.printStackTrace();
}
ci=new CutImage(mm);
child=ci.getChildImage(50, 0, 150, 220);
}
public void init(){
}
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("原图:",0,10);
g.drawImage(mm, 20,10,this);
g.drawString("ci.getChildImage(50, 0, 150, 220)截取后的图片",mm.getWidth()+30,10);
g.drawImage(child, mm.getWidth()+50,20,this);
}
}
import java.awt.Image;
import java.awt.image.BufferedImage;
public class CutImage {
private BufferedImage img;
private BufferedImage child;
public CutImage(){
}
public CutImage(BufferedImage im){
img=im;
}
public CutImage(Image im){
img=(BufferedImage)im;
}
public void setImg(BufferedImage img) {
this.img = img;
}
public BufferedImage getChildImage(int x,int y,int width,int height) {
int cw=width;
int ch=height;
int pw=img.getWidth();
int ph=img.getHeight();
if(pwx+width){
System.out.println("给出的参数超出原图片的范围!程序会自动减小宽度或高度");
cw=pw-x;
}
if(phy+height){
System.out.println("给出的参数超出原图片的范围!程序会自动减小宽度或高度");
ch=ph-y;
}
child=new BufferedImage(cw,ch,BufferedImage.TYPE_INT_ARGB );
for(int i=0;ich;i++){
for(int j=0;jcw;j++){
child.setRGB(j, i, img.getRGB(x+j, y+i));
}
}
return child;
}
}
呵呵,希望楼主能够满意哦,如果你愿意的话,稍微改一下代码就可以把截取的图片child报春到你的电脑上了。下面程序的运行效果吧!
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应用。
实现方法:可参见此用例,具体封装何种C/C++组件请自行选择。
PS:示例来源于ACA HTML to Image Converter项目( ),这是一个收费的HTML转Image第三方组件,但封装方式在Java中大同小异。
引用JNI封装:
[java] view plaincopy
import sun.awt.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.peer.*;
public class Snap
{
static
{
System.loadLibrary("Snap");
}
public static void main( String[] argv )
{
Snap t_xSnap = new Snap();
t_xSnap.Start("", "snapshot-google.png");
}
public native void Start(String pi_strURL, String pi_strImageName);
}
CPP部分的实现:
[java] view plaincopy
#include windows.h
#include atlbase.h
#include "snap.h"
#pragma comment(lib,"atl.lib")
#import "./../../acawebthumb.dll" no_namespace
JNIEXPORT void JNICALL Java_Snap_Start(JNIEnv *pEnv, jobject, jstring pi_strUrl, jstring pi_strFileName)
{
CoInitialize(0);
_bstr_t t_strUrl = pEnv-GetStringUTFChars(pi_strUrl, 0);
_bstr_t t_strFileName = pEnv-GetStringUTFChars(pi_strFileName, 0);
IThumbMakerPtr HTML_Converter = NULL;
HRESULT hr = HTML_Converter.CreateInstance(L"ACAWebThumb.ThumbMaker");
if (SUCCEEDED(hr))
{
HTML_Converter-SetURL(t_strUrl);
if ( 0 == HTML_Converter-StartSnap() )
HTML_Converter-SaveImage(t_strFileName);
}
if (HTML_Converter)
HTML_Converter.Release();
CoUninitialize();
}
以该组件图像化yahoo界面的效果图:
3、最扎实的方法——自行解析HTML标记,并将其图像化
方法详解:众所周知,HTML之所以在浏览器中以具体的网页格式出现,并非服务器端传了一整个应用到客户端,而是源自于浏览器对于客户端自行解析的结果。因此,只要我们将对应的解析一一实现,那么将网页图形化,就将不是什么难事。
优势:纯Java实现,一劳永逸,一旦开发完成则永远通用,而且有一定的商用价值。
劣势:开发费时,且需要针对不同语法做精确分析,才能保证输出的基本正确。尤其在涉及到JavaScript解析时,难度将尤其增大。
实现方法:目前尚无具体案例可供参考。但是,由于Java有jdic之类的浏览器项目存在(),而Java图形界面又属绘制生成。从理论上说,我们可以将所有具备Graphics的组件图形化保存。
而如果自行解析,那么您需要建立HTML解析器(或使用第三方的,万幸Java在这方面的组件很多),了解Java2D机制,了解何时该使用drawString绘制文字,何时又该使用drawImage插入图片等等。
谢谢采纳!
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代码获取 这个网页上的图片
你也没说是要把图片下载下来还是什么的
这里我实现的是将图片下载到电脑
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DownLoad {
public static void main(String[] args) {
File dstFile = new File("E:/test/test/test.jpg");
try {
URL url = new URL("");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
saveFile(is, dstFile);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveFile(InputStream is,File dstFile){
FileOutputStream fos = null;
File parentFile = dstFile.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
try {
fos = new FileOutputStream(dstFile);
byte[] buff = new byte[1024 * 4];
int len;
while((len = is.read(buff)) != -1){
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(is != null){
is.close();
}
if(fos != null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
名称栏目:图形截图java代码下载 图形截图java代码下载安装
链接地址:http://ybzwz.com/article/dodshsi.html