Java bufferedimage from array

Convert Byte Array to BufferedImage in Java

Convert byte array to BufferedImage in java

In this article, we will see how to convert byte array to BufferedImage in java.

πŸ’‘ Outline

To convert byte array to BufferedImage, you can use below code:

Convert byte array image to BufferedImage in java

Using ByteArrayInputStream

Here are steps to convert byte array to BufferedImage in java.

  • Create ByteArrayInputStream object by passing byte[] in the constructor.
  • Pass above InputStream to ImageIo.read() and get BufferedImage from it.

public static BufferedImage convertByteArrayToBufferedImage ( byte [ ] byteArr ) throws IOException

[email protected]: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = [email protected] transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 500 height = 500 #numDataElements 3 dataOff[0] = 2

Using BufferedImage’s setData

Above approach may not work in some case and may return null.

You can follow below steps to use this method as per this stackoverflow thread.

  • Create a blank image
  • Transform byte array to Raster and use setData to fill the image

public static BufferedImage convertByteArrayToBufferedImage ( byte [ ] byteArr ) throws IOException

Raster raster = Raster . createRaster ( bufferedImage . getSampleModel ( ) , new DataBufferByte ( byteArr , byteArr . length ) , new Point ( ) ) ;

That’s all about how to convert byte array to BufferedImage in java.

Was this post helpful?

Share this

Author

Count Files in Directory in Java

Count Files in Directory in Java

Table of ContentsUsing java.io.File ClassUse File.listFiles() MethodCount Files in the Current Directory (Excluding Sub-directories)Count Files in the Current Directory (Including Sub-directories)Count Files & Folders in Current Directory (Excluding Sub-directories)Count Files & Folders in Current Directory (Including Sub-directories)Use File.list() MethodUsing java.nio.file.DirectoryStream ClassCount Files in the Current Directory (Excluding Sub-directories)Count Files in the Current Directory (Including Sub-directories)Count […]

Convert System.nanoTime to Seconds in Java

Table of ContentsIntroductionSystem.nanoTime()Dividing the System.nanoTime() with a Constant ValueUsing the convert() Method of Time Unit Class in JavaUsing the toSeconds() Method of Time Unit Class in JavaUsing the Utility Methods of Duration Class in Java Introduction In this article, we will look into How to Convert System.nanoTime() to Seconds in Java. We will look at […]

Update Value of Key in HashMap in Java

Table of ContentsUsing the put() Method of HashMap Collection in JavaUsing the compute() Method of HashMap Collection in JavaUsing the merge() Method of the HashMap Collection in JavaUsing the computeIfPresent() Method of The HashMap Collection in JavaUsing the replace() Method of The HashMap Collection in JavaUsing the TObjectIntHashMap Class of Gnu.Trove Package in JavaUsing the […]

How to Get Variable From Another Class in Java

Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

Create Array of Linked Lists in Java

Table of ContentsIntroductionLinked List in JavaApplication of Array of Linked ListsCreate Array of Linked Lists in JavaUsing Object[] array of Linked Lists in JavaUsing the Linked List array in JavaUsing the ArrayList of Linked Lists in JavaUsing the Apache Commons Collections Package Introduction In this article, we will look at how to Create an Array […]

Check if Date Is Between Two Dates in Java

Table of ContentsIntroductionDate & Local Date Class in JavaCheck if The Date Is Between Two Dates in JavaUsing the isAfter() and isBefore() Methods of the Local Date Class in JavaUsing the compareTo() Method of the Local Date Class in JavaUsing the after() and before() Methods of the Date Class in JavaUsing the compare To() Method […]

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Π§ΠΈΡ‚Π°ΠΉΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅:  ΠŸΠ΅Ρ€Π΅Π½ΠΎΡ строки константа php
ΠžΡ†Π΅Π½ΠΈΡ‚Π΅ ΡΡ‚Π°Ρ‚ΡŒΡŽ