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.
1 Answers
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 File
as my inputs, but to demonstrate the basic concept, I've create InputStream
and OutputStream
(as ImageIO
can read/write File
s)
File inputFile = new File("/path/to/image.png");
File outputFile = new File("Test.jpg");
try (InputStream is = new FileInputStream(inputFile)) {
BufferedImage image = ImageIO.read(is);
try (OutputStream os = new FileOutputStream(outputFile)) {
ImageIO.write(image, "jpg", os);
} catch (Exception exp) {
exp.printStackTrace();
}
} catch (Exception exp) {
exp.printStackTrace();
}
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 ImageInputStream
and ImageOutputStream
, although this is normally done when you want to look up the providers for a given image format.
File inputFile = new File("...");
File outputFile = new File("Test.jpg");
try (InputStream is = new FileInputStream(inputFile)) {
ImageInputStream iis = ImageIO.createImageInputStream(is);
BufferedImage image = ImageIO.read(iis);
try (OutputStream os = new FileOutputStream(outputFile)) {
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
ImageIO.write(image, "jpg", ios);
} catch (Exception exp) {
exp.printStackTrace();
}
} catch (Exception exp) {
exp.printStackTrace();
}

- 343,457
- 22
- 230
- 366
-
Hi MadProgrammer, I tried with the ImageIO. I have read the input stream and created a bufferedImage, and created the desired format. The input file is PNG and the output file JPG. The input file has a black line with white backgroud and output file produced contains the line with Orange Background. – vvekselva Jan 31 '13 at 07:21
-
Thanks for the Update MadProgrammer. But the same code when we use the png image created from ms paint doesn't work. Even I use the same snippet. – vvekselva Jan 31 '13 at 11:04
-
I've tested it using a PNG created using MS Paint without issue. Try an isolated/standalone test first and see if makes any difference, it is possible that the `InputStream` could be getting corrupted...It might require you to fully upload the file first and THEN convert it – MadProgrammer Jan 31 '13 at 20:23
-
+1 great example + screens. @wekselva see [this](http://stackoverflow.com/questions/14551646/convert-a-jpanel-to-an-image-in-a-jscrollpane/14551679#14551679) alternate approach (scroll to bottom of answer) – David Kroukamp Jan 31 '13 at 21:15