Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting a Base64 encoded String to an Image

 
Carl Jenkins
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From which library is the BASE64Decoder class?
 
Carl Jenkins
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the apache Base64 and the sun.misc.BASE64Decoder.

But, got the same results from both..
 
Ernesto Chicas
Greenhorn
Posts: 18
Debian Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
Debian Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm I still get nothing.

Just a png file and when I open it says "No preview available".
 
Carl Jenkins
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
Debian Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html
 
Carl Jenkins
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ernesto Chicas
Greenhorn
Posts: 18
Debian Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try This:


the above method decodes Base64 String to bytes image.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hanuma Deepak Muvvala
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have written this code in one of my projects.
 
Tope Kolawole
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can you show add how came about the variable "map2" because the code is not compiling
 
Hanuma Deepak Muvvala
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some class level variables for map1 and map2, please see below:

 
Tope Kolawole
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Than you very much it saves my ass
 
Emmanuel Oluwagbemi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
....and my ass too!
 
Emmanuel Oluwagbemi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic