導航:首頁 > 編程語言 > java抓取圖片

java抓取圖片

發布時間:2022-10-07 00:46:30

A. java截取圖片

呵呵,很明確的告訴你:可以!

代碼半小時後出來!!!

……

終於出來了(呵呵,好像超過了半小時哈)且看代碼:

importjava.awt.Color;

importjava.awt.Graphics;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

importjavax.imageio.ImageIO;

importjavax.swing.JApplet;

publicclassTestextendsJApplet{

Stringaddrs="F:\images\mm.bmp";//改成自己的圖片路徑

BufferedImagemm,child;

CutImageci;

publicTest(){

try{

mm=ImageIO.read(newFile(addrs));

}catch(IOExceptione){

System.out.println("圖片讀取失敗!");

e.printStackTrace();

}

ci=newCutImage(mm);

child=ci.getChildImage(50,0,150,220);

}

publicvoidinit(){

}

publicvoidpaint(Graphicsg){

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);

}

}

importjava.awt.Image;

importjava.awt.image.BufferedImage;

publicclassCutImage{

privateBufferedImageimg;

privateBufferedImagechild;

publicCutImage(){

}

publicCutImage(BufferedImageim){

img=im;

}

publicCutImage(Imageim){

img=(BufferedImage)im;

}

publicvoidsetImg(BufferedImageimg){

this.img=img;

}

(intx,inty,intwidth,intheight){

intcw=width;

intch=height;

intpw=img.getWidth();

intph=img.getHeight();

if(pw<x+width){

System.out.println("給出的參數超出原圖片的范圍!程序會自動減小寬度或高度");

cw=pw-x;

}

if(ph<y+height){

System.out.println("給出的參數超出原圖片的范圍!程序會自動減小寬度或高度");

ch=ph-y;

}

child=newBufferedImage(cw,ch,BufferedImage.TYPE_INT_ARGB);

for(inti=0;i<ch;i++){

for(intj=0;j<cw;j++){

child.setRGB(j,i,img.getRGB(x+j,y+i));

}

}

returnchild;

}

}

呵呵,希望樓主能夠滿意哦,如果你願意的話,稍微改一下代碼就可以把截取的圖片child報春到你的電腦上了。下面程序的運行效果吧!

B. 用java怎麼讀取圖片

思路:使用 java.awt.Image包下的Image可以接收圖片。讀取則使用ImageIO對象。

代碼如下:

/**
* 讀取圖片,首先導入以下的包
*/
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;

/**
* 用Image對象來接收圖片
* 路徑根據實際情況修改
*/
Image image = ImageIO.read(new File("c:\\1.png"));
System.out.println(image.getSource());

C. Java爬蟲爬取圖片

httpclient +jsoup解決問題

D. 如何用java代碼獲取 這個網頁上的圖片

你也沒說是要把圖片下載下來還是什麼的

