我试图以编程方式在Java中设置jpeg图像的dpi元数据.图像的来源是扫描仪,因此我从TWAIN获得水平/垂直分辨率以及图像原始数据.我想保存此信息以获得更好的打印效果.
这是我到目前为止的代码.它将原始图像(byteArray)保存为JPEG文件,但忽略了我通过IIOMetadata指定的X/Ydensity信息.我有什么建议我做错了吗?
任何其他解决方案(第三方库等)也会受到欢迎.
import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import java.io.File; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageTypeSpecifier; import javax.imageio.metadata.IIOMetadata; import javax.imageio.plugins.jpeg.JPEGImageWriteParam; import javax.imageio.stream.ImageOutputStream import org.w3c.dom.Element; import com.sun.imageio.plugins.jpeg.JPEGImageWriter; public boolean saveJpeg(int[] byteArray, int width, int height, int dpi, String file) { BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); WritableRaster wr = bufferedImage.getRaster(); wr.setPixels(0, 0, width, height, byteArray); try { // Image writer JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO.getImageWritersBySuffix("jpeg").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File(file)); imageWriter.setOutput(ios); // Compression JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam(); jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(0.85f); // Metadata (dpi) IIOMetadata data = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(bufferedImage), jpegParams); Element tree = (Element)data.getAsTree("javax_imageio_jpeg_image_1.0"); Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0); jfif.setAttribute("Xdensity", Integer.toString(dpi)); jfif.setAttribute("Ydensity", Integer.toString(dpi)); jfif.setAttribute("resUnits", "1"); // density is dots per inch // Write and clean up imageWriter.write(data, new IIOImage(bufferedImage, null, null), jpegParams); ios.close(); imageWriter.dispose(); } catch (Exception e) { return false; } return true; }
谢谢!
这里没有考虑的一些问题:
树未直接映射到IOMetaData.要从树应用数据,请在设置密度和栅格参数后添加以下调用:
data.setFromTree("javax_imageio_jpeg_image_1.0", tree);
不要将元数据用作写调用中的第一个参数.见JPEGImageWriter#write(IIOMetaData, IIOImage, ImageWriteParam)
.如果streamMetaData不为NULL,则将生成警告(WARNING_STREAM_METADATA_IGNORED).
设置的元数据IOMetadata
的IOImage
.JPEGImageWriter使用这些元数据.然后是正确的写入调用
imageWriter.write(null, new IIOImage(F_scaledImg, null, data), jpegParams);