Search in sources :

Example 56 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.

the class DeleteOnExitTest method main.

public static void main(String[] args) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(new byte[100]);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    String tmp = System.getProperty("java.io.tmpdir", ".");
    System.out.println("tmp: " + tmp);
    // count number of files before test
    ImageIO.setUseCache(true);
    ImageIO.setCacheDirectory(new File(tmp));
    File tmpDir = ImageIO.getCacheDirectory();
    System.out.println("tmpDir is " + tmpDir);
    int fnum_before = tmpDir.list().length;
    System.out.println("Files before test: " + fnum_before);
    ImageInputStream iis = ImageIO.createImageInputStream(is);
    System.out.println("iis = " + iis);
    ImageInputStream iis2 = ImageIO.createImageInputStream(is);
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);
    System.out.println("ios = " + ios);
    ImageOutputStream ios2 = ImageIO.createImageOutputStream(os);
    iis2.close();
    ios2.close();
    int fnum_after = tmpDir.list().length;
    System.out.println("Files after test: " + fnum_after);
    if (fnum_before == fnum_after) {
        throw new RuntimeException("Test failed: cache was not used.");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 57 with ImageInputStream

use of javax.imageio.stream.ImageInputStream 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 ImageInputStream

use of javax.imageio.stream.ImageInputStream 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 59 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project beast-mcmc by beast-dev.

the class GTOPO30Tile method read.

void read(String sFile) {
    if (createImage) {
        image = new BufferedImage(NCOLS, NROWS, BufferedImage.TYPE_INT_RGB);
    }
    try {
        ImageInputStream iis = ImageIO.createImageInputStream(new File(sFile));
        iis.setByteOrder(ByteOrder.BIG_ENDIAN);
        for (int y = 0; y < NROWS; y++) {
            iis.readFully(height[y], 0, NCOLS);
            if (createImage) {
                for (int x = 0; x < NCOLS; x++) {
                    //iis.readShort();
                    int color = height[y][x];
                    if (color == -9999) {
                        image.setRGB(x, y, Color.blue.darker().getRGB());
                    } else {
                        image.setRGB(x, y, colorFunction.getColor((float) color).getRGB());
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.err.println();
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) BufferedImage(java.awt.image.BufferedImage)

Example 60 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project poi by apache.

the class BitmapImageRenderer method readImage.

/**
     * Read the image data via ImageIO and optionally try to workaround metadata errors.
     * The resulting image is of image type {@link BufferedImage#TYPE_INT_ARGB}
     *
     * @param data the data stream
     * @param contentType the content type
     * @return the bufferedImage or null, if there was no image reader for this content type
     * @throws IOException thrown if there was an error while processing the image
     */
private static BufferedImage readImage(InputStream data, String contentType) throws IOException {
    IOException lastException = null;
    BufferedImage img = null;
    if (data.markSupported()) {
        data.mark(data.available());
    }
    // currently don't use FileCacheImageInputStream,
    // because of the risk of filling the file handles (see #59166)
    ImageInputStream iis = new MemoryCacheImageInputStream(data);
    try {
        iis = new MemoryCacheImageInputStream(data);
        iis.mark();
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        while (img == null && iter.hasNext()) {
            ImageReader reader = iter.next();
            ImageReadParam param = reader.getDefaultReadParam();
            // 0:default mode, 1:fallback mode
            for (int mode = 0; img == null && mode < 3; mode++) {
                lastException = null;
                try {
                    iis.reset();
                } catch (IOException e) {
                    if (data.markSupported()) {
                        data.reset();
                        data.mark(data.available());
                        iis.close();
                        iis = new MemoryCacheImageInputStream(data);
                    } else {
                        // can't restore the input stream, so we need to stop processing here
                        lastException = e;
                        break;
                    }
                }
                iis.mark();
                try {
                    switch(mode) {
                        case 0:
                            reader.setInput(iis, false, true);
                            img = reader.read(0, param);
                            break;
                        case 1:
                            {
                                // try to load picture in gray scale mode
                                // fallback mode for invalid image band metadata
                                // see http://stackoverflow.com/questions/10416378
                                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                                while (imageTypes.hasNext()) {
                                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                                        param.setDestinationType(imageTypeSpecifier);
                                        break;
                                    }
                                }
                                reader.setInput(iis, false, true);
                                img = reader.read(0, param);
                                break;
                            }
                        case 2:
                            {
                                // try to load truncated pictures by supplying a BufferedImage
                                // and use the processed data up till the point of error
                                reader.setInput(iis, false, true);
                                int height = reader.getHeight(0);
                                int width = reader.getWidth(0);
                                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                                if (imageTypes.hasNext()) {
                                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                    img = imageTypeSpecifier.createBufferedImage(width, height);
                                    param.setDestination(img);
                                } else {
                                    lastException = new IOException("unable to load even a truncated version of the image.");
                                    break;
                                }
                                try {
                                    reader.read(0, param);
                                } finally {
                                    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
                                        int y = findTruncatedBlackBox(img, width, height);
                                        if (y < height) {
                                            BufferedImage argbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                                            Graphics2D g = argbImg.createGraphics();
                                            g.clipRect(0, 0, width, y);
                                            g.drawImage(img, 0, 0, null);
                                            g.dispose();
                                            img.flush();
                                            img = argbImg;
                                        }
                                    }
                                }
                                break;
                            }
                    }
                } catch (IOException e) {
                    if (mode < 2) {
                        lastException = e;
                    }
                } catch (RuntimeException e) {
                    if (mode < 2) {
                        lastException = new IOException("ImageIO runtime exception - " + (mode == 0 ? "normal" : "fallback"), e);
                    }
                }
            }
            reader.dispose();
        }
    } finally {
        iis.close();
    }
    // If you don't have an image at the end of all readers
    if (img == null) {
        if (lastException != null) {
            // multiple locations above ...
            throw lastException;
        }
        LOG.log(POILogger.WARN, "Content-type: " + contentType + " is not support. Image ignored.");
        return null;
    }
    // add alpha channel
    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
        BufferedImage argbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = argbImg.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();
        return argbImg;
    }
    return img;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) ImageReadParam(javax.imageio.ImageReadParam) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader)

Aggregations

ImageInputStream (javax.imageio.stream.ImageInputStream)60 ImageReader (javax.imageio.ImageReader)32 BufferedImage (java.awt.image.BufferedImage)23 IOException (java.io.IOException)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageOutputStream (javax.imageio.stream.ImageOutputStream)9 File (java.io.File)8 InputStream (java.io.InputStream)7 IIOImage (javax.imageio.IIOImage)7 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)7 ImageReadParam (javax.imageio.ImageReadParam)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageWriter (javax.imageio.ImageWriter)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 Iterator (java.util.Iterator)4 IIOException (javax.imageio.IIOException)4 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2