add negative support

This commit is contained in:
Looly 2019-09-17 17:46:46 +08:00
parent be91b672d2
commit 8b94fa0c5d
2 changed files with 13 additions and 8 deletions

View File

@ -6,6 +6,7 @@
## 4.6.7 ## 4.6.7
### 新特性 ### 新特性
* 【core】 ImgUtil.rotate支持负数issue#543@Github
### Bug修复 ### Bug修复
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -202,7 +202,7 @@ public class Img implements Serializable{
// PNG图片特殊处理 // PNG图片特殊处理
if (ImgUtil.IMAGE_TYPE_PNG.equals(this.targetImageType)) { if (ImgUtil.IMAGE_TYPE_PNG.equals(this.targetImageType)) {
final AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance((double) scale, (double) scale), null); final AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
this.targetImage = op.filter(ImgUtil.toBufferedImage(srcImg), null); this.targetImage = op.filter(ImgUtil.toBufferedImage(srcImg), null);
} else { } else {
final String scaleStr = Float.toString(scale); final String scaleStr = Float.toString(scale);
@ -311,7 +311,7 @@ public class Img implements Serializable{
*/ */
public Img cut(Rectangle rectangle) { public Img cut(Rectangle rectangle) {
final Image srcImage = getValidSrcImg(); final Image srcImage = getValidSrcImg();
rectangle = fixRectangle(rectangle, srcImage.getWidth(null), srcImage.getHeight(null)); fixRectangle(rectangle, srcImage.getWidth(null), srcImage.getHeight(null));
final ImageFilter cropFilter = new CropImageFilter(rectangle.x, rectangle.y, rectangle.width, rectangle.height); final ImageFilter cropFilter = new CropImageFilter(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
final Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcImage.getSource(), cropFilter)); final Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcImage.getSource(), cropFilter));
@ -476,7 +476,7 @@ public class Img implements Serializable{
public Img pressImage(Image pressImg, Rectangle rectangle, float alpha) { public Img pressImage(Image pressImg, Rectangle rectangle, float alpha) {
final Image targetImg = getValidSrcImg(); final Image targetImg = getValidSrcImg();
rectangle = fixRectangle(rectangle, targetImg.getWidth(null), targetImg.getHeight(null)); fixRectangle(rectangle, targetImg.getWidth(null), targetImg.getHeight(null));
this.targetImage = draw(ImgUtil.toBufferedImage(targetImg), pressImg, rectangle, alpha); this.targetImage = draw(ImgUtil.toBufferedImage(targetImg), pressImg, rectangle, alpha);
return this; return this;
} }
@ -499,8 +499,8 @@ public class Img implements Serializable{
// 抗锯齿 // 抗锯齿
graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// 从中心旋转 // 从中心旋转
graphics2d.translate((rectangle.width - width) / 2, (rectangle.height - height) / 2); graphics2d.translate((rectangle.width - width) / 2D, (rectangle.height - height) / 2D);
graphics2d.rotate(Math.toRadians(degree), width / 2, height / 2); graphics2d.rotate(Math.toRadians(degree), width / 2D, height / 2D);
graphics2d.drawImage(image, 0, 0, null); graphics2d.drawImage(image, 0, 0, null);
graphics2d.dispose(); graphics2d.dispose();
this.targetImage = targetImg; this.targetImage = targetImg;
@ -631,7 +631,7 @@ public class Img implements Serializable{
} }
/** /**
* 修正矩形框位置如果{@link Img#setPositionFromCentre(boolean)} 设为{@code true}则坐标修正为基于图形中心否则基于左上角 * 修正矩形框位置如果{@link Img#setPositionBaseCentre(boolean)} 设为{@code true}则坐标修正为基于图形中心否则基于左上角
* *
* @param rectangle 矩形 * @param rectangle 矩形
* @param baseWidth 参考宽 * @param baseWidth 参考宽
@ -643,8 +643,8 @@ public class Img implements Serializable{
if (this.positionBaseCentre) { if (this.positionBaseCentre) {
// 修正图片位置从背景的中心计算 // 修正图片位置从背景的中心计算
rectangle.setLocation(// rectangle.setLocation(//
rectangle.x + (int) (Math.abs(baseWidth - rectangle.width) / 2), // rectangle.x + (Math.abs(baseWidth - rectangle.width) / 2), //
rectangle.y + (int) (Math.abs(baseHeight - rectangle.height) / 2)// rectangle.y + (Math.abs(baseHeight - rectangle.height) / 2)//
); );
} }
return rectangle; return rectangle;
@ -660,6 +660,10 @@ public class Img implements Serializable{
* @since 4.1.20 * @since 4.1.20
*/ */
private static Rectangle calcRotatedSize(int width, int height, int degree) { private static Rectangle calcRotatedSize(int width, int height, int degree) {
if(degree < 0){
// 负数角度转换为正数角度
degree += 360;
}
if (degree >= 90) { if (degree >= 90) {
if (degree / 90 % 2 == 1) { if (degree / 90 % 2 == 1) {
int temp = height; int temp = height;