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();
}