在使用java做数字图像处理的时候,有时候需要保留图像的EXIF属性信息,比如相机型号,GPS位置等。处理图像的时候,一般直接通过ImageIO读取图像每个像素上的RGB色彩信息,但是图像的其他属性信息是没有读取的,这样在处理完图像RGB色彩信息,通过ImageIO重新写入图片或写入新图片都不会保留原图的属性信息。
查阅了大量资料,有通过第三方图像处理库来读取的,也有直接通过解析图像文件编码来读取EXIF区域信息。最后查阅java官方文档,发现其实ImageIO下还包含很多图像处理相关的类库,其中IIOImage是一个图像容器,可以存入和取出图像栅格信息(RenderedImage)和属性信息(IIOMetadata)。操作流程如下:
1、通过ImageIO的getImageReadersByFormatName()方法和createImageInputStream()来获取imagereader迭代器和实例化图像输入流;
// 通过ImageIO和图像输入流设置imageReader
Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName("JPG"); // 通过ImageIO获取imageReader迭代器
ImageReader imageReader = iterator.next(); // 通过迭代器实例化ImageReader
ImageInputStream imageInputStream = ImageIO.createImageInputStream(srcImage); // 通过ImageIO实例化图像输入流
imageReader.setInput(imageInputStream, true);
2、通过imagereader读取源图片所有信息,存入IIOImage,再从IIOImage中get到属性信息,存入IIOMetadata;
// 读取源图片EXIF信息,存入IIOMetadata
ImageReadParam imageReadParam = imageReader.getDefaultReadParam();
IIOImage iioImage = imageReader.readAll(0, imageReadParam);
IIOMetadata iioMetadata = iioImage.getMetadata();
3、用和读取源图像同样的方式读取目标图片信息;
// 读取目标图片信息
Iterator<ImageReader> iterator2 = ImageIO.getImageReadersByFormatName("JPG");
ImageReader imageReader2 = iterator2.next();
ImageInputStream imageInputStream2 = ImageIO.createImageInputStream(destImage);
imageReader2.setInput(imageInputStream2, true);
4、将源图片iioMetadata信息存入目标图片的IIOImage;
// 将源图片iioMetadata信息存入目标图片的IIOImage
ImageReadParam imageReadParam2 = imageReader2.getDefaultReadParam();
IIOImage iioImage2 = imageReader2.readAll(0, imageReadParam2);
iioImage2.setMetadata(iioMetadata);
5、通过图像输出流和imagewriter将iioImage写入目标图片;
// 将iioImage写入目标图片
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(destImage);
Iterator<ImageWriter> iterator3 = ImageIO.getImageWritersByFormatName("JPG");
ImageWriter imageWriter = iterator3.next();
imageWriter.setOutput(imageOutputStream);
imageWriter.write(iioImage2);
6、最后不要忘了关闭流;
//关闭流
imageInputStream.close();
imageOutputStream.close();
完整的测试代码如下:
import java.io.File;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
public class test {
public static void main(String[] args) throws Exception{
File srcImage = new File("C:/Users/admin/Desktop/test/1.jpg");
File destImage = new File("C:/Users/admin/Desktop/test/2.jpg");
// 通过ImageIO和图像输入流设置imageReader
Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName("JPG"); // 通过ImageIO获取imageReader迭代器
ImageReader imageReader = iterator.next(); // 通过迭代器实例化ImageReader
ImageInputStream imageInputStream = ImageIO.createImageInputStream(srcImage); // 通过ImageIO实例化图像输入流
imageReader.setInput(imageInputStream, true);
// 读取源图片IIOMetadata信息,存入IIOMetadata
ImageReadParam imageReadParam = imageReader.getDefaultReadParam();
IIOImage iioImage = imageReader.readAll(0, imageReadParam);
IIOMetadata iioMetadata = iioImage.getMetadata();
// 读取目标图片信息
Iterator<ImageReader> iterator2 = ImageIO.getImageReadersByFormatName("JPG");
ImageReader imageReader2 = iterator2.next();
ImageInputStream imageInputStream2 = ImageIO.createImageInputStream(destImage);
imageReader2.setInput(imageInputStream2, true);
// 将源图片iioMetadata信息存入目标图片的IIOImage
ImageReadParam imageReadParam2 = imageReader2.getDefaultReadParam();
IIOImage iioImage2 = imageReader2.readAll(0, imageReadParam2);
iioImage2.setMetadata(iioMetadata);
// 将iioImage写入目标图片
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(destImage);
Iterator<ImageWriter> iterator3 = ImageIO.getImageWritersByFormatName("JPG");
ImageWriter imageWriter = iterator3.next();
imageWriter.setOutput(imageOutputStream);
imageWriter.write(iioImage2);
//关闭流
imageInputStream.close();
imageOutputStream.close();
}
}
可以看到,两张不同照片,此时有了一样的属性信息。另外,实际情况下,处理图像数据,为了减少IO次数,可以从IIOImage中取出图像栅格数据,转为BufferedImage对象,对image进行处理后再存入IIOImage;
BufferedImage image = (BufferedImage) iioImage.getRenderedImage();
iioImage.setRenderedImage(image);