Image to binary data in java

Image to binary data in java

Today i learn a new thing. That is come across like this way.. I tried to install “Bing” search provider to  my Mozilla Firefox. But that Bing favicon is not shown on the search provider.

Download the mozilla firefox addon from here . It gave an xml file for me.

My internet connection is not available at that time. so the favicon [ not downloaded] for Bing is not shown for me. But the favicon for other search provider like google and amazon are shown. So i thought this is some thing different. Find the path for searchplugin in mozilla. In ubuntu the search provider location is
For google and others the image tag is look like the below
<img alt="" width="16" height="16" />data:image/png;base64,AAABAAEAEBAAAAEAGABoAw.........
Now i realized we can use the binary data of the image as a src instead of image url. Here the wiki page for that
You can use the binary data in “img” tag in html
<img alt="Embedded Image"  src="data:image/png;base64,iVBORw0KGgoAAAAN................ />

So i write java program for to convert image to data. Here is the code for your reference

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;


public class Image2Base64 {

public static void main(String args[]) {
      BufferedImage image = File("favicon.png"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(image, "png", baos);
      String encodedImage = Base64.encode(baos.toByteArray());
} catch(Exception e) {

Here is the Bing img in data format

Embedded Image

Finally i Fix the Bing favicon problem in my Firefox search plugin


  • Remove the Bing search plugin first.
  • Goto Manage Search Engine → Select Bing → Remove
  • Copy the below xml and save as bing.xml in “/usr/lib/firefox-addons/searchplugins/en-US” for ubuntu OR “C:\Program Files\Mozilla\Firefox\searchplugins” for windows
  • Restart your Firefox
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="">
    <Description>Bing. Search by Microsoft.</Description>
    <Image width="16" height="16">data:image/x-icon;base64,AAABAAEAEBAAAAEAGABoAwAAFgAAACgAAAAQAAAAIAAAAAEAGAAAAAAAAAAAABMLAAATCwAAAAAAAAAAAAAVpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8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v8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv%2FF6f%2F%2F%2F%2F%2FF6f8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv9Bt%2F9QvP9Bt%2F8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8AAHBsAABhdAAAbiAAAHJ0AABsaQAAdGkAACBDAABlbgAAUEEAAEVYAAAuQwAAOy4AAEU7AABBVAAAQ00AAC5W</Image>
    <Url type="text/html" template="{searchTerms}&amp;form=OSDSRC"/>
    <Url type="application/x-suggestions+json" template="{searchTerms}&amp;language={language}&amp;form=OSDJAS"/>

Update: Right now this code only supports png and sometimes ico. i don’t know why it behave like this…

  • pavan

    hii Arul….ur code successfully converts image to data but how can we convert data back to image… if i put the data of an image in img tag, i m nt gettng the expected image . I am usin eclipse and chrome .

  • aaqman

    hello arul,

    can you plz guide that how you can do the water marking in java in a PNG file. i mean if you want add a text string (ASCII string) into image and then extract that string involving data encoding and decoding of the embedded bits.


  • sree

    hi arul,
    A qstn

    what are the jar file you have used for these imports

    • Arul

      Hi I am not using any jar file here. Those 2 class files are there in jdk by default. You may got warnings when you compile this class. Because Here I have used deprecated class of Base64.

      • sree

        Thanks for the reply.Was working on android.These things are hard to import in android


    I have already used this technique “”
    5 years ago for Image based Search for the company which i worked…..

  • Casper

    Hi Arul,

    Just a quick note to say thank you before I try it out later.

    (In my Chrome browser, your xml code does not wordwrap so some lines are cut off. However, by looking at the source text of your page I should be able to copy and paste anyway.)

  • Casper

    Hi, I have the same problem, in my Firefox 3.6.12 the favicon for Bing does not show in the search box, but the favicons for the other search providers do.

    So can you please explain step by step what one should do in order to make the Bing favicon show?

    • Arul

      Hi casper,
      It fixed now…