抓取网上的图片(转)

package com.winkee.wse.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {

  
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        try{
             new ImageUtil().createImage(new URL("http://www.google.cn/intl/zh-CN/images/logo_cn.gif"),new File("./loge.jpg"));
         }catch(Exception e){
                 e.printStackTrace();
         } 
    }
   
    public void createImage(File file,File targetFile) throws IOException{
        Image src=ImageIO.read(file);
        int width=src.getWidth(null);
        int height=src.getHeight(null);
        this.writerToImage(src, targetFile, width, height);
    }
   
    public void createImage(File file,File targetFile,int width,int height) throws IOException{
        Image src=ImageIO.read(file);
        this.writerToImage(src, targetFile, width, height);
    }
   
    public void createImage(URL url,File targetFile) throws IOException{
        Image src=ImageIO.read(url);
        int width=src.getWidth(null);
        int height=src.getHeight(null);
        this.writerToImage(src, targetFile, width, height);
    }
   
    public void createImage(URL url,File targetFile,int width,int height) throws IOException{
        Image src=ImageIO.read(url);
        this.writerToImage(src, targetFile, width, height);
    }
   
    //生成图片(长width,宽height)并将图片放入targetFile中。
   
    public void writerToImage(Image src,File targetFile,int width,int height) throws IOException{
        BufferedImage tag=new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
        tag.getGraphics().drawImage(src,0,0,width,height,null); //绘制缩小后的图
        FileOutputStream destOut=new FileOutputStream(targetFile); //输出到文件流
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(destOut);
        encoder.encode(tag); //JPEG编码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值