aspose file tools*
The moose likes Java in General and the fly likes Converting a Base64 encoded String to an Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting a Base64 encoded String to an Image" Watch "Converting a Base64 encoded String to an Image" New topic
Author

Converting a Base64 encoded String to an Image

Carl Jenkins
Ranch Hand

Joined: Feb 19, 2006
Posts: 39
Hey all!

I am having a bit of difficulty here I was hoping someone could help me out with.

As the subject line states - I am basically trying to take a Base64 encoded string and decode it then turn in into an PNG image.
Problem is I keep just getting an empty file.



Any help would be fantastic! Thanks!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

From which library is the BASE64Decoder class?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Carl Jenkins
Ranch Hand

Joined: Feb 19, 2006
Posts: 39
I tried the apache Base64 and the sun.misc.BASE64Decoder.

But, got the same results from both..
Ernesto Chicas
Greenhorn

Joined: Jan 13, 2010
Posts: 18

Try this
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Are you sure that you're getting an empty file and not a IllegalArgumentException because bufImg == null.
Because that is what I'm getting and that's because there is no ImageReader registered that can read the input.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Carl,

Packages in the sun.* hierarchy should be expressly avoided. Those are private to Sun, and there is no guarantee that they will work from one build to the next.

John.
Ernesto Chicas
Greenhorn

Joined: Jan 13, 2010
Posts: 18

John de Michele wrote:Carl,

Packages in the sun.* hierarchy should be expressly avoided. Those are private to Sun, and there is no guarantee that they will work from one build to the next.

John.


certainly

http://java.sun.com/products/jdk/faq/faq-sun-packages.html


Carl Jenkins
Ranch Hand

Joined: Feb 19, 2006
Posts: 39
Hmm I still get nothing.

Just a png file and when I open it says "No preview available".
Carl Jenkins
Ranch Hand

Joined: Feb 19, 2006
Posts: 39
Carl,

Packages in the sun.* hierarchy should be expressly avoided. Those are private to Sun, and there is no guarantee that they will work from one build to the next.

John.


Understood, but even when using the Apache libs I still get nothing...

Apache Base64
Ernesto Chicas
Greenhorn

Joined: Jan 13, 2010
Posts: 18



http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html
Carl Jenkins
Ranch Hand

Joined: Feb 19, 2006
Posts: 39
1. String value= "Wyk+HjAxHTAyNzg3MDUdODQwHTAxOR0wMDAwMDAwMDAwMDAwMD
AdRkRFQh0wMDAwMDAwHTA0MB0dMS8xHTUwLjVMQh1OHVcgMzR0aCBTdHJlZXQdQXVzdGluHV
RYHSAeMDYdMTBaR0QwMDQdMTFaUmVjaXBpZW50IENvbXBhbnkgTmFtZR0xMlo5MDEyNjM3OTA
2HTE0WioqVEVTVCBMQUJFTCAtIERPIE5PVCBTSElQKiodMjNaTh0yMlocWR0yMFogHDAdMjZaNjEzMxwdHgQ=";
2. Base64 decoder = new Base64();
3. byte[] imgBytes = decoder.decode(value);
4. FileOutputStream osf = new FileOutputStream(new File("yourImage.png"););
5. osf.write(imgBytes);
6. osf.flush();

http://commons.apache.org/codec/apidocs/org/apache...mmons/codec/binary/Base64.html



I tried the apache codes as well.
Certainly, appreciate the help, but this just isn't working for some reason.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You are aware that PNG has a certain structure and if you just decode the String then the output doesn't match that structure?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40


Agreed. Its not a PNG file. Just ran the program and examined the saved file.

Its a text file. With something related to a company in Austin Texas.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernesto Chicas
Greenhorn

Joined: Jan 13, 2010
Posts: 18

Henry Wong wrote:
Agreed. Its not a PNG file. Just ran the program and examined the saved file.

Its a text file. With something related to a company in Austin Texas.

Henry



Is correct, the String is not an image....

I got the following:
[)>010278705840019000000000000000FDEB00000000401/150.5LBNW 34th StreetAustinTX ........................................................

Greetings!
Hanuma Deepak Muvvala
Ranch Hand

Joined: Nov 20, 2005
Posts: 158
Try This:


the above method decodes Base64 String to bytes image.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Hanuma, did you write that code yourself? If not, then give credit to the person who did - QuoteYourSources. Also, please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Hanuma Deepak Muvvala
Ranch Hand

Joined: Nov 20, 2005
Posts: 158
Yes, I have written this code in one of my projects.
Tope Kolawole
Greenhorn

Joined: Oct 01, 2012
Posts: 2
Please can you show add how came about the variable "map2" because the code is not compiling
Hanuma Deepak Muvvala
Ranch Hand

Joined: Nov 20, 2005
Posts: 158
I have some class level variables for map1 and map2, please see below:

Tope Kolawole
Greenhorn

Joined: Oct 01, 2012
Posts: 2
Than you very much it saves my ass
Emmanuel Oluwagbemi
Greenhorn

Joined: Aug 31, 2012
Posts: 2
....and my ass too!
Emmanuel Oluwagbemi
Greenhorn

Joined: Aug 31, 2012
Posts: 2
I find something else though.

equally gets the conversion done. The fully qualified name of the class is sun.misc.BASE64Decoder and it 'ships' with java se (well, the Sun Oracle implementation).

For those who want to use this to convert a html dataURL to binary data (good old bytes), you'll keep getting exceptions if you try convert directly. That's because, dataURL contains some metadata (http://tools.ietf.org/html/rfc2397) before the actual data. It you print the data url, you'll see similar to:
data:image/png;base64,iVBORw0KGgoAAAA

The actual data starts after base64,

So you do this first:


But of course you already knew that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
I have had to break up the long lines inside the code tags because they make the whole thread difficult to read. You can see the right way to do it in the code tags and the wrong way in the quote tags.
You would have done it a lot better than me, because you would have put the " +↩" in the same places throughout.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting a Base64 encoded String to an Image