這里我實現的是將圖片下載到電腦

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
publicclassDownLoad{
publicstaticvoidmain(String[]args){
FiledstFile=newFile("E:/test/test/test.jpg");
try{
URLurl=newURL("http://mmbiz.qpic.cn/mmbiz/18oibvzQ4h2PROnwgdEIg/0");
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
InputStreamis=conn.getInputStream();
saveFile(is,dstFile);
}catch(MalformedURLExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidsaveFile(InputStreamis,FiledstFile){
FileOutputStreamfos=null;
FileparentFile=dstFile.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
try{
fos=newFileOutputStream(dstFile);
byte[]buff=newbyte[1024*4];
intlen;
while((len=is.read(buff))!=-1){
fos.write(buff,0,len);
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!=null){
is.close();
}
if(fos!=null){
fos.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}

E. java 編寫客戶端 獲取圖片

packagecom;

importjava.io.File;
importjava.util.Iterator;

importcom.drew.imaging.jpeg.JpegMetadataReader;
importcom.drew.imaging.jpeg.JpegProcessingException;
importcom.drew.metadata.Directory;
importcom.drew.metadata.Metadata;
importcom.drew.metadata.Tag;
importcom.drew.metadata.exif.ExifDirectory;
importcom.drew.metadata.exif.GpsDirectory;
importcom.drew.metadata.jpeg.JpegDirectory;


publicclassTest{
publicstaticvoidmain(String[]args){
FilejpegFile=newFile("F:/圖片定位/DCIM/P20130116_125539.jpg");
Metadatametadata;
try{
metadata=JpegMetadataReader.readMetadata(jpegFile);
Directoryexif=metadata.getDirectory(ExifDirectory.class);
Iteratortags1=exif.getTagIterator();
while(tags1.hasNext()){
Tagtag=(Tag)tags1.next();
System.out.println(tag);
}
Directoryjpeg=metadata.getDirectory(JpegDirectory.class);
Iteratortags2=jpeg.getTagIterator();
while(tags2.hasNext()){
Tagtag=(Tag)tags2.next();
System.out.println(tag);
}
Directorygps=metadata.getDirectory(GpsDirectory.class);
Iteratortags3=gps.getTagIterator();
while(tags3.hasNext()){
Tagtag=(Tag)tags3.next();
System.out.println(tag);
}
}catch(JpegProcessingExceptione){
e.printStackTrace();
}
}

}

F. 網路爬蟲 抓取圖片問題 Java

大部分網路抓圖都是網頁上帶的有圖片url的那種。高級的網路抓圖支持部分Javascript
,其實原理和抓取html頁面的一樣,解析並拼接javascript中的圖片地址,然後批量抓取。

G. 如何用java實現從顯示器上指定坐標抓取a*b像素的圖片

Java裡面有個ToolKit的現成的截屏的東西,截出來放在BufferImage里想咋處理都行哇,不過這取到的可能是全屏,在緩沖區里可以以位元組指定位置么,不過演算法要自己寫,應該也不會太難的,就是從一個大的位元組緩沖區中,一小段一小段的根據偏移點和結束點取一行一行的位元組,一個矩形區域可以由左上角一點到右下角一點決定,把這些位元組拼成一個小的圖像。要是不想自己寫演算法的話,網上一大堆呢,不過都是C代碼,自己照貓畫虎地寫唄!

H. Java中怎麼抓取網頁中的圖片

通過httpclient來爬取網站內容,分析當前內容頁中的圖片『規則』
抓取一般都是模擬瀏覽器訪問目標網頁,通過返回的頁面html代碼進行分析自己需要的數據
查找規則,例如你爬取的網頁 ,看到當前頁面顯示的圖片格式如下<img src="http://www..com/img/20101025_user.png">
通過解析爬取的網頁源代碼(html)進行字元串的操作即可,現在有相應的第三方jar包可以幫你更快的完成這部分工作,例如htmlpaser,獲取到對應的地址,然後進行保存或下載。
你可以搜索,java爬蟲(httpclient)和htmlpaser做更多的了解。

I. java中如何獲取特定目錄下的圖片

ClassLoader/Class的getResource/getResourceAsStream 是從CLASSPATH中獲取資源。

可以在eclipse的項目下新建一個resources目錄,把images文件夾放到這個目錄下,然後把resources加到Build Path里。

J. 使用Java的IO讀取圖片

package test;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadFile {

public static void getSong(String _path, String _savePath) {
String savePath = _savePath;
String path = _path;
int BYTE_SIZE = 1;
int SAVE_SIZE = 1024;
byte[] buff = new byte[BYTE_SIZE]; // 每次讀的緩存
byte[] save = new byte[SAVE_SIZE]; // 保存前緩存
BufferedInputStream bf = null;
FileOutputStream file;
URL url = null;
HttpURLConnection httpUrl;
try {
url = new URL(path);
httpUrl = (HttpURLConnection) url.openConnection();
System.out.println("已經打開連接....");
bf = new BufferedInputStream(httpUrl.getInputStream());
System.out.println("已經獲取資源......");
file = new FileOutputStream(savePath);
System.out.println("准備保存到:" + savePath);

System.out.println("開始讀入......");
int i = 0;
while (bf.read(buff) != -1) { // 一個位元組一個位元組讀
save[i] = buff[0];
if (i == SAVE_SIZE - 1) { // 達到保存長度時開始保存
file.write(save, 0, SAVE_SIZE);
save = new byte[SAVE_SIZE];
i = 0;
} else {
i++;
}
}
// 最後這段如果沒達到保存長度,需要把前面的保存下來
if (i > 0) {
file.write(save, 0, i - 1);
}
System.out.println("下載成功!!!");
httpUrl.disconnect();
file.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {

bf.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
try {
DownloadFile.getSong(
"http://wap.sonyericsson.com/UAprof/K700cR201.xml",
"D://1.xml");
DownloadFile.getSong(
"http://nds1.nds.nokia.com/uaprof/NN78-1r100.xml",
"D://2.xml");
DownloadFile.getSong("http://www.sohu.com", "D://3.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

閱讀全文

與java抓取圖片相關的資料

熱點內容
重生二戰德國元首希特勒 瀏覽:134
被迫成為言情文的炮灰男小三 瀏覽:646
風月片在線觀看視頻 瀏覽:427
如何更新搶修app 瀏覽:711
aqdya愛情網 瀏覽:743
韓國倫理電影正宇 瀏覽:887
男主角在劇里叫諾亞的電影 瀏覽:794
集結號的男主 瀏覽:415
魔獸世界懷舊服會長移交命令 瀏覽:100
中文字幕在線觀看的網站 瀏覽:473
主角上自己女兒的小說 瀏覽:112
javaextjs文件上傳 瀏覽:28
有哪些佛教電影 瀏覽:149
成人劇情小說 瀏覽:113
國外免費小電影網站 瀏覽:909
怎麼把文件夾百度網盤 瀏覽:788
韓國頂級愛情電影 瀏覽:489
美國電影紅 瀏覽:946
框架梁與次梁哪條梁需要加密 瀏覽:6
俄羅斯版越戰英豪電影 瀏覽:970