使用Apache Commons Imaging 图像处理库
引入依赖

// https://mvnrepository.com/artifact/org.apache.commons/commons-imaging
compile group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-alpha1'

编写测试方法

public static void main(String[] args) throws Exception {
        ImageInfo imageInfo = Imaging.getImageInfo(new File("C:\\Users\\Administrator\\Desktop\\图片素材\\2.jpg"));
        System.out.println("dpi为"+imageInfo.getPhysicalHeightDpi());
}

方法执行成功打印Dpi

Q.E.D.


愿你编码半生,归来仍是少年