Java Code Examples for javax.imageio.ImageReader#setInput()
The following examples show how to use
javax.imageio.ImageReader#setInput() .
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: TruncatedImageWarningTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { String sep = System.getProperty("file.separator"); String dir = System.getProperty("test.src", "."); String filePath = dir+sep+fileName; System.out.println("Test file: " + filePath); File f = new File(filePath); ImageInputStream in = ImageIO.createImageInputStream(f); ImageReader reader = ImageIO.getImageReaders(in).next(); TruncatedImageWarningTest twt = new TruncatedImageWarningTest(); reader.addIIOReadWarningListener(twt); reader.setInput(in); reader.read(0); if (!twt.receivedWarning) { throw new RuntimeException("No expected warning"); } }
Example 2
Source File: SourceImage.java From screenstudio with GNU General Public License v3.0 | 6 votes |
private BufferedImage[] readGif(File input) { BufferedImage[] images = new BufferedImage[0]; try { ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next(); ImageInputStream stream = ImageIO.createImageInputStream(input); reader.setInput(stream); int count = reader.getNumImages(true); images = new BufferedImage[count]; for (int index = 0; index < count; index++) { BufferedImage frame = reader.read(index); images[index] = new BufferedImage(mBounds.width, mBounds.height, mImageType); images[index].createGraphics().drawImage(frame.getScaledInstance(mBounds.width, mBounds.height, Image.SCALE_SMOOTH), 0, 0, null); data = ((DataBufferByte) images[index].getRaster().getDataBuffer()).getData(); } mTimeDelay = 100; } catch (IOException ex) { } return images; }
Example 3
Source File: ImageLoadTwelveMonkeysTest.java From java-image-processing-survival-guide with Apache License 2.0 | 6 votes |
/** * Load a multi-page TIFF image and split it into its individual pages. */ @Test public void testExtractPagesFromMultiPageTiffCompression7() throws Exception { File sourceImageFile = getImageFile("tiff", "test-multi-rgb-compression-type-7.tiff"); ImageInputStream is = ImageIO.createImageInputStream(sourceImageFile); // get the first matching reader Iterator<ImageReader> iterator = ImageIO.getImageReaders(is); ImageReader imageReader = iterator.next(); imageReader.setInput(is); // split the multi-page TIFF int pages = imageReader.getNumImages(true); for (int i = 0; i < pages; i++) { BufferedImage bufferedImage = imageReader.read(i); assertValidBufferedImage(bufferedImage); } assertEquals("Expect to have 10 pages", 10, pages); }
Example 4
Source File: InputImageTests.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public void runTest(Object ctx, int numReps) { final Context ictx = (Context)ctx; final ImageReader reader = ictx.reader; final boolean seekForwardOnly = ictx.seekForwardOnly; final boolean ignoreMetadata = ictx.ignoreMetadata; do { try { ImageInputStream iis = ictx.createImageInputStream(); reader.setInput(iis, seekForwardOnly, ignoreMetadata); reader.read(0); reader.reset(); iis.close(); ictx.closeOriginalStream(); } catch (IOException e) { e.printStackTrace(); } } while (--numReps >= 0); }
Example 5
Source File: InputImageTests.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
public void runTest(Object ctx, int numReps) { final Context ictx = (Context)ctx; final ImageReader reader = ictx.reader; final boolean seekForwardOnly = ictx.seekForwardOnly; final boolean ignoreMetadata = ictx.ignoreMetadata; do { try { ImageInputStream iis = ictx.createImageInputStream(); reader.setInput(iis, seekForwardOnly, ignoreMetadata); reader.getImageMetadata(0); reader.reset(); iis.close(); ictx.closeOriginalStream(); } catch (IOException e) { e.printStackTrace(); } } while (--numReps >= 0); }
Example 6
Source File: Utils.java From skin-composer with MIT License | 6 votes |
public static boolean doesImageFitBox(FileHandle fileHandle, float width, float height) { boolean result = false; String suffix = fileHandle.extension(); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try (var stream = new FileImageInputStream(fileHandle.file())) { reader.setInput(stream); int imageWidth = reader.getWidth(reader.getMinIndex()); int imageHeight = reader.getHeight(reader.getMinIndex()); result = imageWidth < width && imageHeight < height; } catch (IOException e) { Gdx.app.error(Utils.class.getName(), "error checking image dimensions", e); } finally { reader.dispose(); } } else { Gdx.app.error(Utils.class.getName(), "No reader available to check image dimensions"); } return result; }
Example 7
Source File: ITXtTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private static ITXtTest readFrom(File f) { try { ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader r = ImageIO.getImageReaders(iis).next(); r.setInput(iis); IIOImage dst = r.readAll(0, null); // look for iTXt node IIOMetadata m = dst.getMetadata(); Node root = m.getAsTree(m.getNativeMetadataFormatName()); Node n = root.getFirstChild(); while (n != null && !"iTXt".equals(n.getNodeName())) { n = n.getNextSibling(); } if (n == null) { throw new RuntimeException("No iTXt node!"); } ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n); return t; } catch (Throwable e) { throw new RuntimeException("Reading test failed.", e); } }
Example 8
Source File: MJPGImageReader.java From jpexs-decompiler with GNU General Public License v3.0 | 6 votes |
/** Reads the image header. * Does nothing if the header has already been loaded. */ private void readHeader() throws IOException { if (image == null) { ImageReader r = new JPEGImageReader(getOriginatingProvider()); Object in = getInput(); /*if (in instanceof Buffer) { Buffer buffer = (Buffer) in; in=buffer.getData(); }*/ if (in instanceof byte[]) { r.setInput(new MemoryCacheImageInputStream(AVIBMPDIB.prependDHTSeg((byte[]) in))); } else if (in instanceof ImageInputStream) { r.setInput(AVIBMPDIB.prependDHTSeg((ImageInputStream) in)); } else { r.setInput(AVIBMPDIB.prependDHTSeg((InputStream) in)); } image = r.read(0); } }
Example 9
Source File: ImageUtil.java From density-converter with Apache License 2.0 | 6 votes |
/** * 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); } finally { reader.dispose(); } } throw new IOException("Not a known image file: " + imgFile.getAbsolutePath()); }
Example 10
Source File: ImageHelper.java From MillionHero with MIT License | 6 votes |
/** * 图片裁剪通用接口 * * @param src 图片源地址,图片格式PNG * @param dest 图片目的地址 * @param x 图片起始点x坐标 * @param y 图片起始点y坐标 * @param w 图片宽度 * @param h 图片高度 */ public void cutImage(String src, String dest, int x, int y, int w, int h) { try{ Iterator iterator = ImageIO.getImageReadersByFormatName("png"); ImageReader reader = (ImageReader) iterator.next(); InputStream in = new FileInputStream(src); ImageInputStream iis = ImageIO.createImageInputStream(in); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x, y, w, h); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, "png", new File(dest)); }catch (IOException e){ System.err.println("裁剪图片失败"); } }
Example 11
Source File: TruncatedImageWarningTest.java From hottub with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { String sep = System.getProperty("file.separator"); String dir = System.getProperty("test.src", "."); String filePath = dir+sep+fileName; System.out.println("Test file: " + filePath); File f = new File(filePath); ImageInputStream in = ImageIO.createImageInputStream(f); ImageReader reader = ImageIO.getImageReaders(in).next(); TruncatedImageWarningTest twt = new TruncatedImageWarningTest(); reader.addIIOReadWarningListener(twt); reader.setInput(in); reader.read(0); if (!twt.receivedWarning) { throw new RuntimeException("No expected warning"); } }
Example 12
Source File: ImageLoadJaiTest.java From java-image-processing-survival-guide with Apache License 2.0 | 6 votes |
/** * Load a multi-page TIFF image and split it into its individual pages. */ @Test public void testExtractPagesFromMultiPageTiffCompression7() throws Exception { File sourceImageFile = getImageFile("tiff", "test-multi-rgb-compression-type-7.tiff"); ImageInputStream is = ImageIO.createImageInputStream(sourceImageFile); // get the first matching reader Iterator<ImageReader> iterator = ImageIO.getImageReaders(is); ImageReader imageReader = iterator.next(); imageReader.setInput(is); // split the multi-page TIFF int pages = imageReader.getNumImages(true); for (int i = 0; i < pages; i++) { BufferedImage bufferedImage = imageReader.read(i); assertValidBufferedImage(bufferedImage); } assertEquals("Expect to have 10 pages", 10, pages); }
Example 13
Source File: BigBufferedImage.java From OpenRS with GNU General Public License v3.0 | 6 votes |
@Override public ImagePartLoader call() throws Exception { Thread.currentThread().setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2); try (ImageInputStream stream = ImageIO.createImageInputStream(file);) { Iterator<ImageReader> readers = ImageIO.getImageReaders(stream); if (readers.hasNext()) { ImageReader reader = readers.next(); reader.setInput(stream, true, true); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(region); BufferedImage part = reader.read(0, param); Raster source = part.getRaster(); WritableRaster target = image.getRaster(); target.setRect(0, y, source); } } return ImagePartLoader.this; }
Example 14
Source File: ReadAsGrayTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
private static void doTest(int type) throws IOException { BufferedImage src = createTestImage(type); File f = new File("test.jpg"); if (!ImageIO.write(src, "jpg", f)) { throw new RuntimeException("Failed to write test image."); } ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader reader = ImageIO.getImageReaders(iis).next(); reader.setInput(iis); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); ImageTypeSpecifier srgb = null; ImageTypeSpecifier gray = null; // look for gray and srgb types while ((srgb == null || gray == null) && types.hasNext()) { ImageTypeSpecifier t = types.next(); if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) { gray = t; } if (t.getColorModel().getColorSpace() == sRGB) { srgb = t; } } if (gray == null) { throw new RuntimeException("No gray type available."); } if (srgb == null) { throw new RuntimeException("No srgb type available."); } System.out.println("Read as GRAY..."); testType(reader, gray, src); System.out.println("Read as sRGB..."); testType(reader, srgb, src); }
Example 15
Source File: ReadAsGrayTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static void doTest(int type) throws IOException { BufferedImage src = createTestImage(type); File f = new File("test.jpg"); if (!ImageIO.write(src, "jpg", f)) { throw new RuntimeException("Failed to write test image."); } ImageInputStream iis = ImageIO.createImageInputStream(f); ImageReader reader = ImageIO.getImageReaders(iis).next(); reader.setInput(iis); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); ImageTypeSpecifier srgb = null; ImageTypeSpecifier gray = null; // look for gray and srgb types while ((srgb == null || gray == null) && types.hasNext()) { ImageTypeSpecifier t = types.next(); if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) { gray = t; } if (t.getColorModel().getColorSpace() == sRGB) { srgb = t; } } if (gray == null) { throw new RuntimeException("No gray type available."); } if (srgb == null) { throw new RuntimeException("No srgb type available."); } System.out.println("Read as GRAY..."); testType(reader, gray, src); System.out.println("Read as sRGB..."); testType(reader, srgb, src); }
Example 16
Source File: BooleanAttributes.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception { BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB); ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next(); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageOutputStream ios = new MemoryCacheImageOutputStream(os); iw.setOutput(ios); ImageWriteParam param = null; IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param); IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param); IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta; Source src = new StreamSource(new StringReader(metaXml)); DOMResult dst = new DOMResult(); transform(src, dst); Document doc = (Document)dst.getNode(); Element node = doc.getDocumentElement(); String metaFormat = node.getNodeName(); // Verify that the default metadata gets formatted correctly. verify(meta.getAsTree(metaFormat), boolXpaths, false); meta.mergeTree(metaFormat, node); // Verify that the merged metadata gets formatte correctly. verify(meta.getAsTree(metaFormat), boolXpaths, true); iw.write(streamMeta, new IIOImage(img, null, imageMeta), param); iw.dispose(); ios.close(); ImageReader ir = ImageIO.getImageReader(iw); byte[] bytes = os.toByteArray(); if (bytes.length == 0) throw new AssertionError("Zero length image file"); ByteArrayInputStream is = new ByteArrayInputStream(bytes); ImageInputStream iis = new MemoryCacheImageInputStream(is); ir.setInput(iis); if (useStreamMeta) meta = ir.getStreamMetadata(); else meta = ir.getImageMetadata(0); // Verify again after writing and re-reading the image verify(meta.getAsTree(metaFormat), boolXpaths, true); }
Example 17
Source File: ImageUtils.java From RemoteSupportTool with Apache License 2.0 | 4 votes |
public static BufferedImage read(InputStream input) throws IOException { ImageReader reader = ImageIO.getImageReadersByFormatName("jpg").next(); reader.setInput(ImageIO.createImageInputStream(input), false); //LOGGER.debug("number of images: " + reader.getNumImages(true)); return reader.read(0); }
Example 18
Source File: UIUtil.java From birt with Eclipse Public License 1.0 | 4 votes |
/** * Returns the DPI info of given image if applicable. * * @param imageStream * @return the DPI values in format of {hdpi, vdpi}. */ public static int[] getImageResolution( InputStream imageStream ) { int[] dpi = { 0, 0 }; if ( imageStream != null ) { try { ImageInputStream iis = ImageIO.createImageInputStream( imageStream ); Iterator<ImageReader> i = ImageIO.getImageReaders( iis ); ImageReader r = i.next( ); r.setInput( iis ); r.read( 0 ); IIOMetadata meta = r.getImageMetadata( 0 ); if ( meta != null ) { double mm2inch = 25.4; NodeList lst; Element node = (Element) meta.getAsTree( "javax_imageio_1.0" ); //$NON-NLS-1$ lst = node.getElementsByTagName( "HorizontalPixelSize" ); //$NON-NLS-1$ if ( lst != null && lst.getLength( ) == 1 ) { dpi[0] = (int) ( mm2inch / Float.parseFloat( ( (Element) lst.item( 0 ) ).getAttribute( "value" ) ) ); //$NON-NLS-1$ } lst = node.getElementsByTagName( "VerticalPixelSize" ); //$NON-NLS-1$ if ( lst != null && lst.getLength( ) == 1 ) { dpi[1] = (int) ( mm2inch / Float.parseFloat( ( (Element) lst.item( 0 ) ).getAttribute( "value" ) ) ); //$NON-NLS-1$ } } } catch ( Exception e ) { e.printStackTrace( ); } } return dpi; }
Example 19
Source File: BMPPixelSpacingTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Exception { // Header contaning X & Y pixels-per-meter more than value 1 byte[] bmpHeaderData = { (byte) 0x42, (byte) 0x4d, (byte) 0x7e, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x3e, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x28, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }; ImageInputStream imageInput = ImageIO. createImageInputStream(new ByteArrayInputStream(bmpHeaderData)); for (Iterator<ImageReader> it = ImageIO.getImageReaders(imageInput); it.hasNext(); ) { ImageReader reader = it.next(); reader.setInput(imageInput); IIOMetadata metadata = reader.getImageMetadata(0); Node rootNode = metadata.getAsTree("javax_imageio_1.0"); NodeList nl = rootNode.getChildNodes(); //Parse until you get Dimension child node for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if ((node.getNodeName()).equals("Dimension")) { //get childnode list under Dimension node NodeList cl = node.getChildNodes(); //Corresponding node indices under Dimension node int horizontalNodeIndex = 1; int verticalNodeIndex = 2; Node horizontalNode = cl.item(horizontalNodeIndex); Node verticalNode = cl.item(verticalNodeIndex); //get attributes for horizontal and vertical nodes NamedNodeMap horizontalAttr = horizontalNode. getAttributes(); NamedNodeMap verticalAttr = verticalNode.getAttributes(); //since they have only one attribute index is 0 int attributeIndex = 0; Node horizontalValue = horizontalAttr.item(attributeIndex); Node verticalValue = verticalAttr.item(attributeIndex); float horizontalNodeValue = Float. parseFloat((horizontalValue.getNodeValue())); float verticalNodeValue = Float. parseFloat((verticalValue.getNodeValue())); float expectedHorizontalValue, expectedVerticalValue; // in test metadata xPixelsPerMeter & yPixelsPerMeter is 2 expectedHorizontalValue = expectedVerticalValue = 1000.0F / 2; //expected and returned values should be same if ((Float.compare(horizontalNodeValue, expectedHorizontalValue) != 0) || (Float.compare(verticalNodeValue, expectedVerticalValue) != 0)) { throw new RuntimeException("Invalid pixel spacing"); } } } } }
Example 20
Source File: BooleanAttributes.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception { BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB); ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next(); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageOutputStream ios = new MemoryCacheImageOutputStream(os); iw.setOutput(ios); ImageWriteParam param = null; IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param); IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param); IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta; Source src = new StreamSource(new StringReader(metaXml)); DOMResult dst = new DOMResult(); transform(src, dst); Document doc = (Document)dst.getNode(); Element node = doc.getDocumentElement(); String metaFormat = node.getNodeName(); // Verify that the default metadata gets formatted correctly. verify(meta.getAsTree(metaFormat), boolXpaths, false); meta.mergeTree(metaFormat, node); // Verify that the merged metadata gets formatte correctly. verify(meta.getAsTree(metaFormat), boolXpaths, true); iw.write(streamMeta, new IIOImage(img, null, imageMeta), param); iw.dispose(); ios.close(); ImageReader ir = ImageIO.getImageReader(iw); byte[] bytes = os.toByteArray(); if (bytes.length == 0) throw new AssertionError("Zero length image file"); ByteArrayInputStream is = new ByteArrayInputStream(bytes); ImageInputStream iis = new MemoryCacheImageInputStream(is); ir.setInput(iis); if (useStreamMeta) meta = ir.getStreamMetadata(); else meta = ir.getImageMetadata(0); // Verify again after writing and re-reading the image verify(meta.getAsTree(metaFormat), boolXpaths, true); }