File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Convert Base64 String to JPG Without Quality Loss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert Base64 String to JPG Without Quality Loss" Watch "Convert Base64 String to JPG Without Quality Loss" New topic

Convert Base64 String to JPG Without Quality Loss

clay jay

Joined: Feb 05, 2009
Posts: 25
Hi Everyone,

I have a Flex application passing in a base64 encoding String to a Servlet. I am then taking that String and converting it to a BufferedImage so I can display to the user. I have tested with Flex encoding and decoding the image and saving it directly from the Flex application and the image looks perfect so I don't think the encoding on the Flex end is an issue.

When I do the conversion in Java and display it to the user it loses quality somewhere along the way and I just for the life of me figure out why. Below is the code I'm using to take the base64 encoded String out of the request, convert it to a BufferedImage and output it to the page.

Yes it's kinda gross but it's a long story of why it needs to be displayed on a new tab instead of just giving the user a save dialog. If anyone has any ideas of why it would be losing quality(or just another way to do it) it would be greatly appreciated. Thanks.
clay jay

Joined: Feb 05, 2009
Posts: 25
Ok I think I know the cause but still unsure of the solution. BufferedImage defaults to 72dpi while the image I'm trying to display to the user is originally 96dpi. So now I need to find a way to change the dpi setting of the BufferedImage(or use an alternative). Surprisingly it's not as easy as expected and I don't see any sort of setDpi methods or anything so any help here would be great.

EDIT: While the above is still an issue it's definitely not the reason the image quality is less.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Note that JPG is a "lossy" format and will never perfectly rebuild your image - thats the whole trade-off to get a compact format.

If your image has a limited color pallete, use GIF compression.

clay jay

Joined: Feb 05, 2009
Posts: 25
Thanks for the reply William.

I'm not that familiar with doing this imaging work so I assumed there must be a way to re-encode the JPG at least close to the proper quality even though JPG's are lossy but maybe that's where I've been going wrong this entire time. Probably should have just been going with a PNG instead.

Either way I found this pretty cool little trick for embedding inline images into a webpage and the image comes out in proper quality because it doesn't have to do any re-encoding.

And for anyone coming across this thread in the future, here is how I encoded the image in Flex:
I agree. Here's the link:
subject: Convert Base64 String to JPG Without Quality Loss
It's not a secret anymore!