如何通过itext围绕图像中心旋转?

How to rotate around the image center by itext?

double degPi = degrees * Math.PI / 180;   
double a = Math.cos(degPi)*tImgCover.getScaledHeight();
double b = Math.sin(degPi)*tImgCover.getScaledWidth();
double c = -Math.sin(degPi) * tImgCover.getScaledHeight();
double d = Math.cos(degPi)* tImgCover.getScaledWidth();
double e = absX;
double f = absY;

contentByte.addImage(imgae, a, b, c, d, e, f);/*add image*/

如何通过itext围绕图片中心旋转?

    public static BufferedImage rotateClockwise90( BufferedImage inputImage ){
        int width = inputImage.getWidth();
        int height = inputImage.getHeight();
        BufferedImage returnImage = new BufferedImage( height, width , inputImage.getType()  );

        for( int x = 0; x < width; x++ ) {
            for( int y = 0; y < height; y++ ) {
                returnImage.setRGB( height-y-1, x, inputImage.getRGB( x, y  )  );

            }
        }
        return returnImage;
}

如果我们有一个Image image和坐标x, y,我们可以像这样在给定的坐标处不旋转地绘制图像

contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);

资源中的位图图像大小为 1x1,坐标原点在其左下角。因此,此操作将图像拉伸到正确大小并移动它,使其左下角位于给定坐标处。

如果我们想绘制与上面绘制的图像绕其中心旋转一个角度 rotate 相同的图像,因此,我们可以通过移动 1x1 图像使原点位于它的中心,将其拉伸到正确的大小,旋转它,然后将原点(仍然位于旋转图像的中心)移动到未旋转图像的中心。使用 AffineTransform 实例(来自包 com.itextpdf.awt.geom)而不是数字元组更容易表达这些操作。因此:

// Draw image as if the previous image was rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Stretch it to its dimensions
AffineTransform B = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform C = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform D = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
//Draw
contentByte.addImage(image, M);

(AddRotatedImage.java测试方法testAddRotatedImage)

例如使用

绘制两个图像
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;

结果如下:

翻转

OP 在评论中提问

how to add rotate and flip image?

为此,您只需将镜像仿射变换插入到上述变换序列中即可。

不幸的是,OP 没有提到他指的是水平翻转还是垂直翻转。但由于改变旋转角度相应地改变了一个在另一个中,这也不是真正必要的。

// Draw image as if the previous image was flipped and rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Flip it horizontally
AffineTransform B = new AffineTransform(-1, 0, 0, 1, 0, 0);
// Stretch it to its dimensions
AffineTransform C = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform D = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform E = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
M.preConcatenate(E);
//Draw
contentByte.addImage(image, M);

(AddRotatedImage.java测试方法testAddRotatedFlippedImage)

与上图相同的结果:

有插值

OP 在另一条评论中提问

How anti aliasing ?

iText Image class 知道 Interpolation 属性。通过将其设置为 true( 将图像添加到文档之前,很明显),

image.setInterpolation(true);

低分辨率图像在绘制时会进行插值。

例如使用具有不同颜色像素的 2x2 图像而不是 Willi 的图像,您会得到以下结果,首先没有插值,然后有插值:

赋予 AddRotatedImage.java 测试 testAddRotatedInterpolatedImage 添加此图像:

注意: iText Image 属性 Interpolation 有效地设置 Interpolate 条目PDF图像词典。此上下文中的 PDF 规范说明:

NOTE A conforming Reader may choose to not implement this feature of PDF, or may use any specific implementation of interpolation that it wishes.

因此,在某些查看器上,插值的发生可能与您的查看器不同,甚至可能根本没有。如果您需要对每个查看器进行特定类型的插值,请在将图像加载到 iText Image.

之前使用所需的插值量 / anti-aliasing 放大图像