javax.imageio.stream.ImageInputStream#close ( )源码实例Demo

下面列出了javax.imageio.stream.ImageInputStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-8   文件: ConcurrentReadingTest.java
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.");
}
 
源代码2 项目: jdk8u_jdk   文件: InputImageTests.java
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);
}
 
源代码3 项目: springboot-admin   文件: ImageUtils.java
/**
 * 等比缩放,居中剪切
 * 
 * @param src
 * @param dest
 * @param w
 * @param h
 * @throws IOException
 */
public static void scale(String src, String dest, int w, int h) throws IOException {
	String srcSuffix = src.substring(src.lastIndexOf(".") + 1);
	Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(srcSuffix);
	ImageReader reader = (ImageReader) iterator.next();

	InputStream in = new FileInputStream(src);
	ImageInputStream iis = ImageIO.createImageInputStream(in);
	reader.setInput(iis);

	BufferedImage srcBuffered = readBuffereImage(reader, w, h);
	BufferedImage targetBuffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

	Graphics graphics = targetBuffered.getGraphics();
	graphics.drawImage(srcBuffered.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图

	graphics.dispose();
	srcBuffered.flush();
	in.close();
	iis.close();

	ImageIO.write(targetBuffered, srcSuffix, new File(dest));
	targetBuffered.flush();
}
 
源代码4 项目: openjdk-8-source   文件: ImageIO.java
/**
 * 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;
}
 
源代码5 项目: openjdk-8   文件: ImageIO.java
/**
 * 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;
}
 
源代码6 项目: openjdk-8-source   文件: InputStreamTests.java
public void runTest(Object ctx, int numReps) {
    final Context ictx = (Context)ctx;
    try {
        do {
            ImageInputStream iis = ictx.createImageInputStream();
            iis.close();
            ictx.closeOriginalStream();
        } while (--numReps >= 0);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
源代码7 项目: openjdk-jdk9   文件: InputStreamTests.java
public void runTest(Object ctx, int numReps) {
    final Context ictx = (Context)ctx;
    try {
        do {
            ImageInputStream iis = ictx.createImageInputStream();
            iis.close();
            ictx.closeOriginalStream();
        } while (--numReps >= 0);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
@Override
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	ImageInputStream imageInputStream = null;
	ImageReader imageReader = null;
	try {
		imageInputStream = createImageInputStream(inputMessage.getBody());
		MediaType contentType = inputMessage.getHeaders().getContentType();
		Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
		if (imageReaders.hasNext()) {
			imageReader = imageReaders.next();
			ImageReadParam irp = imageReader.getDefaultReadParam();
			process(irp);
			imageReader.setInput(imageInputStream, true);
			return imageReader.read(0, irp);
		}
		else {
			throw new HttpMessageNotReadableException(
					"Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
		}
	}
	finally {
		if (imageReader != null) {
			imageReader.dispose();
		}
		if (imageInputStream != null) {
			try {
				imageInputStream.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}
}
 
源代码9 项目: cs-actions   文件: OcrService.java
private static String deskewImage(String filePath) throws IOException {
    File imageFile = new File(filePath);
    BufferedImage bi = ImageIO.read(imageFile);
    ImageDeskew id = new ImageDeskew(bi);

    double imageSkewAngle = id.getSkewAngle(); // determine skew angle
    if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD))) {
        bi = ImageHelper.rotateImage(bi, -imageSkewAngle); // deskew image
    }

    ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
    Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

    if (!iter.hasNext()) {
        throw new RuntimeException("No readers found!");
    }

    ImageReader reader = iter.next();
    String formatName = reader.getFormatName();
    iis.close();

    String tempImage = Files.createTempFile("tempImage", formatName).toString();

    ImageIO.write(bi, formatName, new File(tempImage));

    return tempImage;
}
 
源代码10 项目: lams   文件: BufferedImageHttpMessageConverter.java
@Override
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	ImageInputStream imageInputStream = null;
	ImageReader imageReader = null;
	try {
		imageInputStream = createImageInputStream(inputMessage.getBody());
		MediaType contentType = inputMessage.getHeaders().getContentType();
		Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
		if (imageReaders.hasNext()) {
			imageReader = imageReaders.next();
			ImageReadParam irp = imageReader.getDefaultReadParam();
			process(irp);
			imageReader.setInput(imageInputStream, true);
			return imageReader.read(0, irp);
		}
		else {
			throw new HttpMessageNotReadableException(
					"Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
		}
	}
	finally {
		if (imageReader != null) {
			imageReader.dispose();
		}
		if (imageInputStream != null) {
			try {
				imageInputStream.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}
}
 
源代码11 项目: jdk8u-dev-jdk   文件: InputStreamTests.java
public void runTest(Object ctx, int numReps) {
    final Context ictx = (Context)ctx;
    try {
        do {
            ImageInputStream iis = ictx.createImageInputStream();
            iis.close();
            ictx.closeOriginalStream();
        } while (--numReps >= 0);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
源代码12 项目: jdk8u_jdk   文件: InputStreamTests.java
public void runTest(Object ctx, int numReps) {
    final Context ictx = (Context)ctx;
    try {
        do {
            ImageInputStream iis = ictx.createImageInputStream();
            iis.close();
            ictx.closeOriginalStream();
        } while (--numReps >= 0);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
源代码13 项目: multimedia-indexing   文件: ImageIOGreyScale.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding a supplied <code>File</code> with an
 * <code>ImageReader</code> chosen automatically from among those currently registered. The
 * <code>File</code> is wrapped in an <code>ImageInputStream</code>. If no registered
 * <code>ImageReader</code> claims to be able to read the resulting stream, <code>null</code> is returned.
 * 
 * <p>
 * The current cache settings from <code>getUseCache</code>and <code>getCacheDirectory</code> will be used
 * to control caching in the <code>ImageInputStream</code> that is created.
 * 
 * <p>
 * Note that there is no <code>read</code> method that takes a filename as a <code>String</code>; use this
 * method instead after creating a <code>File</code> from the filename.
 * 
 * <p>
 * This method does not attempt to locate <code>ImageReader</code>s that can read directly from a
 * <code>File</code>; that may be accomplished using <code>IIORegistry</code> and
 * <code>ImageReaderSpi</code>.
 * 
 * @param input
 *            a <code>File</code> to read from.
 * 
 * @return a <code>BufferedImage</code> containing the decoded contents of the input, or <code>null</code>
 *         .
 * 
 * @exception IllegalArgumentException
 *                if <code>input</code> is <code>null</code>.
 * @exception IOException
 *                if an error occurs during reading.
 */
public static BufferedImage read(File input) throws IOException {
	if (input == null) {
		throw new IllegalArgumentException("input == null!");
	}
	if (!input.canRead()) {
		throw new IIOException("Can't read input file!");
	}

	ImageInputStream stream = createImageInputStream(input);
	if (stream == null) {
		throw new IIOException("Can't create an ImageInputStream!");
	}
	BufferedImage bi = read(stream);
	if (bi == null) {
		stream.close();
	}
	return bi;
}
 
源代码14 项目: jdk8u-jdk   文件: ImageIO.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding
 * a supplied <code>File</code> with an <code>ImageReader</code>
 * chosen automatically from among those currently registered.
 * The <code>File</code> is wrapped in an
 * <code>ImageInputStream</code>.  If no registered
 * <code>ImageReader</code> claims to be able to read the
 * resulting stream, <code>null</code> is returned.
 *
 * <p> The current cache settings from <code>getUseCache</code>and
 * <code>getCacheDirectory</code> will be used to control caching in the
 * <code>ImageInputStream</code> that is created.
 *
 * <p> Note that there is no <code>read</code> method that takes a
 * filename as a <code>String</code>; use this method instead after
 * creating a <code>File</code> from the filename.
 *
 * <p> This method does not attempt to locate
 * <code>ImageReader</code>s that can read directly from a
 * <code>File</code>; that may be accomplished using
 * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
 *
 * @param input a <code>File</code> to read from.
 *
 * @return a <code>BufferedImage</code> containing the decoded
 * contents of the input, or <code>null</code>.
 *
 * @exception IllegalArgumentException if <code>input</code> is
 * <code>null</code>.
 * @exception IOException if an error occurs during reading.
 */
public static BufferedImage read(File input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }
    if (!input.canRead()) {
        throw new IIOException("Can't read input file!");
    }

    ImageInputStream stream = createImageInputStream(input);
    if (stream == null) {
        throw new IIOException("Can't create an ImageInputStream!");
    }
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}
 
源代码15 项目: openjdk-jdk8u   文件: ImageIO.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding
 * a supplied <code>InputStream</code> with an <code>ImageReader</code>
 * chosen automatically from among those currently registered.
 * The <code>InputStream</code> is wrapped in an
 * <code>ImageInputStream</code>.  If no registered
 * <code>ImageReader</code> claims to be able to read the
 * resulting stream, <code>null</code> is returned.
 *
 * <p> The current cache settings from <code>getUseCache</code>and
 * <code>getCacheDirectory</code> will be used to control caching in the
 * <code>ImageInputStream</code> that is created.
 *
 * <p> This method does not attempt to locate
 * <code>ImageReader</code>s that can read directly from an
 * <code>InputStream</code>; that may be accomplished using
 * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
 *
 * <p> This method <em>does not</em> close the provided
 * <code>InputStream</code> after the read operation has completed;
 * it is the responsibility of the caller to close the stream, if desired.
 *
 * @param input an <code>InputStream</code> to read from.
 *
 * @return a <code>BufferedImage</code> containing the decoded
 * contents of the input, or <code>null</code>.
 *
 * @exception IllegalArgumentException if <code>input</code> is
 * <code>null</code>.
 * @exception IOException if an error occurs during reading or when not
 * able to create required ImageInputStream.
 */
