Image to binary data in java

Image to binary data in java

image0

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.

image1

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

.. raw:: html
/usr/lib/firefox-addons/searchplugins/en-US .. raw:: html

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</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F%2F%2F%2F%2F%2F%2F+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>

image3

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

Show Comments