[英]How to get the dpi of an image(Java)
In c#, we can use Image.HorizontalResolution and Image.VerticalResolution. 在c#中,我们可以使用Image.HorizontalResolution和Image.VerticalResolution。
But in java, how to get it? 但在java中,如何获得它?
I found ImageInfo.java
, but it only support a few image types. 我找到了
ImageInfo.java
,但它只支持一些图像类型。
http://kickjava.com/src/imageinfo/ImageInfo.java.htm http://kickjava.com/src/imageinfo/ImageInfo.java.htm
You can use Apache Commons Sanselan library to get image info: http://commons.apache.org/imaging/index.html . 您可以使用Apache Commons Sanselan库来获取图像信息: http ://commons.apache.org/imaging/index.html。
final ImageInfo imageInfo = Sanselan.getImageInfo(file_);
final int physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
final int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
With the help of an ImageReader instance, you can get the image meta data in a neutral format, and then parse it for what you need. 借助ImageReader实例,您可以以中性格式获取图像元数据,然后根据需要进行解析。 A DTD is here .
DTD就在这里 。
ImageInputStream iis = ImageIO.createImageInputStream(new File(path));
Iterator it = ImageIO.getImageReaders(iis);
if (!it.hasNext())
{
System.err.println("No reader for this format");
return;
}
ImageReader reader = (ImageReader) it.next();
reader.setInput(iis);
IIOMetadata meta = reader.getImageMetadata(0);
IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree("javax_imageio_1.0");
NodeList nodes = root.getElementsByTagName("HorizontalPixelSize");
if (nodes.getLength() > 0)
{
IIOMetadataNode dpcWidth = (IIOMetadataNode) nodes.item(0);
NamedNodeMap nnm = dpcWidth.getAttributes();
Node item = nnm.item(0);
int xDPI = Math.round(25.4f / Float.parseFloat(item.getNodeValue()));
System.out.println("xDPI: " + xDPI);
}
else
System.out.println("xDPI: -");
if (nodes.getLength() > 0)
{
nodes = root.getElementsByTagName("VerticalPixelSize");
IIOMetadataNode dpcHeight = (IIOMetadataNode) nodes.item(0);
NamedNodeMap nnm = dpcHeight.getAttributes();
Node item = nnm.item(0);
int yDPI = Math.round(25.4f / Float.parseFloat(item.getNodeValue()));
System.out.println("yDPI: " + yDPI);
}
else
System.out.println("yDPI: -");
(Source/Inspiration: David Thielen ) (来源/灵感: David Thielen )
Note that you will get a dpi only if it is there. 请注意,只有在存在时才会获得dpi。
If you wonder what's in the Metadata XML, use this code: 如果您想知道元数据XML中的内容,请使用以下代码:
StringWriter xmlStringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(xmlStringWriter);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // http://stackoverflow.com/a/1264872/535646
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource domSource = new DOMSource(root);
transformer.transform(domSource, streamResult);
System.out.println (xmlStringWriter);
It is working for me. 它对我有用。
try {
final ImageInfo imageInfo = Sanselan.getImageInfo(new File("C:/Users/AngryMan/Desktop/abc.png"));
final int physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
final int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
System.out.println("physicalWidthDpi :"+physicalWidthDpi );
System.out.println("physicalHeightDpi : "+physicalHeightDpi);
} catch (Exception e) {
e.printStackTrace();
}
Maven Depndency Maven Depndency
<!-- https://mvnrepository.com/artifact/org.apache.sanselan/sanselan -->
<dependency>
<groupId>org.apache.sanselan</groupId>
<artifactId>sanselan</artifactId>
<version>0.97-incubator</version>
</dependency>
Find dpi of .bmp image use : 找到.bmp图片使用的dpi:
import com.lowagie.text.Image.
public class BitmapResolution {
public static void main(String args[]) {
try {
Image img = Image.getInstance("C:/Users/AngryMan/Desktop/img003.bmp");
System.out.println(img.getDpiX());
System.out.println(img.getDpiY());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Maven Dependency : Maven依赖:
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
Get your ImageReader instance. 获取ImageReader实例。 Then use the first ImageReader, set the Input and read IIOImage or only getImageMetadata(pageIndex).
然后使用第一个ImageReader,设置Input并读取IIOImage或仅设置getImageMetadata(pageIndex)。 You get the image format neutral metadata xml and parse it for the desired data.
您将获得图像格式中性元数据xml并解析它以获取所需数据。
ImageInputStream iis = ImageIO.createImageInputStream(in);
Iterator it = ImageIO.getImageReaders(iis);
if (!it.hasNext()) {
System.outprintln("No reader for this format");
}
ImageReader reader = (ImageReader) it.next();
reader.setInput(iis);
IIOMetadata meta = reader.getImageMetadata(0);
IIOMetadataNode dimNode = meta.getStandardDimensionNode();
NodeList nodes = dimNode.getElementsByTagName("HorizontalPixelSize");
IIOMetadataNode dpcWidth = (IIOMetadataNode)nodes.nextElement();
nodes = dimNode.getElementsByTagName("VerticalPixelSize");
IIOMetadataNode dpcHeight = (IIOMetadataNode)nodes.nextElement();
// ... calc dot per centimeter to dpi : dpi = dpc / 2.54 // ...每厘米到dpi的计算点数:dpi = dpc / 2.54
The whole image neutral metadata format at 整个图像中性元数据格式在
ImageMagick is a powerful tool for all image related work. ImageMagick是所有图像相关工作的强大工具。 IM needs to be installed and requires some configuration for the environment but it's worth the trouble.
IM需要安装并需要对环境进行一些配置,但这是值得的。
http://www.imagemagick.org http://www.imagemagick.org
I recommend you use JMagick wit IM: 我建议你使用JMagick机智IM:
http://www.jmagick.org http://www.jmagick.org
I won't explain the details on how to since it is documented in urls given. 我不会解释有关如何的细节,因为它是在给出的网址中记录的。
I found this example interesting: 我发现这个例子很有趣:
ByteArrayInputStream bis = new
ByteArrayInputStream(uploadedFile.getContents());
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = (ImageReader) readers.next();
IIOMetadata meta = reader.getImageMetadata(0);
Element tree = (Element) meta.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);
int dpiH = Integer.parseInt( jfif.getAttribute("Xdensity") );
int dpiV = Integer.parseInt( jfif.getAttribute("Ydensity") );
/* now test that (dpiH == dpiV) */
/* imports are:
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import org.primefaces.model.UploadedFile;
import org.w3c.dom.Element;
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.