Java Code Examples for javax.imageio.ImageReader#canReadRaster()

The following examples show how to use javax.imageio.ImageReader#canReadRaster() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Filter.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Finds a suitable image reader for a format.
 *
 * @param formatName The format to search for.
 * @param errorCause The probably cause if something goes wrong.
 * @return The image reader for the format.
 * @throws MissingImageReaderException if no image reader is found.
 */
protected static ImageReader findImageReader(String formatName, String errorCause) throws MissingImageReaderException
{
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(formatName);
    ImageReader reader = null;
    while (readers.hasNext())
    {
        reader = readers.next();
        if (reader != null && reader.canReadRaster())
        {
            break;
        }
    }
    if (reader == null)
    {
        throw new MissingImageReaderException("Cannot read " + formatName + " image: " + errorCause);
    }
    return reader;
}
 
Example 2
Source File: JPEGFactory.java    From sambox with Apache License 2.0 6 votes vote down vote up
private static BufferedImage readJpeg(Object fileOrStream) throws IOException
{
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
    ImageReader reader = null;
    while (readers.hasNext())
    {
        reader = readers.next();
        if (reader.canReadRaster())
        {
            break;
        }
    }
    requireIOCondition(nonNull(reader), "Cannot find an ImageIO reader for JPEG image");

    try (ImageInputStream iis = ImageIO.createImageInputStream(fileOrStream))
    {
        reader.setInput(iis);
        ImageIO.setUseCache(false);
        return reader.read(0);
    }
    finally
    {
        reader.dispose();
    }
}
 
Example 3
Source File: Filter.java    From sambox with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a suitable image reader for a format.
 *
 * @param formatName The format to search for.
 * @param errorCause The probably cause if something goes wrong.
 * @return The image reader for the format.
 * @throws MissingImageReaderException if no image reader is found.
 */
protected static ImageReader findImageReader(String formatName, String errorCause)
        throws MissingImageReaderException
{
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(formatName);
    ImageReader reader = null;
    while (readers.hasNext())
    {
        reader = readers.next();
        if (nonNull(reader) && reader.canReadRaster())
        {
            break;
        }
    }
    if (reader == null)
    {
        throw new MissingImageReaderException(
                "Cannot read " + formatName + " image: " + errorCause);
    }
    return reader;
}
 
Example 4
Source File: JPEGFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static Raster readJPEGRaster(InputStream stream) throws IOException
{
    // find suitable image reader
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
    ImageReader reader = null;
    while (readers.hasNext())
    {
        reader = readers.next();
        if (reader.canReadRaster())
        {
            break;
        }
    }

    if (reader == null)
    {
        throw new MissingImageReaderException(
                "Cannot read JPEG image: a suitable JAI I/O image filter is not installed");
    }

    ImageInputStream iis = null;
    try
    {
        iis = ImageIO.createImageInputStream(stream);
        reader.setInput(iis);
        ImageIO.setUseCache(false);
        return reader.readRaster(0, null);
    }
    finally
    {
        if (iis != null)
        {
            iis.close();
        }
        reader.dispose();
    }
}
 
Example 5
Source File: ImageUtils.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ImageReader getRasterReader(Iterator<ImageReader> readers) {
	ImageReader reader = null;
	// pick the first available ImageReader that reads raster
	while (readers.hasNext()) {
		reader = readers.next();
		if (reader.canReadRaster()) {
			break;
		}
	}
	return reader;
}