如何将HtmlImage对象转换为RenderedImage?

时间:2014-02-15 20:10:12

标签: java image htmlunit

我有这样的方法,它试图下载图像:

private static void downloadImage() throws IOException {
            int imgSrc = 0;
            for(HtmlImage img : urlList){ 
                String imageFormat = img.toString().substring(img.toString().lastIndexOf(".") + 1);
                String imgPath = "C:\\" + imgSrc + "";
                imgSrc++;
                if (img != null) {
                    File file = new File(imgPath);
                    //In the next method i need img in RenderedImage type
                    ImageIO.write(img, imageFormat, file);
                }
            }  
        }

我如何转换它HtmlImage img => RenderedImage img

2 个答案:

答案 0 :(得分:1)

根据您提到的评论,您刚刚尝试将HtmlImage保存到文件中,那么最简单的方法是使用HtmlImage的{​​{1}}方法类。

您的代码看起来像这样:

if (img != null) {
    File file = new File(imgPath);
    img.saveAs(file);
}

请记住,该方法可能会抛出IOException

答案 1 :(得分:1)

您可以直接获得RenderedImage,而无需保存到文件中:

private RenderedImage getImage(HtmlImage image) throws IOException {
    ImageReader reader = image.getImageReader();
    int minIndex = reader.getMinIndex();
    return reader.read(minIndex);
}

这是一个有效的例子:

package org.human.joecoder.htmlunit;

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlUnitImageScraper {

    public static void main(String[] args) throws Exception {
        WebClient webClient = new WebClient();
        HtmlPage currentPage = (HtmlPage) webClient.getPage(new URL(
                "http://www.google.com"));
        final List<?> images = currentPage.getByXPath("//img");
        for (int i = 0; i < images.size(); i++) {
            Object imageObject = images.get(i);
            HtmlImage image = (HtmlImage) imageObject;
            RenderedImage buf = getImage(image);
            ImageIO.write(buf, "png", new File("image_"+i+".png"));
        }
        webClient.closeAllWindows();
    }

    private static RenderedImage getImage(HtmlImage image) throws IOException {
        ImageReader reader = image.getImageReader();
        int minIndex = reader.getMinIndex();
        return reader.read(minIndex);
    }
}