• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Converting a Base64 encoded String to an Image

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

But, got the same results from both..
 
Greenhorn
Posts: 18
jQuery Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 600
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try This:


the above method decodes Base64 String to bytes image.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Deepak Mula
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I have written this code in one of my projects.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can you show add how came about the variable "map2" because the code is not compiling
 
Deepak Mula
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Than you very much it saves my ass
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
....and my ass too!
 
Emmanuel Oluwagbemi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80639
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic