gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:40:34 24 4
gpt4 key购买 nike

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围绕图像中心旋转?

最佳答案

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

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

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

如果我们想绘制同一张图像,就好像上面绘制的图像围绕其中心旋转了一个角度rotate,因此,我们可以通过移动 1x1 图像来做到这一点,使原点位于在其中心,将其拉伸(stretch)到正确的大小,旋转它,然后将原点(仍位于旋转图像的中心)移动到未旋转图像的中心。使用 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;

结果是这样的:

Screenshot

翻转一下

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)

与上图相同的结果:

Screenshot

有插值

OP 在另一条评论中问道

How anti aliasing ?

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

image.setInterpolation(true);

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

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

rotatedInterpolatedImage.pdf screenshot

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

注意:iText Image 属性Interpolation 有效地设置了PDF 图像字典中的Interpolate 条目。此上下文中的 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 之前使用所需数量的插值/抗锯齿来放大图像。

关于java - 如何通过itext围绕图像中心旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com