try(ImageInputStream in = ImageIO.createImageInputStream(resourceFile)){ final Iterator<ImageReader> readers = ImageIO.getImageReaders(in); if (readers.hasNext()) { ImageReader reader = readers.next(); try { reader.setInput(in); return new Dimension(reader.getWidth(0), reader.getHeight(0)); } finally { reader.dispose(); } } }
public static Point getImageSizeFromStream(InputStream is) { if (!initialized) { // Stops ImageIO from creating temp files when reading images // from input stream. ImageIO.setUseCache(false); initialized = true; } try { ImageInputStream imageStream = ImageIO.createImageInputStream(is); Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream); if (!readers.hasNext()) return null; ImageReader reader = readers.next(); try { reader.setInput(imageStream); return new Point(reader.getWidth(0), reader.getHeight(0)); } finally { reader.dispose(); } } catch (IOException e) { throw new RuntimeException(e); } }
public ImageMetadata getImageMetadata(InputStream artifactStream) throws Exception { ImageMetadata imageMetadata = new ImageMetadata(); ImageInputStream iis = ImageIO.createImageInputStream(artifactStream); Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) { ImageReader reader = readers.next(); reader.setInput(iis, true); imageMetadata.setWidth(reader.getWidth(0)); imageMetadata.setHeight(reader.getHeight(0)); } else { throw new Exception("Unable to retrieve image metadata from stream. Are you sure the stream provided is a valid input stream for an image source?"); } return imageMetadata; }
/** * Gets image dimensions for given file * @param imgFile image file * @return dimensions of image * @throws IOException if the file is not a known image */ public static Dimension getImageDimension(File imgFile) throws IOException { int pos = imgFile.getName().lastIndexOf("."); if (pos == -1) throw new IOException("No extension for file: " + imgFile.getAbsolutePath()); String suffix = imgFile.getName().substring(pos + 1); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(imgFile); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); return new Dimension(width, height); } catch (IOException e) { log.warn("Error reading: " + imgFile.getAbsolutePath(), e); } finally { reader.dispose(); } } throw new IOException("Not a known image file: " + imgFile.getAbsolutePath()); }
private static BufferedImage readBuffereImage(ImageReader reader, int w, int h) throws IOException { ImageReadParam param = reader.getDefaultReadParam(); int srcWidth = reader.getWidth(0); int srcHeight = reader.getHeight(0); Rectangle rect = null; if ((float) w / h > (float) srcWidth / srcHeight) { h = h * srcWidth / w; w = srcWidth; rect = new Rectangle(0, (srcHeight - h) / 2, w, h); } else { w = w * srcHeight / h; h = srcHeight; rect = new Rectangle((srcWidth - w) / 2, 0, w, h); } param.setSourceRegion(rect); BufferedImage srcBuffered = reader.read(0, param); return srcBuffered; }
private Dimension getImageDim(final String path) { Dimension result = null; String suffix = this.getFileSuffix(path); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(new File(path)); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); result = new Dimension(width, height); } catch (IOException e) { log(e.getMessage()); } finally { reader.dispose(); } } else { log("No reader found for given format: " + suffix)); } return result; }
int width = reader.getWidth(0);
int width = reader.getWidth(FIRST_IMAGE_INDEX); int height = reader.getHeight(FIRST_IMAGE_INDEX);
int width = reader.getWidth(FIRST_IMAGE_INDEX); int height = reader.getHeight(FIRST_IMAGE_INDEX);
public int getWidth(int pIndex) throws IOException { init(pIndex); BufferedImage image = thumbnails[pIndex]; return image != null ? image.getWidth() : reader.getWidth(0); }
/** * Retrieves the dimensions of the {@link RenderedImage} at index <code>imageIndex</code> for * the provided {@link ImageReader} and {@link ImageInputStream}. * * <p> * * <p>Notice that none of the input parameters can be <code>null</code> or a {@link * NullPointerException} will be thrown. Morevoer the <code>imageIndex</code> cannot be negative * or an {@link IllegalArgumentException} will be thrown. * * @param imageIndex the index of the image to get the dimensions for. * @param inStream the {@link ImageInputStream} to use as an input * @param reader the {@link ImageReader} to decode the image dimensions. * @return a {@link Rectangle} that contains the dimensions for the image at index <code> * imageIndex</code> * @throws IOException in case the {@link ImageReader} or the {@link ImageInputStream} fail. */ static Rectangle getDimension(final int imageIndex, final ImageReader reader) throws IOException { Utilities.ensureNonNull("reader", reader); if (imageIndex < 0) throw new IllegalArgumentException( Errors.format(ErrorKeys.INDEX_OUT_OF_BOUNDS_$1, imageIndex)); return new Rectangle(0, 0, reader.getWidth(imageIndex), reader.getHeight(imageIndex)); }
/** * Gets resolution information about the coverage itself. * * @param reader an {@link ImageReader} to use for getting the resolution information. * @throws IOException * @throws TransformException */ private void getResolutionInfo(ImageReader reader) throws IOException, TransformException { // // // // get the dimension of the hr image and build the model as well as // computing the resolution // // final Rectangle actualDim = new Rectangle(0, 0, reader.getWidth(0), reader.getHeight(0)); originalGridRange = new GridEnvelope2D(actualDim); // /// // // setting the higher resolution avalaible for this coverage // // /// highestRes = getResolution(originalEnvelope, actualDim, crs); }
/** * Retrieves the dimensions of the {@link RenderedImage} at index <code>imageIndex</code> for * the provided {@link ImageReader} and {@link ImageInputStream}. * * <p>Notice that none of the input parameters can be <code>null</code> or a {@link * NullPointerException} will be thrown. Morevoer the <code>imageIndex</code> cannot be negative * or an {@link IllegalArgumentException} will be thrown. * * @param imageIndex the index of the image to get the dimensions for. * @param inStream the {@link ImageInputStream} to use as an input * @param reader the {@link ImageReader} to decode the image dimensions. * @return a {@link Rectangle} that contains the dimensions for the image at index <code> * imageIndex</code> * @throws IOException in case the {@link ImageReader} or the {@link ImageInputStream} fail. */ public static Rectangle getDimension( final int imageIndex, final ImageInputStream inStream, final ImageReader reader) throws IOException { Utilities.ensureNonNull("inStream", inStream); Utilities.ensureNonNull("reader", reader); if (imageIndex < 0) throw new IllegalArgumentException( Errors.format(ErrorKeys.INDEX_OUT_OF_BOUNDS_$1, imageIndex)); inStream.reset(); reader.setInput(inStream); return new Rectangle(0, 0, reader.getWidth(imageIndex), reader.getHeight(imageIndex)); }
@Test public void testGetWidthIndexOutOfBounds() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); //int width = 0; try { /*width = */reader.getWidth(-1); // NOTE: Some readers (like the com.sun.imageio stuff) ignores // index in getWidth/getHeight for formats with only one image... //assertEquals("Wrong width reported", data.getDimension().width, width); } catch (IndexOutOfBoundsException ignore) { } catch (IOException e) { fail("Could not read image aspect ratio: " + e); } reader.dispose(); }
@Test public void testSetDestinationOffset() throws IOException { final ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); Point point = new Point(37, 42); param.setDestinationOffset(point); BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(reader.getWidth(0) + point.x, image.getWidth()); assertEquals(reader.getHeight(0) + point.y, image.getHeight()); reader.dispose(); }
private ByteArrayOutputStream transcode(final ImageReader reader, final URL resource, final ImageWriter writer, int outCSType) throws IOException { try (ImageInputStream input = ImageIO.createImageInputStream(resource)) { reader.setInput(input); ImageTypeSpecifier specifier = null; Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); while (types.hasNext()) { ImageTypeSpecifier type = types.next(); if (type.getColorModel().getColorSpace().getType() == outCSType) { specifier = type; break; } } // Read image with requested color space ImageReadParam readParam = reader.getDefaultReadParam(); readParam.setSourceRegion(new Rectangle(Math.min(100, reader.getWidth(0)), Math.min(100, reader.getHeight(0)))); readParam.setDestinationType(specifier); IIOImage image = reader.readAll(0, readParam); // Write it back ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024); try (ImageOutputStream output = new MemoryCacheImageOutputStream(bytes)) { writer.setOutput(output); ImageWriteParam writeParam = writer.getDefaultWriteParam(); writeParam.setDestinationType(specifier); writer.write(null, image, writeParam); return bytes; } } }
@Test public void testGetWidth() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); int width = 0; try { width = reader.getWidth(0); } catch (IOException e) { fail("Could not read image width: " + e); } assertEquals("Wrong width reported", data.getDimension(0).width, width); reader.dispose(); }
@Test public void testGetWidthNoInput() { ImageReader reader = createReader(); int width = 0; try { width = reader.getWidth(0); fail("Width read without imput"); } catch (IllegalStateException ignore) { } catch (IOException e) { fail("Could not read image width: " + e); } assertEquals("Wrong width reported", 0, width); reader.dispose(); }
/** * Extract the ImageLayout from the provided reader for the first available image. * * @param reader an istance of {@link ImageReader} * @throws IOException in case an error occurs */ protected void setLayout(ImageReader reader) throws IOException { Utilities.ensureNonNull("reader", reader); // save ImageLayout ImageLayout2 layout = new ImageLayout2(); ImageTypeSpecifier its = reader.getImageTypes(0).next(); layout.setColorModel(its.getColorModel()).setSampleModel(its.getSampleModel()); layout.setMinX(0).setMinY(0).setWidth(reader.getWidth(0)).setHeight(reader.getHeight(0)); layout.setTileGridXOffset(0) .setTileGridYOffset(0) .setTileWidth(reader.getTileWidth(0)) .setTileHeight(reader.getTileHeight(0)); setlayout(layout); }
@Test public void testSetDestinationRaw() throws IOException { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); ImageTypeSpecifier type = reader.getRawImageType(0); if (type != null) { BufferedImage destination = type.createBufferedImage(reader.getWidth(0), reader.getHeight(0)); param.setDestination(destination); BufferedImage result = null; try { result = reader.read(0, param); } catch (Exception e) { failBecause("Image could not be read", e); } assertSame(destination, result); } else { System.err.println("WARNING: Test skipped due to reader.getRawImageType(0) returning null"); } reader.dispose(); }