Browse Source

图片生成类

wangliang 3 năm trước cách đây
mục cha
commit
950d8edccc

+ 74 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/ImageTrajectoryUtil.java

@@ -0,0 +1,74 @@
+package com.qmth.teachcloud.common.util;
+
+import com.sun.image.codec.jpeg.JPEGCodec;
+import com.sun.image.codec.jpeg.JPEGImageEncoder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Objects;
+
+/**
+ * @Description: 图片轨迹坐标生成
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/11/2
+ */
+public class ImageTrajectoryUtil {
+    private final static Logger log = LoggerFactory.getLogger(ImageTrajectoryUtil.class);
+
+    /**
+     * 生成图片
+     *
+     * @param imgFile
+     * @param targetFile
+     * @param x
+     * @param y
+     * @throws IOException
+     */
+    public static void createImage(File imgFile, File targetFile, int x, int y) throws IOException {
+        FileOutputStream output = null;
+        try {
+            BufferedImage image = ImageIO.read(new FileInputStream(imgFile));
+            Graphics2D g = image.createGraphics();// 得到图形上下文
+            g.setColor(Color.red); // 设置画笔颜色
+            // 设置字体
+            g.setFont(new Font("微软雅黑", Font.LAYOUT_LEFT_TO_RIGHT, 20));// 写入签名
+            // 下面这一句中的43,image.getHeight()-10可以改成你要的坐标。
+            g.drawString("22.00", 23, image.getHeight() - 20);
+            g.drawString("×", 33, image.getHeight() - 40);
+            g.drawString("√", 43, image.getHeight() - 60);
+            g.dispose();
+
+            output = new FileOutputStream(targetFile);
+            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
+            encoder.encode(image);
+        } catch (Exception e) {
+            log.error("请求出错", e);
+            e.printStackTrace();
+        } finally {
+            if (Objects.nonNull(output)) {
+                output.flush();
+                output.close();
+            }
+        }
+    }
+
+    /**
+     * @param args
+     */
+    public static void main(String[] args) {
+        try {
+            ImageTrajectoryUtil.createImage(new File("/Users/king/Downloads/spring.jpg"), new File("/Users/king/Downloads/spring1.jpg"), 10, 10);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}