首页 > 解决方案 > 如何使用 Java 计算缓冲图像的 DPI

问题描述

首先,我必须将图像转换为 Base64,然后通过将 base64 转换为缓冲图像来计算图像的 DPI。

这是我尝试过的一些代码,它适用于 image.PNG(.png) 格式,但以其他格式返回 -1(错误输出),如 (.jpg)...!但我也需要这个也适用于其他格式。

 private static float base64toBuffer(String inputbuffer) throws IOException, ImageReadException{
         byte[] Rimage=decodeToImage(inputbuffer);
       final org.apache.sanselan.ImageInfo imageInfo = Sanselan.getImageInfo(Rimage);
       final int physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
       final int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
        return physicalWidthDpi;

    }
     private static byte[] buffertoByte(String imageString) {

        byte[] imageByte;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            imageByte = decoder.decodeBuffer(imageString);
            return imageByte;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

标签: javaimage-processing

解决方案


推荐阅读