Convert BufferedImage to Byte Array in Java

Convert BufferedImage to byte array in Java

In this article, we will see how to convert BufferedImage to Byte Array in Java.

💡 Outline

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

Java BufferedImage to Byte Array

Here are steps to convert BufferedImage to Byte Array in java:

  • Create instance of ByteArrayOutputStream baos.
  • Call ImageIo.write() with bufferedImage, formatName such as png,jpg etc. and baos as parameters
  • Get byte array from ByteArrayOutputStream baos by calling toByteArray() method.

Here is complete program:

Convert BufferedImage to byte[] without writing to disk

In case you have large amount of BufferedImages and do not want to write to disk, you can use following code as per this stackoverflow thread.
{!{pre code="java" title="BufferedImage to byte[] without writing to disk"}!}czo5MzpcIg0KYnl0ZVtdIGltYWdlQnl0ZXMgPSAoKERhdGFCdWZmZXJCeXRlKSBidWZmZXJlZEltYWdlLmdldERhdGEoKS5nZXREYXR7WyYqJl19YUJ1ZmZlcigpKS5nZXREYXRhKCk7DQpcIjt7WyYqJl19{!{/pre}!}

Above code will create copy of image. If you are looking for direct reference you can use bufferedImage.getRaster().getDataBuffer().

That’s all about how to Convert BufferedImage to Byte Array in Java.

Was this post helpful?

Related Posts

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 25 May

    Get Variable Name as String in JavaScript

    Table of ContentsUsing Object.keys()Using toString() methodUsing hash tableWas this post helpful? In this post, we will see how to get variable name as String in JavaScript. There are multiple ways to get Get variable name as String in JavaScript. Let’s go through them. Using Object.keys() To get variable name as String in JavaScript, use Object.keys() […]

  • 25 May

    Format Date to yyyy-MM-dd in java

    Table of ContentsUsing DateTimeFormatter with LocalDate (Java 8)Using SimpleDateFormatUsing Calendar classWas this post helpful? In this post, we will see how to Format Date to yyyy-MM-dd in java. There are multiple ways to format Date to yyyy-MM-dd in java. Let’s go through them. Using DateTimeFormatter with LocalDate (Java 8) To Format Date to yyyy-MM-dd in […]

  • 25 May

    Remove Comma from String in JavaScript

    Table of ContentsUsing replace()Using replaceAll()Using split() and join() methodWas this post helpful? In this post, we will see how to remove comma from String in Javascript. There are multiple ways to do it. Let’s go through them. Using replace() To remove comma from String in Javascript: Use replace() method with regular expression /\,/g as first […]

  • 25 May

    Escape new line in JavaScript

    Table of ContentsEscape new line in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape new line in JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape new line in JavaScript To escape new line in JavaScript: Use replace() method with regular expression […]

  • 23 May

    Escape Ampersand in URL in JavaScript

    Table of ContentsGeneric solution to escape special character in JavaScriptWas this post helpful? In this post, we will see how to escape ampersand in JavaScript. To escape Ampersand in JavaScript, use encodeURIComponent() in JavaScript. [crayon-62e0eecacf5e1562185581/] Output [crayon-62e0eecacf5e5484249730/] As you can see that when we called encodeURIComponent, it encoded & to %26. You can use % […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.