MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to convert image to byte array and byte array to image in Java

This article will tell you, how you may show images on your webpage without having the need to store the images using the conversion image to byte array and byte array to image.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

The conventional way of including an image on a webpage is to store an image on server or database and give its path on html webpage so that the image is obtained from that place and then shown on the webpage.

This article will help you to show images on your webpage’s without having those images stored in your server or database.

This is all possible because image can be converted into sequence of byte array. And the converted byte array can be used to get back the original image.

This byte array converter can be made easily in Java which we will discuss here. After that we will show how to use that byte array to display image on web page.

First we make a Java program which is going to input an image and then convert it into byte array and then encode it using Base 64 encoding which is standard.

Listing 1: Java code to get byte Array

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
 
public class Image2Base64 {
 
public static void main(String args[]) {
try{
      BufferedImage image = ImageIO.read(new File("icon.png"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(image, "png", baos);
     byte[] res=baos.toByteArray();
      String encodedImage = Base64.encode(baos.toByteArray());
      System.out.println(“The encoded image byte array is shown below.Please use this in your webpage image tag.\n”+encodedImage);
} 
catch(Exception e) {
     e.printStackTrace();
System.out.println(“Some problem has occurred. Please try again”);
}
}
 
}

We give a short description of what is happening in the code ,

  1. First we explain the import files.
  2. BufferedImage is used to store the image(in our case icon.png) in a buffer.
  3. This is used to convert the image in byte array
  4. File is used to read a file.
  5. ImageIO is used to write the buffered image into byte output array
  6. Base 64 is used to encode the byte array obtained in Base 64 encoding.
  7. The name of the class is Image2Base64 .We define a main function.
  8. The image to be read is icon.png
  9. First we read the image and store it inside a Buffered image variable which is image.
  10. Now we write the buffered image into a ByteArrayOutputStream object with help of ImageIO class so that it can be converted into a byte array
  11. Now we convert it into byte array.
  12. Now we encode this using Base64 encoding
  13. Then we display the byte array for image on screen.Remember the converted byte array strin is a very long string.
  14. You need to copy this output and then use it in the html webpage to include images(I will show that).Make sure you copy it properly because if anything is missed then image will not be displayed or will be distorted and hence will be of no use.

Now we come to HTML part and tell how to show using a byte array.

Listing 2: Web page using byte Array

<HTML>
<HEAD>
<TITLE>csanuragjain</TITLE>
</HEAD>
<BODY>
<img class='imagem_artigo'  src="data:image/png;base64,PASTE THE BYTE ARRAY HERE WHICH IS OBTAINED FROM JAVA PROGRAM" alt="IMG DESC">
</BODY>
</HTML>

Here, I have shown the html which will be used.

You can define any title you like. Here I have given it as csanuragjain but you may change that in your webpage so that it may reflect appropriate message to the user.

You just need to copy the output of the java program in the area in Bold. The output would be a very long string like iVBORw0KGgoAAAANSUhEUgAAAEAAAAA………………

Now , you may store that byte array in your database but remember that this is a very long string.If you want to use it later in your webpage like in php or jsp then just connect with mysql and get the byte array value from there and then put the value obtained in your own html image tag.

Sample Output :

Sample output

Explaination :

We founded the byte array from the above java programs.It does all the work and at last generates an encoded byte array which could be used easily.

Usage :

You may make a backup of your image so that you always have the byte array to resemble to your favorite images

This is all for this article. Hope you liked it. See you next time with some new exciting article.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login