检查java中的图像是否为200dpi

时间:2015-01-01 09:56:46

标签: java image pdfbox

我想检查pdf中的图像是否为200dpi,以及当前我是否使用pdfbox从PDF中提取图像但是我是否可以使用任何其他免费库来执行此操作在java中也可以工作。

在Pdfbox中,我能够获得pdf中存在的图像的pdxObjectImage,但有时很多情况下,从这些pdf中提取的图像中没有元数据。

String key = (String) imageIter.next();
PDXObjectImage pdxObjectImage = (PDXObjectImage)pageImages.get(key);
PDMetadata metadata=pdxObjectImage.getMetadata();
if (metadata == null) {
    System.out.println("No Metadata found for this image.");
} else {                
    try {
        xmlInputStream = metadata.createInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
xmlInputStream = metadata.createInputStream();

他们是一种计算图像dpi的方法。为了查找是否可以使用此颜色

pdxObjectImage.getColorSpace().getJavaColorSpace().isCS_sRGB()

或者是他们做其他任何其他更好的技术

我们如何在java中做到这一点。

1 个答案:

答案 0 :(得分:0)

我没有使用过pdfbox。但是如果你可以从pdf中使用pdfbox提取图像,那么你可以使用apache common-imaging lib进行进一步处理。使用这个lib的优点是它提供了大量的图像处理。

示例代码段可能对您有所帮助 -

import java.io.File;
import java.io.IOException;

import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;

public class ImageingTest{


    private static File fileName = new File("/home/razib/Desktop/img/cactus.jpg");

    private static ImageInfo imageInfo;

    public static void main(String[] args){

        int physicalWidthDpi = 0;
        int physicalHeightDpi = 0;

        try {


            imageInfo = Imaging.getImageInfo(fileName);
            physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
            physicalHeightDpi = imageInfo.getPhysicalHeightDpi();

            System.out.println("File Name: " + fileName.getName() + " File Format: " +imageInfo.getFormatName());
            System.out.println("Dimension: " +imageInfo.getHeight() +"X"+ imageInfo.getWidth());
            System.out.println("physicalWidthDpi: " +physicalWidthDpi 
                    +"\nphysicalHeightDpi: " +physicalHeightDpi);

            System.out.println();
            System.out.println("COLOR_TYPE_BW: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_BW));
            System.out.println("COLOR_TYPE_CMYK: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_CMYK));
            System.out.println("COLOR_TYPE_GRAYSCALE: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_GRAYSCALE));
            System.out.println("COLOR_TYPE_RGB: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_RGB));
            System.out.println("COLOR_TYPE_YCbCr: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_YCbCr));
            System.out.println("COLOR_TYPE_YCC: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_YCC));
            System.out.println("COLOR_TYPE_YCCK: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_YCCK));
            System.out.println("COLOR_TYPE_OTHER: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_OTHER));
            System.out.println("COLOR_TYPE_UNKNOWN: " +(imageInfo.getColorType()==ImageInfo.COLOR_TYPE_UNKNOWN));

        } catch (ImageReadException e) {
            //Handle exception
            e.printStackTrace();
        } catch (IOException e) {
            //Handle exception
            e.printStackTrace();
        }
    }

}