parent
af4d2f6e28
commit
e38935c147
@ -1,20 +1,91 @@
|
||||
import com.github.jaiimageio.jpeg2000.impl.J2KImageReader;
|
||||
import com.github.jaiimageio.jpeg2000.impl.J2KImageReaderSpi;
|
||||
import org.apache.commons.imaging.ImageInfo;
|
||||
import org.apache.commons.imaging.ImageReadException;
|
||||
import org.apache.commons.imaging.ImageWriteException;
|
||||
import org.apache.commons.imaging.Imaging;
|
||||
import org.apache.commons.imaging.common.ImageMetadata;
|
||||
import org.apache.commons.imaging.common.RationalNumber;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.metadata.IIOMetadata;
|
||||
import javax.imageio.metadata.IIOMetadataNode;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Jpm2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 指定 JP2 图像文件路径
|
||||
String filePath = "C:\\Users\\zhanghai\\Documents\\WeChat Files\\wxid_wexo4ubjorso22\\FileStorage\\File\\2023-11\\1.jpm"; // 替换成你的 JP2 图像文件路径
|
||||
|
||||
// 创建 ImageInputStream
|
||||
ImageInputStream iis = ImageIO.createImageInputStream(new File(filePath));
|
||||
|
||||
// 获取 DPI 信息
|
||||
float xDpi = getDPI(iis, "HorizontalPixelSize");
|
||||
float yDpi = getDPI(iis, "VerticalPixelSize");
|
||||
|
||||
System.out.println("X DPI: " + xDpi);
|
||||
System.out.println("Y DPI: " + yDpi);
|
||||
|
||||
// 关闭 ImageInputStream
|
||||
iis.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static float getDPI(ImageInputStream iis, String elementName) {
|
||||
float dpi = -1.0f;
|
||||
try {
|
||||
// 获取 ImageReader
|
||||
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
|
||||
if (readers.hasNext()) {
|
||||
ImageReader reader = readers.next();
|
||||
|
||||
// 设置输入流
|
||||
reader.setInput(iis);
|
||||
|
||||
// 获取第一个图像的元数据
|
||||
IIOMetadata metadata = reader.getImageMetadata(0);
|
||||
|
||||
// 获取 DPI 信息
|
||||
String value = getMetadataValue(metadata, elementName);
|
||||
if (value != null) {
|
||||
dpi = Float.parseFloat(value);
|
||||
}
|
||||
|
||||
// 关闭 ImageReader
|
||||
reader.dispose();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return dpi;
|
||||
}
|
||||
|
||||
private static String getMetadataValue(IIOMetadata metadata, String elementName) {
|
||||
String value = null;
|
||||
String[] names = metadata.getMetadataFormatNames();
|
||||
for (String name : names) {
|
||||
IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(name);
|
||||
value = getMetadataValueRecursive(root, elementName);
|
||||
if (value != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String getMetadataValueRecursive(IIOMetadataNode node, String elementName) {
|
||||
if (node.getNodeName().equals(elementName)) {
|
||||
return node.getAttribute("value");
|
||||
}
|
||||
|
||||
for (int i = 0; i < node.getLength(); i++) {
|
||||
String value = getMetadataValueRecursive((IIOMetadataNode) node.item(i), elementName);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue