Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Convert Base64 String to JPG Without Quality Loss

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
clay jay
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic