如何计算检索到的pdf图像每个顶点的坐标

时间:2019-04-17 13:18:32

标签: java pdf coordinates pdfbox affinetransform

我要计算提取图像的顶点坐标

我使用PDFBOX检索了图像。然后我开始检查矩阵

Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();

我做了很多尝试来确定每个角度的坐标,但是无法发明通用解决方案(正常图像,翻转,旋转等)

public class StreamEngine extends PDFStreamEngine {

    private ImageFile buildImage(final PDXObject element) throws IOException {
        final PDRectangle cropBox = getCurrentPage().getCropBox();
        final PDRectangle mediaBox = getCurrentPage().getMediaBox();
        final PDImageXObject image = (PDImageXObject) element;
        Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
        final double imageWidth = ctmNew.getScalingFactorX();
        final double imageHeight = ctmNew.getScalingFactorY();
        final double bottom = mediaBox.getHeight() - ctmNew.getTranslateY(); //I need coordinates started from top
        final double left = ctmNew.getTranslateX();
        final ImageFile imageFile = new ImageFile();
        imageFile.setImageFormat("png");
        imageFile.setImageHeight(imageHeight);
        imageFile.setImageWidth(imageWidth);
        imageFile.setLeft(left);
        imageFile.setPageWidth(cropBox.getWidth());
        imageFile.setPageHeight(cropBox.getHeight());
        imageFile.setBottom(bottom);
// do proper calculation here
        return imageFile;
    }
}

当前,我没有收到翻转和旋转图像的正确坐标。而且每次我以某种方式调整特定案例的代码时,以前的案例都会被破坏。

请不要修复上面的代码,只给我一些建议,如何计算坐标。

scheme of the data required

1 个答案:

答案 0 :(得分:0)

答案很简单 链接:https://code-industry.net/masterpdfeditor-help/transformation-matrix/

    private void calculateCoordinates(final ImageFile imageFile, final Matrix ctmNew) {

        double llx = calculateXCoordinate(ctmNew, 0, 0);
        double lly = calculateYCoordinate(ctmNew, 0, 0);

        double ulx = calculateXCoordinate(ctmNew, 0, 1);
        double uly = calculateYCoordinate(ctmNew, 0, 1);

        double lrx = calculateXCoordinate(ctmNew, 1, 0);
        double lry = calculateYCoordinate(ctmNew, 1, 0);

        double urx = calculateXCoordinate(ctmNew, 1, 1);
        double ury = calculateYCoordinate(ctmNew, 1, 1);

        final double left = Math.min(Math.min(llx, ulx), Math.min(lrx, urx));
        final double top = Math.max(Math.max(lly, uly), Math.max(lry, ury));

        final double right = Math.max(Math.max(llx, ulx), Math.max(lrx, urx));
        final double bottom = Math.min(Math.min(lly, uly), Math.min(lry, ury));

        imageFile.setTop(top);
        imageFile.setBottom(bottom);
        imageFile.setLeft(left);
        imageFile.setRight(right);
    }

    private double calculateXCoordinate(final Matrix ctmNew, final double x, final double y) {
        double a = ctmNew.getValue(0, 0);
        double c = ctmNew.getValue(1, 0);
        double e = ctmNew.getValue(2, 0);
        return a * x + c * y + e;
    }

    private double calculateYCoordinate(final Matrix ctmNew, final double x, final double y) {
        double b = ctmNew.getValue(0, 1);
        double d = ctmNew.getValue(1, 1);
        double f = ctmNew.getValue(2, 1);
        return b * x + d * y + f;
    }