public static BufferedImage read(InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }

    ImageInputStream stream = createImageInputStream(input);
    if (stream == null) {
        throw new IIOException("Can't create an ImageInputStream!");
    }
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}
 
源代码16 项目: openjdk-jdk9   文件: DeleteOnExitTest.java
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.");
    }
}
 
源代码17 项目: hottub   文件: ImageIO.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding
 * a supplied <code>File</code> with an <code>ImageReader</code>
 * chosen automatically from among those currently registered.
 * The <code>File</code> is wrapped in an
 * <code>ImageInputStream</code>.  If no registered
 * <code>ImageReader</code> claims to be able to read the
 * resulting stream, <code>null</code> is returned.
 *
 * <p> The current cache settings from <code>getUseCache</code>and
 * <code>getCacheDirectory</code> will be used to control caching in the
 * <code>ImageInputStream</code> that is created.
 *
 * <p> Note that there is no <code>read</code> method that takes a
 * filename as a <code>String</code>; use this method instead after
 * creating a <code>File</code> from the filename.
 *
 * <p> This method does not attempt to locate
 * <code>ImageReader</code>s that can read directly from a
 * <code>File</code>; that may be accomplished using
 * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
 *
 * @param input a <code>File</code> to read from.
 *
 * @return a <code>BufferedImage</code> containing the decoded
 * contents of the input, or <code>null</code>.
 *
 * @exception IllegalArgumentException if <code>input</code> is
 * <code>null</code>.
 * @exception IOException if an error occurs during reading.
 */
public static BufferedImage read(File input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }
    if (!input.canRead()) {
        throw new IIOException("Can't read input file!");
    }

    ImageInputStream stream = createImageInputStream(input);
    if (stream == null) {
        throw new IIOException("Can't create an ImageInputStream!");
    }
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}
 
源代码18 项目: hottub   文件: ImageIO.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding
 * a supplied <code>URL</code> with an <code>ImageReader</code>
 * chosen automatically from among those currently registered.  An
 * <code>InputStream</code> is obtained from the <code>URL</code>,
 * which is wrapped in an <code>ImageInputStream</code>.  If no
 * registered <code>ImageReader</code> claims to be able to read
 * the resulting stream, <code>null</code> is returned.
 *
 * <p> The current cache settings from <code>getUseCache</code>and
 * <code>getCacheDirectory</code> will be used to control caching in the
 * <code>ImageInputStream</code> that is created.
 *
 * <p> This method does not attempt to locate
 * <code>ImageReader</code>s that can read directly from a
 * <code>URL</code>; that may be accomplished using
 * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
 *
 * @param input a <code>URL</code> to read from.
 *
 * @return a <code>BufferedImage</code> containing the decoded
 * contents of the input, or <code>null</code>.
 *
 * @exception IllegalArgumentException if <code>input</code> is
 * <code>null</code>.
 * @exception IOException if an error occurs during reading.
 */
public static BufferedImage read(URL input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }

    InputStream istream = null;
    try {
        istream = input.openStream();
    } catch (IOException e) {
        throw new IIOException("Can't get input stream from URL!", e);
    }
    ImageInputStream stream = createImageInputStream(istream);
    BufferedImage bi;
    try {
        bi = read(stream);
        if (bi == null) {
            stream.close();
        }
    } finally {
        istream.close();
    }
    return bi;
}
 
源代码19 项目: openjdk-8-source   文件: ImageIO.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding
 * a supplied <code>File</code> with an <code>ImageReader</code>
 * chosen automatically from among those currently registered.
 * The <code>File</code> is wrapped in an
 * <code>ImageInputStream</code>.  If no registered
 * <code>ImageReader</code> claims to be able to read the
 * resulting stream, <code>null</code> is returned.
 *
 * <p> The current cache settings from <code>getUseCache</code>and
 * <code>getCacheDirectory</code> will be used to control caching in the
 * <code>ImageInputStream</code> that is created.
 *
 * <p> Note that there is no <code>read</code> method that takes a
 * filename as a <code>String</code>; use this method instead after
 * creating a <code>File</code> from the filename.
 *
 * <p> This method does not attempt to locate
 * <code>ImageReader</code>s that can read directly from a
 * <code>File</code>; that may be accomplished using
 * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
 *
 * @param input a <code>File</code> to read from.
 *
 * @return a <code>BufferedImage</code> containing the decoded
 * contents of the input, or <code>null</code>.
 *
 * @exception IllegalArgumentException if <code>input</code> is
 * <code>null</code>.
 * @exception IOException if an error occurs during reading.
 */
public static BufferedImage read(File input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }
    if (!input.canRead()) {
        throw new IIOException("Can't read input file!");
    }

    ImageInputStream stream = createImageInputStream(input);
    if (stream == null) {
        throw new IIOException("Can't create an ImageInputStream!");
    }
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}
 
源代码20 项目: jdk8u-jdk   文件: ImageIO.java
/**
 * Returns a <code>BufferedImage</code> as the result of decoding
 * a supplied <code>InputStream</code> with an <code>ImageReader</code>
 * chosen automatically from among those currently registered.
 * The <code>InputStream</code> is wrapped in an
 * <code>ImageInputStream</code>.  If no registered
 * <code>ImageReader</code> claims to be able to read the
 * resulting stream, <code>null</code> is returned.
 *
 * <p> The current cache settings from <code>getUseCache</code>and
 * <code>getCacheDirectory</code> will be used to control caching in the
 * <code>ImageInputStream</code> that is created.
 *
 * <p> This method does not attempt to locate
 * <code>ImageReader</code>s that can read directly from an
 * <code>InputStream</code>; that may be accomplished using
 * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
 *
 * <p> This method <em>does not</em> close the provided
 * <code>InputStream</code> after the read operation has completed;
 * it is the responsibility of the caller to close the stream, if desired.
 *
 * @param input an <code>InputStream</code> to read from.
 *
 * @return a <code>BufferedImage</code> containing the decoded
 * contents of the input, or <code>null</code>.
 *
 * @exception IllegalArgumentException if <code>input</code> is
 * <code>null</code>.
 * @exception IOException if an error occurs during reading.
 */
public static BufferedImage read(InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }

    ImageInputStream stream = createImageInputStream(input);
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}