public File addImageExtension(File incoming) throws IOException { String format = null; ImageInputStream iis = ImageIO.createImageInputStream(incoming); Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis); while (imageReaders.hasNext()) { ImageReader reader = (ImageReader) imageReaders.next(); format = reader.getFormatName().toLowerCase(); log.debug("filetype is: " + format); File newfile = new File("imageimage." + format); if (newfile.exists()) { newfile.delete(); } Files.copy(incoming.toPath(), newfile.toPath()); incoming.delete(); return newfile; } return null; }
public boolean canDecodeInput(Object input) throws IOException { if (!(input instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream)input; byte[] b = new byte[8]; stream.mark(); stream.readFully(b); stream.reset(); return (b[0] == (byte)137 && b[1] == (byte)80 && b[2] == (byte)78 && b[3] == (byte)71 && b[4] == (byte)13 && b[5] == (byte)10 && b[6] == (byte)26 && b[7] == (byte)10); }
/** * Extracts the picture size of a given Image. * * @param path Path to the image * @return Image-size in pixel * @throws IOException Throws exception when file-access fails */ public static Dimension getPictureSize(final String path) throws IOException { try (ImageInputStream in = ImageIO.createImageInputStream(path)) { 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(); } } return null; } }
/** * Close an {@link ImageInputStream} unconditionally. Equivalent to * calling <code>s.close()</code> when <code>s</code> is nonnull. * {@link IOException}s are quietly logged, as there is generally * nothing that can be done about exceptions on closing. * * @param s a (possibly <code>null</code>) <code>ImageInputStream</code> */ // Why doesn't ImageInputStream implement Closeable? Argh! public static void closeQuietly(ImageInputStream s) { if (s == null) return; try { s.close(); } catch (IOException e) { // ignore // Note that ImageInputStreamImpl.close() rather ridiculously throws // an IOException if the stream is already closed. This is always done // via ImageInputStreamImpl.checkClosed(). } }
public static void main(String[] args) throws Exception { createTestFile(); ImageInputStream iis = ImageIO.createImageInputStream(file); r = ImageIO.getImageReaders(iis).next(); iis.close(); for (int i = 0; i < MAX_THREADS; i++) { (new ConcurrentReadingTest()).start(); } // wait for started threads boolean needWait = true; while (needWait) { Thread.sleep(100); synchronized(lock) { needWait = completeCount < MAX_THREADS; } } System.out.println("Test PASSED."); }
public static ImageReader F2IR(String input) throws IOException { final File file = new File(input); // ��ȡͼƬ��ʽ final String formatname = GetPostfix.fromFilepath(input); Iterator<ImageReader> readers; ImageReader reader; ImageInputStream iis; readers = ImageIO.getImageReadersByFormatName(formatname); reader = readers.next(); iis = ImageIO.createImageInputStream(file); reader.setInput(iis, false); return reader; }
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); }
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); }
public TestImageReaderSpi(String[] FORMATNAMES, String[] SUFFIXES, String[] MIMETYPES) { super("J Duke", // vendor "1.0", // version FORMATNAMES, // format names SUFFIXES, // file suffixes MIMETYPES, // mimetypes "readTest.TestImageReader", // reader class name new Class<?>[] { ImageInputStream.class }, // input types null, // writer class names. true, // supports native metadata, null, // [no] native stream metadata format null, // [no] native stream metadata class null, // [no] native extra stream metadata format null, // [no] native extra stream metadata class true, // supports standard metadata, null, // metadata format name, null, // metadata format class name null, // [no] extra image metadata format null // [no] extra image metadata format class ); }
/** * Returns a <code>BufferedImage</code> as the result of decoding * a supplied <code>ImageInputStream</code> with an * <code>ImageReader</code> chosen automatically from among those * currently registered. If no registered * <code>ImageReader</code> claims to be able to read the stream, * <code>null</code> is returned. * * <p> Unlike most other methods in this class, this method <em>does</em> * close the provided <code>ImageInputStream</code> after the read * operation has completed, unless <code>null</code> is returned, * in which case this method <em>does not</em> close the stream. * * @param stream an <code>ImageInputStream</code> to read from. * * @return a <code>BufferedImage</code> containing the decoded * contents of the input, or <code>null</code>. * * @exception IllegalArgumentException if <code>stream</code> is * <code>null</code>. * @exception IOException if an error occurs during reading. */ public static BufferedImage read(ImageInputStream stream) throws IOException { if (stream == null) { throw new IllegalArgumentException("stream == null!"); } Iterator iter = getImageReaders(stream); if (!iter.hasNext()) { return null; } ImageReader reader = (ImageReader)iter.next(); ImageReadParam param = reader.getDefaultReadParam(); reader.setInput(stream, true, true); BufferedImage bi; try { bi = reader.read(0, param); } finally { reader.dispose(); stream.close(); } return bi; }
public static void main(String[] args) { try { File f = new File("ImageInputStreamFromRAF.tmp"); RandomAccessFile raf = new RandomAccessFile(f, "rw"); ImageInputStream istream = ImageIO.createImageInputStream(raf); ImageOutputStream ostream = ImageIO.createImageOutputStream(raf); f.delete(); if (istream == null) { throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!"); } if (ostream == null) { throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!"); } if (!(istream instanceof FileImageInputStream)) { throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!"); } if (!(ostream instanceof FileImageOutputStream)) { throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!"); } } catch (IOException ioe) { throw new RuntimeException("Unexpected IOException: " + ioe); } }
public ImageInputStream createInputStreamInstance(Object input, boolean useCache, File cacheDir) throws IOException { if (input instanceof InputStream) { InputStream is = (InputStream)input; if (useCache) { return new FileCacheImageInputStream(is, cacheDir); } else { return new MemoryCacheImageInputStream(is); } } else { throw new IllegalArgumentException(); } }
@Override public boolean canDecodeInput(Object input) throws IOException { if (!(input instanceof ImageInputStream)) { input = ImageIO.createImageInputStream(input); } if (input == null) { return false; } ImageInputStream stream = (ImageInputStream)input; byte[] b = new byte[12]; try { stream.mark(); stream.readFully(b); } catch (IOException e) { return false; } return Arrays.equals(b, HEADER_MAGIC); }
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) { setThreadLock(); try { cbLock.check(); super.setInput(input, seekForwardOnly, ignoreMetadata); this.ignoreMetadata = ignoreMetadata; resetInternalState(); iis = (ImageInputStream) input; // Always works setSource(structPointer); } finally { clearThreadLock(); } }
BufferedImage getThumbnail(ImageInputStream iis, int index, JPEGImageReader reader) throws IOException { reader.thumbnailStarted(index); BufferedImage ret = null; if ((thumb != null) && (index == 0)) { ret = thumb.getThumbnail(iis, reader); } else { if (thumb != null) { index--; } JFIFExtensionMarkerSegment jfxx = (JFIFExtensionMarkerSegment) extSegments.get(index); ret = jfxx.thumb.getThumbnail(iis, reader); } reader.thumbnailComplete(); return ret; }
void readByteBuffer(ImageInputStream iis, byte [] data, JPEGImageReader reader, float workPortion, float workOffset) throws IOException { int progInterval = Math.max((int)(data.length/20/workPortion), 1); for (int offset = 0; offset < data.length;) { int len = Math.min(progInterval, data.length-offset); iis.read(data, offset, len); offset += progInterval; float percentDone = ((float) offset* 100) / data.length * workPortion + workOffset; if (percentDone > 100.0F) { percentDone = 100.0F; } reader.thumbnailProgress (percentDone); } }
public JPEGImageReaderSpi() { super(JPEG.vendor, JPEG.version, JPEG.names, JPEG.suffixes, JPEG.MIMETypes, "com.sun.imageio.plugins.jpeg.JPEGImageReader", new Class[] { ImageInputStream.class }, writerSpiNames, true, JPEG.nativeStreamMetadataFormatName, JPEG.nativeStreamMetadataFormatClassName, null, null, true, JPEG.nativeImageMetadataFormatName, JPEG.nativeImageMetadataFormatClassName, null, null ); }
public boolean canDecodeInput(Object source) throws IOException { if (!(source instanceof ImageInputStream)) { return false; } ImageInputStream iis = (ImageInputStream) source; iis.mark(); // If the first two bytes are a JPEG SOI marker, it's probably // a JPEG file. If they aren't, it definitely isn't a JPEG file. int byte1 = iis.read(); int byte2 = iis.read(); iis.reset(); if ((byte1 == 0xFF) && (byte2 == JPEG.SOI)) { return true; } return false; }
public GIFImageReaderSpi() { super(vendorName, version, names, suffixes, MIMETypes, readerClassName, new Class[] { ImageInputStream.class }, writerSpiNames, true, GIFStreamMetadata.nativeMetadataFormatName, "com.sun.imageio.plugins.gif.GIFStreamMetadataFormat", null, null, true, GIFImageMetadata.nativeMetadataFormatName, "com.sun.imageio.plugins.gif.GIFImageMetadataFormat", null, null ); }
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) { super.setInput(input, seekForwardOnly, ignoreMetadata); if (input != null) { if (!(input instanceof ImageInputStream)) { throw new IllegalArgumentException ("input not an ImageInputStream!"); } this.stream = (ImageInputStream)input; } else { this.stream = null; } // Clear all values based on the previous stream contents resetStreamSettings(); }
public MarkTryFinallyReproducerSpi() { super("MarkTryFinallyReproducerSpi", "1.0", localNames, localSuffixes, localMIMETypes, readerClassName, new Class[]{ImageInputStream.class}, new String[0], false, null, null, new String[0], new String[0], false, null, null, new String[0], new String[0]); }
public PNGImageReaderSpi() { super(vendorName, version, names, suffixes, MIMETypes, readerClassName, new Class<?>[] { ImageInputStream.class }, writerSpiNames, false, null, null, null, null, true, PNGMetadata.nativeMetadataFormatName, "com.sun.imageio.plugins.png.PNGMetadataFormat", null, null ); }
private static ITXtTest readFrom(File f) { try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(f)) { ImageReader r = ImageIO.getImageReaders(imageInputStream).next(); r.setInput(imageInputStream); 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); } }
public TIFFImageReaderSpi() { super("Oracle Corporation", "1.0", new String[] {"tif", "TIF", "tiff", "TIFF"}, new String[] {"tif", "tiff"}, new String[] {"image/tiff"}, "com.sun.imageio.plugins.tiff.TIFFImageReader", new Class<?>[] { ImageInputStream.class }, new String[] {"com.sun.imageio.plugins.tiff.TIFFImageWriterSpi"}, false, TIFFStreamMetadata.NATIVE_METADATA_FORMAT_NAME, "com.sun.imageio.plugins.tiff.TIFFStreamMetadataFormat", null, null, true, TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME, "com.sun.imageio.plugins.tiff.TIFFImageMetadataFormat", null, null ); }
public PNGImageReaderSpi() { super(vendorName, version, names, suffixes, MIMETypes, readerClassName, new Class[] { ImageInputStream.class }, writerSpiNames, false, null, null, null, null, true, PNGMetadata.nativeMetadataFormatName, "com.sun.imageio.plugins.png.PNGMetadataFormat", null, null ); }
public void test() { boolean gotException = false; ImageInputStream iis = createShortStream(); try { testRead(iis); } catch (IOException e) { e.printStackTrace(System.out); gotException = true; } if (!gotException) { throw new RuntimeException("Test failed."); } System.out.println("Test PASSED"); }
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"); } }
/** * 获取图片的尺寸 * * @param extName * 后缀名 * @param imgContent * 图片内容 * @return 数组:[宽,高] */ public int[] getSize(String extName, byte[] imgContent) { if (imgContent == null || WakaUtils.string.isEmpty(extName)) { throw new ZhhrUtilException("获取文件大小失败!入参为空"); } // 字节转图片流 try (ImageInputStream imgInputStream = byteArrayStreamToImageInputStream(imgContent)) { // 取得图片读取器 ImageReader imgReader = getImageReader(extName); // 把文件流加载到图片读取器中 imgReader.setInput(imgInputStream, true); return new int[] { imgReader.getWidth(0), imgReader.getHeight(0) }; } catch (IOException e) { throw new ZhhrUtilException(e.getMessage(), e); } }
public void doTest(String compression, int bi_type) throws IOException { System.out.println("Test " + compression + " on " + getImageTypeName(bi_type)); BufferedImage src = createTestImage(bi_type); writer.reset(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); writer.setOutput(ios); ImageWriteParam wparam = prepareWriteParam(compression); writer.write(null, new IIOImage(src, null, null), wparam); ios.flush(); ios.close(); // read result ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ImageInputStream iis = ImageIO.createImageInputStream(bais); reader.reset(); reader.setInput(iis); BufferedImage dst = reader.read(0); checkResult(dst); }
public void launch(HashMap<String, ImageInputStream> refs) { System.out.printf("%s: current context class loader: %s\n", uniqClassName, Thread.currentThread().getContextClassLoader()); try { byte[] data = new byte[1024]; ByteArrayInputStream bais = new ByteArrayInputStream(data); MyImageInputStream iis = new MyImageInputStream(bais, uniqClassName, problems); if (refs != null) { System.out.printf("%s: added to strong store\n", uniqClassName); refs.put(uniqClassName, iis); } iis.read(); //leave stream open : let's shutdown hook work! } catch (IOException e) { problems.add(e); } }
public BMPImageReaderSpi() { super("Oracle Corporation", "1.0", formatNames, entensions, mimeType, "com.sun.imageio.plugins.bmp.BMPImageReader", new Class<?>[] { ImageInputStream.class }, writerSpiNames, false, null, null, null, null, true, BMPMetadata.nativeMetadataFormatName, "com.sun.imageio.plugins.bmp.BMPMetadataFormat", null, null); }
public InputStream nextElement() { try { firstTime = false; ImageInputStream iis = new SubImageInputStream(stream, length); return new InputStreamAdapter(iis); } catch (IOException e) { return null; } }
public static void main(String[] args) throws IOException { // Generate some trivial image and save it to a temporary array ByteArrayOutputStream tmp = new ByteArrayOutputStream(); ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB), "gif", tmp); // Read it back in ImageInputStream in = new MemoryCacheImageInputStream( new ByteArrayInputStream(tmp.toByteArray())); ImageReader reader = ImageIO.getImageReaders(in).next(); reader.setInput(in); // Retrieve standard image metadata tree IIOMetadata meta = reader.getImageMetadata(0); if (meta == null || !meta.isStandardMetadataFormatSupported()) { throw new Error("Test failure: Missing metadata"); } Element root = (Element) meta. getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName); NodeList nodeList = root. getElementsByTagName(root.getFirstChild().getNodeName()); /* * Accessing the nth node should return null and not throw * IndexOutOfBoundsException. */ Node n = (nodeList.item(nodeList.getLength())); }
@Test public void testCloseQuietlyImageInputStreamOpen() { final ImageInputStream in = new MemoryCacheImageInputStream(new NullInputStream(1000)); IOUtils.closeQuietly(in); try { in.close(); fail(); } catch (IOException e) { // This, oddly, the expected behavior of close(). } }