How to Retrieve an InputStream from a BufferedImage in Java?

Question

How can I retrieve an InputStream from a BufferedImage object in Java?

BufferedImage bigImage = GraphicsUtilities.createThumbnail(ImageIO.read(file), 300);
ImageInputStream bigInputStream = ImageIO.createImageInputStream(bigImage);

Answer

Retrieving an InputStream from a BufferedImage can be accomplished using ByteArrayOutputStream. Since BufferedImage does not directly support InputStream creation, we will convert it to a byte array and then to an InputStream.

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ImageStreamExample {
    public static ByteArrayInputStream getInputStreamFromBufferedImage(BufferedImage image) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        ImageIO.write(image, "png", ios);
        ios.flush();
        return new ByteArrayInputStream(baos.toByteArray());
    }
}

Causes

  • `ImageIO.createImageInputStream()` does not accept a `BufferedImage` as a parameter; it only works with `File`, `ImageReader`, or other image sources.
  • BufferedImage needs to be written to an `OutputStream` before transforming it into an `InputStream`.

Solutions

  • Use a `ByteArrayOutputStream` to write the `BufferedImage` to a byte array.
  • Convert the byte array to a `ByteArrayInputStream` to obtain an InputStream.

Common Mistakes

Mistake: Using ImageIO.createImageInputStream() with BufferedImage

Solution: Use ByteArrayOutputStream to first write the BufferedImage and then convert it to InputStream.

Mistake: Not flushing the ImageOutputStream

Solution: Always flush the output stream after writing the image to ensure all data is flushed to the ByteArrayOutputStream.

Helpers

  • BufferedImage InputStream
  • Java BufferedImage to InputStream
  • ImageIO InputStream example
  • Java InputStream from image
  • BufferedImage InputStream conversion

Related Questions

⦿How to Configure Session Timeout Period with Spring Security 3.0

Learn how to set a custom session timeout period in Spring Security 3.0 for your LDAP authentication.

⦿How to Set Global Encoding in Ant's build.xml for ISO-8859-1?

Learn how to globally set file encoding for ISO88591 in Apache Ants build.xml to avoid unmappable character warnings when compiling Java files.

⦿How to Use @Version Annotation in Spring Data JPA?

Learn how to implement Version in Spring Data JPA for managing entity versioning alongside configuration details and common pitfalls.

⦿Why Can't I Download Sources in IntelliJ IDEA Community 12.1.4 Using Maven 3.0.5?

Explore solutions for downloading sources in IntelliJ IDEA 12.1.4 with Maven 3.0.5. Learn why it fails and how to resolve the issue seamlessly.

⦿How to Clear HSQLDB Data After Each JUnit Test Class Execution

Learn how to effectively wipe data from HSQLDB after each JUnit test class using Maven for seamless testing.

⦿How to Check if a Field is Final in Java Using Reflection?

Learn how to determine if a field is final in Java with reflection including code snippets and common pitfalls.

⦿Best Practices for Creating Java Files Containing Only Constants

Learn how to declare constants in Java files effectively using interfaces or abstract classes with best practices and examples.

⦿How to Construct an Abstract Syntax Tree (AST) from a List of Tokens

Learn how to build an Abstract Syntax Tree AST from tokens in your custom scripting language. Stepbystep guide with code examples and common pitfalls.

⦿How to Automate Migration of JUnit 3 Tests to JUnit 4?

Discover effective methods and tools for bulk migrating JUnit 3 tests to JUnit 4 with annotations like Before After and Test.

⦿How to Use Multiple YAML Files in Spring Boot Configuration

Learn how to organize your Spring Boot configuration using multiple YAML files to keep settings manageable and efficient.

© Copyright 2025 - CodingTechRoom.com