This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.
Joined: Feb 05, 2009
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.
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.
Joined: Feb 05, 2009
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: