Skip to content

Commit

Permalink
finished tools of image.
Browse files Browse the repository at this point in the history
  • Loading branch information
joyang1 committed Sep 2, 2019
1 parent 549b456 commit 9d5d803
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 11 deletions.
118 changes: 108 additions & 10 deletions src/main/java/cn/tommyyang/jtools/image/ImageTools.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cn.tommyyang.jtools.image;

import sun.font.FontDesignMetrics;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
* @Author : TommyYang
Expand Down Expand Up @@ -39,29 +41,123 @@ public static void addWaterMark(String imgPath, ImageWatermark watermark, String

Graphics2D g = bfImage.createGraphics();
g.drawImage(img, 0, 0, null);
Image markImg = ImageIO.read(new File(watermark.getImgPath()));
BufferedImage newMarkImg = rotateImg(markImg, watermark.getRotate(), watermark.getAlpha());
g.drawImage(newMarkImg, watermark.getX(), watermark.getY(), null);
BufferedImage markImg = ImageIO.read(new File(watermark.getImgPath()));
BufferedImage newMarkImg = rotateImg(markImg, watermark.getRotate());
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, watermark.getAlpha()));
g.drawImage(newMarkImg, watermark.getX() - newMarkImg.getWidth() / 2, watermark.getY() - newMarkImg.getHeight() / 2, null);
g.dispose();

ImageIO.write(bfImage, watermark.getFormatName(), new File(destPath));
} catch (Exception e) {
throw new RuntimeException("add image watermark error");
}
}

private static BufferedImage rotateImg(Image img, double rotate, float alpha) {
/**
*
* 旋转图片方法一
*
* @param img 目标图片
*
* @param rotate 旋转的角度
*
* */
public static BufferedImage rotateImg(BufferedImage img, double rotate) {
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Rectangle desRect = calcRotatedSize(new Rectangle(new Dimension(width, height)), rotate);

BufferedImage newImage = new BufferedImage(desRect.width, desRect.height, img.getType());
Graphics2D g = newImage.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.translate((desRect.width - width) / 2, (desRect.height - height) / 2);
g.rotate(Math.toRadians(rotate), width/2, height/2);
g.drawImage(img, null, null);
// g.dispose();
g.drawImage(img, null , null);
g.dispose();

return newImage;
}

private static Rectangle calcRotatedSize(Rectangle srcRect, double angle) {
if (angle < 0) {
angle = 360 + angle;
}
if (angle >= 90) {
if (angle / 90 % 2 == 1) {
int tmp = srcRect.height;
srcRect.height = srcRect.width;
srcRect.width = tmp;
}

angle = angle % 90;
}

double r = Math.sqrt(srcRect.width * srcRect.width + srcRect.height * srcRect.height) / 2;
double len = r * Math.sin(Math.toRadians(angle) / 2) * 2;
double angle1 = (Math.PI - Math.toRadians(angle)) / 2;
double angle2 = Math.atan((double)srcRect.height / srcRect.width);
double angle3 = Math.atan((double)srcRect.width / srcRect.height);

int addWidth = (int)(len * Math.cos(Math.PI - angle1 - angle2));
int addHeight = (int)(len * Math.cos(Math.PI - angle1 - angle3));

int desWidth = srcRect.width + addWidth * 2;
int desHeight = srcRect.height + addHeight * 2;

return new Rectangle(new Dimension(desWidth, desHeight));
}

/**
*
* 旋转图片方法二
*
* @param img 目标图片
*
* @param rotate 旋转的角度
*
* */
public static BufferedImage rotateImg2(BufferedImage img, double rotate) {
int iw = img.getWidth();// 原始图象的宽度
int ih = img.getHeight();// 原始图象的高度
int w = 0;
int h = 0;
int x = 0;
int y = 0;
rotate = rotate % 360;
if (rotate < 0)
rotate = 360 + rotate;// 将角度转换到0-360度之间
double ang = Math.toRadians(rotate);// 将角度转为弧度

/**
* 确定旋转后的图象的高度和宽度
*/

if (rotate == 180 || rotate == 0 || rotate == 360) {
w = iw;
h = ih;
} else if (rotate == 90 || rotate == 270) {
w = ih;
h = iw;
} else {
int d = iw + ih;
w = (int) (d * Math.abs(Math.cos(ang)));
h = (int) (d * Math.abs(Math.sin(ang)));
}

x = (w / 2) - (iw / 2);// 确定原点坐标
y = (h / 2) - (ih / 2);
BufferedImage rotatedImage = new BufferedImage(w, h, img.getType());

AffineTransform at = new AffineTransform();
at.rotate(ang, w / 2, h / 2);// 旋转图象
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at,
AffineTransformOp.TYPE_BICUBIC);
op.filter(img, rotatedImage);
img = rotatedImage;

return img;
}

/**
*
* 添加文字水印
Expand Down Expand Up @@ -91,7 +187,9 @@ public static void addTextMark(String imgPath, TextWatermark watermark, String d
Font rfont = ffont.deriveFont(affineTransform);
g.setFont(rfont);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, watermark.getAlpha()));
g.drawString(watermark.getText(), watermark.getX(), watermark.getY());

FontMetrics fm = FontDesignMetrics.getMetrics(rfont);
g.drawString(watermark.getText(), watermark.getX(), watermark.getY() - fm.getHeight() / 2);
g.dispose();

ImageIO.write(bfImage, watermark.getFormatName(), new File(destPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testAddTextMark() throws Exception {
.setText("https://blog.tommyyang.cn").setX(x).setY(y).build();

ImageWatermark imageWatermark = (ImageWatermark)Watermark.WatermarkBuilder.newBuilder().setType(Watermark.Type.IMAGE).setRotate(-45)
.setAlpha(0.6f).setImgPath(markPath).setX(x).setY(y).build();
.setAlpha(0.2f).setImgPath(markPath).setX(x).setY(y).build();

ImageTools.addTextMark(imgPath, textWatermark, destPath1);
ImageTools.addWaterMark(imgPath, imageWatermark, destPath2);
Expand Down

0 comments on commit 9d5d803

Please sign in to comment.