简体   繁体   中英

How can I cast or initialize ImageInputStream with InputStream?

I am developing an image scraping application. I am getting URL

URL imageUrl = new URL(imageSource);

Then I'm creating an InputStream with this URL:

InputStream is = new URL(imageUrl.toString()).openStream();

After this I wanna create an ImageInputStream to determine ImageIO readers.

ImageInputStream iis = ??????

But I couldn't initialize this. Can I implement URL or InputStream for ImageInputStream?

this is what you are looking for:

ImageInputStream iis = ImageIO.createImageInputStream(is);

Better way is to use ImageIO.read(url) that returns BufferedReader

URL imageUrl = new URL(imageSource);
InputStream is = new URL(imageUrl.toString()).openStream();
ImageInputStream iis = ImageIO.createImageInputStream(is);
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

if(!iter.hasNext())
    throw new RuntimeException("No readers found");
ImageReader reader = iter.next();

This is how you get your reader, or one of the ways of getting the reader. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM