diff --git a/pom.xml b/pom.xml index 7bbfff3..e1e929a 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,13 @@ 1.3.0 + + org.im4java + im4java + 1.4.0 + + + diff --git a/src/main/java/com/docus/sw/fenpan/FenpanService.java b/src/main/java/com/docus/sw/fenpan/FenpanService.java index 52bc8bd..4896bc8 100644 --- a/src/main/java/com/docus/sw/fenpan/FenpanService.java +++ b/src/main/java/com/docus/sw/fenpan/FenpanService.java @@ -7,6 +7,8 @@ import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.Imaging; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -16,7 +18,7 @@ import java.util.*; @Slf4j public class FenpanService { - public void fenpan(String saveUrl,String readUrl){ + public void fenpan(String saveUrl, String readUrl) { try { FenpanService main = new FenpanService(); Map zongMap = main.readFile(readUrl); @@ -85,7 +87,7 @@ public class FenpanService { Config.loadConfig(); String saveUrl = Config.getParam("file.save.url"); String readUrl = Config.getParam("file.read.url"); - new FenpanService().fenpan(saveUrl,readUrl); + new FenpanService().fenpan(saveUrl, readUrl); } @@ -112,28 +114,46 @@ public class FenpanService { File[] files = sourceFile.listFiles(); for (File file : files) { //非图片模式,跳过。 - if(!(file.getName().endsWith(".jpg") || file.getName().endsWith(".png") + if (!(file.getName().endsWith(".jpg") || file.getName().endsWith(".png") || file.getName().endsWith(".jpeg") || file.getName().endsWith(".tif") - || file.getName().endsWith(".tiff"))){ - continue; + || file.getName().endsWith(".tiff")) || file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm")) { + continue; } - try { - ImageInfo imageInfo = Imaging.getImageInfo(file); - int height = imageInfo.getHeight(); - int width = imageInfo.getWidth(); - int physicalHeightDpi = imageInfo.getPhysicalHeightDpi(); - Document document = new Document(width, height, physicalHeightDpi); - documentList.add(document); - } catch (IOException e) { - FileUtils.delete(file); - throw new RuntimeException("非图片格式", e); - } catch (ImageReadException e) { - FileUtils.delete(file); - throw new RuntimeException(e); - } catch (IllegalArgumentException e) { - FileUtils.delete(file); + if (file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm")) { + // 读取 JPEG 2000 图像文件 + + try { + BufferedImage image = ImageIO.read(file); + int height = image.getHeight(); + int width = image.getWidth(); + Document document = new Document(width, height, 300); + documentList.add(document); + } catch (IOException e) { + throw new RuntimeException(e); + } +// ImageInfo imageInfo = Imaging.getImageInfo(inputFile); + + } else { + try { + ImageInfo imageInfo = Imaging.getImageInfo(file); + int height = imageInfo.getHeight(); + int width = imageInfo.getWidth(); + int physicalHeightDpi = imageInfo.getPhysicalHeightDpi(); + Document document = new Document(width, height, physicalHeightDpi); + documentList.add(document); + } catch (IOException e) { + FileUtils.delete(file); + throw new RuntimeException("非图片格式", e); + } catch (ImageReadException e) { + FileUtils.delete(file); + throw new RuntimeException(e); + } catch (IllegalArgumentException e) { + FileUtils.delete(file); + } } + + } piece.put(documentList); } @@ -160,7 +180,7 @@ public class FenpanService { zongMap.put(zongName, zong); } - for(String zongName: zongMap.keySet()){ + for (String zongName : zongMap.keySet()) { Zong zong = zongMap.get(zongName); File zongfile = new File(zong.getAbsolutePath()); File[] files = zongfile.listFiles(); @@ -183,7 +203,7 @@ public class FenpanService { }; Arrays.sort(files, fileComparator); - for(File file:files){ + for (File file : files) { Roll roll = rollMap.get(file.getName()); zong.put(roll); } diff --git a/src/main/java/com/docus/sw/fenpan/MyApplication.java b/src/main/java/com/docus/sw/fenpan/MyApplication.java index 6d56e39..731ce05 100644 --- a/src/main/java/com/docus/sw/fenpan/MyApplication.java +++ b/src/main/java/com/docus/sw/fenpan/MyApplication.java @@ -19,18 +19,30 @@ public class MyApplication { public static void main(String[] args) throws IOException { Config.loadConfig(); // 获取系统支持的外观信息 - UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); - - // 遍历可用的外观并设置所选外观 - for (UIManager.LookAndFeelInfo info : lookAndFeels) { - if ("Nimbus".equals(info.getName())) { // 选择Nimbus外观 - try { - UIManager.setLookAndFeel(info.getClassName()); - break; - } catch (Exception e) { - e.printStackTrace(); - } - } +// UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); +// +// // 遍历可用的外观并设置所选外观 +// for (UIManager.LookAndFeelInfo info : lookAndFeels) { +// if ("Nimbus".equals(info.getName())) { // 选择Nimbus外观 +// try { +// UIManager.setLookAndFeel(info.getClassName()); +// break; +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// } + + try { + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (UnsupportedLookAndFeelException e) { + throw new RuntimeException(e); } SwingUtilities.invokeLater(() -> createAndShowGUI()); diff --git a/src/main/java/com/docus/sw/souyin/SuoyinService.java b/src/main/java/com/docus/sw/souyin/SuoyinService.java index 2aaf75e..9b69912 100644 --- a/src/main/java/com/docus/sw/souyin/SuoyinService.java +++ b/src/main/java/com/docus/sw/souyin/SuoyinService.java @@ -11,6 +11,8 @@ import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.Imaging; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -71,25 +73,42 @@ public class SuoyinService { //非图片模式,跳过。 if(!(docfile.getName().endsWith(".jpg") || docfile.getName().endsWith(".png") || docfile.getName().endsWith(".jpeg") || docfile.getName().endsWith(".tif") - || docfile.getName().endsWith(".tiff"))){ + || docfile.getName().endsWith(".tiff")|| file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm"))){ continue; } - try { - ImageInfo imageInfo = Imaging.getImageInfo(docfile); - int height = imageInfo.getHeight(); - int width = imageInfo.getWidth(); - int physicalHeightDpi = imageInfo.getPhysicalHeightDpi(); - Document document = new Document(width, height, physicalHeightDpi); - documentList.add(document); - } catch (IOException e) { + if (file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm")) { + // 读取 JPEG 2000 图像文件 + + try { + BufferedImage image = ImageIO.read(file); + int height = image.getHeight(); + int width = image.getWidth(); + Document document = new Document(width, height, 300); + documentList.add(document); + } catch (IOException e) { + throw new RuntimeException(e); + } +// ImageInfo imageInfo = Imaging.getImageInfo(inputFile); + + } else { + try { + ImageInfo imageInfo = Imaging.getImageInfo(docfile); + int height = imageInfo.getHeight(); + int width = imageInfo.getWidth(); + int physicalHeightDpi = imageInfo.getPhysicalHeightDpi(); + Document document = new Document(width, height, physicalHeightDpi); + documentList.add(document); + } catch (IOException e) { // FileUtils.delete(file); - throw new RuntimeException("非图片格式", e); - } catch (ImageReadException e) { + throw new RuntimeException("非图片格式", e); + } catch (ImageReadException e) { // FileUtils.delete(file); - throw new RuntimeException(e); - } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalArgumentException e) { // FileUtils.delete(file); + } } + } pieces.put(documentList); pieceList.add(pieces); diff --git a/src/test/java/Jpm2.java b/src/test/java/Jpm2.java index d2b27b5..1677286 100644 --- a/src/test/java/Jpm2.java +++ b/src/test/java/Jpm2.java @@ -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 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; + } } diff --git a/src/test/java/JpmTest.java b/src/test/java/JpmTest.java index 39d45a0..253f8d1 100644 --- a/src/test/java/JpmTest.java +++ b/src/test/java/JpmTest.java @@ -39,7 +39,7 @@ public class JpmTest { // int physicalHeightDpi = imageInfo.getPhysicalHeightDpi(); - String filePath = "C:\\Users\\zhanghai\\Documents\\WeChat Files\\wxid_wexo4ubjorso22\\FileStorage\\File\\2023-11\\JP2/16-35数转模 - 0001.jp2"; // 替换成你的 JP2 图像文件路径 + String filePath = "C:\\Users\\zhanghai\\Documents\\WeChat Files\\wxid_wexo4ubjorso22\\FileStorage\\File\\2023-11\\1.jpm"; // 替换成你的 JP2 图像文件路径 try { // 读取 JPEG 2000 图像文件 @@ -57,7 +57,7 @@ public class JpmTest { e.printStackTrace(); } - new JpmTest().viewExif(filePath); +// new JpmTest().viewExif(filePath); } // public static void main3(String[] args) {