ImageIO do have this functionality itself, but instead of ImageIO.read you will need to use ImageReader:
ImageReader reader = ImageIO.getImageReadersBySuffix("jpg").next();
(you may want to also check if such reader exists).
Then you need to set the input:
reader.setInput(ImageIO.createImageInputStream(your_imput_stream));
Now you may save your metadata:
IIOMetadata metadata = reader.getImageMetadata(0);
// As far as I understand you should provide
// index as tiff images could have multiple pages
And then read the image:
BufferedImage bi = reader.read(0);
When you want to save new image, you should use ImageWriter:
// I'm writing to byte array in memory, but you may use any other stream
ByteArrayOutputStream os = new ByteArrayOutputStream(255);
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
writer.setOutput(ios);
//You may want also to alter jpeg quality
ImageWriteParam iwParam = writer.getDefaultWriteParam();
iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwParam.setCompressionQuality(.95f);
//Note: we're using metadata we've already saved.
writer.write(null, new IIOImage(bi, null, metadata), iwParam);
writer.dispose();
//ImageIO.write(bi, "jpg", ios); <- This was initially in the code but actually it was only adding image again at the end of the file.
As it's old topic, I guess this answer is a bit too late, but may help others as this topic is still googlable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…