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 https://addons.mozilla.org/en-US/firefox/addon/10434 . It gave an xml file for me.

My internet connection is not available at that time. so the favicon [http://www.bing.com/favicon.ico 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
/usr/lib/firefox-addons/searchplugins/en-US
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 http://en.wikipedia.org/wiki/Data_URI_scheme
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 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("favicon.png"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(image, "png", baos);
      String encodedImage = Base64.encode(baos.toByteArray());
     System.out.println(encodedImage);
} catch(Exception e) {
     e.printStackTrace();
}
}

}
Here is the Bing img in data format

Embedded Image

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

Steps:

  • 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="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>Bing</ShortName>
    <Tags>Bing</Tags>
    <Description>Bing. Search by Microsoft.</Description>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+Z2P8Vpv8Vpv%2FF6f%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2Fi9P+K0%2F9QvP9QvP9tx%2F%2FF6f%2F%2F%2F%2F%2F%2F%2F%2F+n3v8Vpv8Vpv8Vpv%2FF6f%2F%2F%2F%2F%2FT7v+Z2P%2Fi9P%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F+24%2F9QvP8Vpv8Vpv8Vpv8Vpv%2FF6f%2F%2F%2F%2F%2FF6f8Vpv8Vpv8krP9QvP9QvP9Bt%2F8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv%2FF6f%2F%2F%2F%2F%2FF6f8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv9Bt%2F9QvP9Bt%2F8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8Vpv8AAHBsAABhdAAAbiAAAHJ0AABsaQAAdGkAACBDAABlbgAAUEEAAEVYAAAuQwAAOy4AAEU7AABBVAAAQ00AAC5W</Image>
    <Url type="text/html" template="http://www.bing.com/search?q={searchTerms}&amp;form=OSDSRC"/>
    <Url type="application/x-suggestions+json" template="http://api.bing.com/osjson.aspx?query={searchTerms}&amp;language={language}&amp;form=OSDJAS"/>
</OpenSearchDescription>

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.

    Thanks

  • sree

    hi arul,
    A qstn

    what are the jar file you have used for these imports
    1)Base64;
    2)ImageIO;
    Thanks
    -sree

    • http://www.arulraj.net/about 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
        Thanks

  • N.S.KARTHIK

    I have already used this technique “http://dean.edwards.name/weblog/2005/06/base64-ie/”
    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?

    • http://arulraj.net Arul

      Hi casper,
      It fixed now…