I’ve designed a rest service to respond the images stored in the database for the users. The service works fine. The service responds the images in jpg format. If the user uploads the image in the jpg format the the response is also fine, if the image is some other formats, then the response cannot be rendered as image. I need to construct a jpg converter or encoder, for all the input image types. Is there are any possible way to achieve this.
Share
You should take a look at
ImageIO. It has support for reading and writing JPEG, PNG, BMP, WBMP and GIF.The JAI API also supplies TIFF support and I’ve used a RAW plugin for Nikon cameras before as well.
Check out Working with Images and the JavaDocs for more info.
Updated with Example
Without the source image is not going to be possible to do a proper test, but this is the basic work flow.
I’ve used
Fileas my inputs, but to demonstrate the basic concept, I’ve createInputStreamandOutputStream(asImageIOcan read/writeFiles)Updated
So using the above code, I was able to convert a PNG file I created in paint to JPG…
PNG/JPG
You could also try converting the input stream and output stream to an
ImageInputStreamandImageOutputStream, although this is normally done when you want to look up the providers for a given image format.