Java 如何通过itext围绕图像中心旋转?
如何通过itext围绕图像中心旋转Java 如何通过itext围绕图像中心旋转?,java,image,rotation,itext,center,Java,Image,Rotation,Itext,Center,如何通过itext围绕图像中心旋转 公共静态缓冲区图像旋转锁定90(缓冲区图像输入图像){ 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.getScaledH
公共静态缓冲区图像旋转锁定90(缓冲区图像输入图像){
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*/
int width=inputImage.getWidth();
int height=inputImage.getHeight();
BuffereImage returnImage=新的BuffereImage(高度、宽度、inputImage.getType());
对于(int x=0;x
公共静态缓冲区图像旋转锁定90(缓冲区图像输入图像){
int width=inputImage.getWidth();
int height=inputImage.getHeight();
BuffereImage returnImage=新的BuffereImage(高度、宽度、inputImage.getType());
对于(int x=0;x
如果我们有一个图像图像
和坐标x,y
,我们可以不旋转地绘制图像,其左下角位于给定的坐标处,如下所示
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;
}
参考资料中的位图图像大小为1x1,坐标原点位于其左下角。因此,此操作会将图像拉伸到正确的大小,并移动图像,使其左下角位于给定的坐标处
如果我们想要绘制与上面绘制的图像相同的图像,就像上面绘制的图像围绕其中心旋转了一个角度rotate
,因此,我们可以通过移动1x1图像,使原点位于其中心,将其拉伸到其正确的大小,旋转它,然后移动原点(仍然位于旋转图像的中心)到未旋转图像的中心。使用AffineTransform
实例(来自packagecom.itextpdf.awt.geom
)而不是数字元组,这些操作更容易表达。因此:
contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);
(测试方法测试旋转图像
)
例如,使用
// 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);
结果如下:
轻轻地
OP在评论中问道
如何添加旋转和翻转图像
为此,只需在上述变换序列中插入镜像仿射变换
不幸的是,OP没有提到他指的是水平翻转还是垂直翻转。但是,随着旋转角度的改变,一个旋转到另一个旋转,这也不是必须的
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;
(测试方法testAddRotatedFlippedImage
)
结果与上图相同:
带插值
OP在另一条评论中问道
如何消除混叠 iText
Image
类知道一个Interpolation
属性。通过将其设置为true(显然,在将图像添加到文档之前)
低分辨率图像在绘制时会进行插值
例如,使用具有不同颜色像素的2x2图像而不是Willi图像,可以得到以下结果,首先不使用插值,然后使用插值:
授予添加此图像的测试testAddRotatedInterpolatedImage
:
注意:iTextImage
propertyInterpolation
有效地设置PDF图像字典中的Interpolate条目。在此上下文中,PDF规范说明:
注:合规读者可选择不实现PDF的此功能,或使用其希望的任何特定插值实现
因此,在某些查看器上,插值的发生方式可能与在查看器中不同,甚至可能根本不一样。如果您需要在每个查看器上使用特定类型的插值,请在将图像加载到iText
图像之前,使用所需的插值/消除混叠量将图像放大,如果我们有图像和坐标x,y
,我们可以在给定的坐标系下,不旋转地绘制图像,如下图所示
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;
}
参考资料中的位图图像大小为1x1,坐标原点位于其左下角。因此,此操作会将图像拉伸到正确的大小,并移动图像,使其左下角位于给定的坐标处
如果我们想要绘制与上面绘制的图像相同的图像,就像上面绘制的图像围绕其中心旋转了一个角度rotate
,因此,我们可以通过移动1x1图像,使原点位于其中心,将其拉伸到其正确的大小,旋转它,然后移动原点(仍然位于旋转图像的中心)到未旋转图像的中心。使用AffineTransform
实例(来自packagecom.itextpdf.awt.geom
)而不是数字元组,这些操作更容易表达。因此:
contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);
(测试方法测试旋转图像
)
例如,使用
// 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);
结果如下:
轻轻地
OP在评论中问道
如何添加旋转和翻转图像
为此,只需在上述变换序列中插入镜像仿射变换
不幸的是,OP没有提到他指的是水平翻转还是垂直翻转。但是,随着旋转角度的改变,一个旋转到另一个旋转,这也不是必须的
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;
(测试方法testAddRotatedFlippedImage
)
结果与上图相同:
带插值
OP在另一条评论中问道
如何消除混叠
iTextImage
类知道一个Interpolation
属性。通过将其设置为true(显然,在将图像添加到文档之前)
低分辨率图像在绘制时会进行插值
例如,使用带有不同c的2x2图像