Search in sources :

Example 56 with ImageReader

use of javax.imageio.ImageReader in project tika by apache.

the class ImageParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    String type = metadata.get(Metadata.CONTENT_TYPE);
    if (type != null) {
        //  fix it up to the new one, so Java is happy
        if (OLD_BMP_TYPE.toString().equals(type)) {
            type = MAIN_BMP_TYPE.toString();
        }
        try {
            Iterator<ImageReader> iterator = ImageIO.getImageReadersByMIMEType(type);
            if (iterator.hasNext()) {
                ImageReader reader = iterator.next();
                try {
                    try (ImageInputStream imageStream = ImageIO.createImageInputStream(new CloseShieldInputStream(stream))) {
                        reader.setInput(imageStream);
                        metadata.set(Metadata.IMAGE_WIDTH, Integer.toString(reader.getWidth(0)));
                        metadata.set(Metadata.IMAGE_LENGTH, Integer.toString(reader.getHeight(0)));
                        metadata.set("height", Integer.toString(reader.getHeight(0)));
                        metadata.set("width", Integer.toString(reader.getWidth(0)));
                        loadMetadata(reader.getImageMetadata(0), metadata);
                    }
                } finally {
                    reader.dispose();
                }
            }
            // Translate certain Metadata tags from the ImageIO
            //  specific namespace into the general Tika one
            setIfPresent(metadata, "CommentExtensions CommentExtension", TikaCoreProperties.COMMENTS);
            setIfPresent(metadata, "markerSequence com", TikaCoreProperties.COMMENTS);
            setIfPresent(metadata, "Data BitsPerSample", Metadata.BITS_PER_SAMPLE);
        } catch (IIOException e) {
            //  which Tika will just ignore.
            if (!(e.getMessage() != null && e.getMessage().equals("Unexpected block type 0!") && type.equals("image/gif"))) {
                throw new TikaException(type + " parse error", e);
            }
        }
    }
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    xhtml.endDocument();
}
Also used : TikaException(org.apache.tika.exception.TikaException) ImageInputStream(javax.imageio.stream.ImageInputStream) IIOException(javax.imageio.IIOException) ImageReader(javax.imageio.ImageReader) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 57 with ImageReader

use of javax.imageio.ImageReader in project fess by codelibs.

the class WebDriverGenerator method convert.

protected void convert(final File inputFile, final File outputFile) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    try (ImageInputStream input = ImageIO.createImageInputStream(inputFile)) {
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
        if (readers.hasNext()) {
            final ImageReader reader = readers.next();
            try {
                reader.setInput(input);
                final ImageReadParam param = reader.getDefaultReadParam();
                final int samplingWidth = reader.getWidth(0) / fessConfig.getThumbnailHtmlPhantomjsThumbnailWidthAsInteger();
                final int samplingHeight = reader.getHeight(0) / fessConfig.getThumbnailHtmlPhantomjsThumbnailHeightAsInteger();
                param.setSourceSubsampling(samplingWidth, samplingHeight, 0, 0);
                param.setSourceRegion(new Rectangle(fessConfig.getThumbnailHtmlPhantomjsWindowWidthAsInteger(), fessConfig.getThumbnailHtmlPhantomjsThumbnailHeightAsInteger() * reader.getHeight(0) / fessConfig.getThumbnailHtmlPhantomjsThumbnailWidthAsInteger()));
                final BufferedImage image = reader.read(0, param);
                ImageIO.write(image, fessConfig.getThumbnailHtmlPhantomjsFormat(), outputFile);
                image.flush();
            } finally {
                reader.dispose();
            }
        }
    } catch (final Throwable t) {
        logger.warn("Failed to convert " + inputFile.getAbsolutePath(), t);
        inputFile.renameTo(outputFile);
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ImageInputStream(javax.imageio.stream.ImageInputStream) Rectangle(java.awt.Rectangle) ImageReader(javax.imageio.ImageReader) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) BufferedImage(java.awt.image.BufferedImage)

Example 58 with ImageReader

use of javax.imageio.ImageReader in project fess by codelibs.

the class HtmlTagBasedGenerator method saveImage.

protected boolean saveImage(final ImageInputStream input, final File outputFile) throws IOException {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
    if (readers.hasNext()) {
        final ImageReader reader = readers.next();
        try {
            reader.setInput(input);
            final ImageReadParam param = reader.getDefaultReadParam();
            final int width = reader.getWidth(0);
            final int height = reader.getHeight(0);
            final int samplingWidth = width / fessConfig.getThumbnailHtmlImageThumbnailWidthAsInteger();
            final int samplingHeight = height / fessConfig.getThumbnailHtmlImageThumbnailHeightAsInteger();
            param.setSourceSubsampling(samplingWidth <= 0 ? 1 : samplingWidth, samplingHeight <= 0 ? 1 : samplingHeight, 0, 0);
            param.setSourceRegion(new Rectangle(width, height > width ? width : height));
            final BufferedImage image = reader.read(0, param);
            final int thumbnailWidth = fessConfig.getThumbnailHtmlImageThumbnailWidthAsInteger();
            final int thumbnailHeight = (int) (((float) (height > width ? width : height)) * fessConfig.getThumbnailHtmlImageThumbnailWidthAsInteger().floatValue() / (float) width);
            BufferedImage thumbnail = new BufferedImage(thumbnailWidth, thumbnailHeight, image.getType());
            thumbnail.getGraphics().drawImage(image.getScaledInstance(thumbnailWidth, thumbnailHeight, Image.SCALE_AREA_AVERAGING), 0, 0, thumbnailWidth, thumbnailHeight, null);
            ImageIO.write(thumbnail, fessConfig.getThumbnailHtmlImageFormat(), outputFile);
            image.flush();
            return true;
        } finally {
            reader.dispose();
        }
    }
    return false;
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) Rectangle(java.awt.Rectangle) ImageReader(javax.imageio.ImageReader) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ImageReader (javax.imageio.ImageReader)58 BufferedImage (java.awt.image.BufferedImage)32 ImageInputStream (javax.imageio.stream.ImageInputStream)32 IOException (java.io.IOException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ImageReadParam (javax.imageio.ImageReadParam)14 ImageWriter (javax.imageio.ImageWriter)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)11 Iterator (java.util.Iterator)10 InputStream (java.io.InputStream)9 IIOImage (javax.imageio.IIOImage)9 IIOMetadata (javax.imageio.metadata.IIOMetadata)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)7 File (java.io.File)5 ImageWriteParam (javax.imageio.ImageWriteParam)5 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)4 Graphics (java.awt.Graphics)3 Graphics2D (java.awt.Graphics2D)3