|
@@ -0,0 +1,92 @@
|
|
|
+package cn.fastfun.util;
|
|
|
+
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author bridge
|
|
|
+ * @date 2023/5/22 11:27
|
|
|
+ */
|
|
|
+public class FileUtils {
|
|
|
+
|
|
|
+ public static void outFile(HttpServletResponse response, String fileName, File file) {
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName);
|
|
|
+ //4.获取下载文件的输入流
|
|
|
+ try {
|
|
|
+ FileInputStream in = new FileInputStream(file);
|
|
|
+ //5.创建缓冲区
|
|
|
+ int len = 0;
|
|
|
+ byte[] buffer = new byte[2048];
|
|
|
+ //6.将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区中的数据输出到客户端。
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
+ while ((len = in.read(buffer)) > 0) {
|
|
|
+ out.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ //7.关闭流
|
|
|
+ in.close();
|
|
|
+ out.close();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void mergeImage(String filePath, String qr, String productTitle, String productCode, String num) {
|
|
|
+ try {
|
|
|
+ File template = new File("/home/label.png");
|
|
|
+ if (!template.exists()) {
|
|
|
+ template = ResourceUtils.getFile("classpath:label.png");
|
|
|
+ }
|
|
|
+ /* 1 读取第一张图片*/
|
|
|
+ BufferedImage imageFirst = ImageIO.read(template);
|
|
|
+ /* 2读取第二张图片 */
|
|
|
+ File fileTwo = new File(filePath + qr);
|
|
|
+ BufferedImage imageSecond = ImageIO.read(fileTwo);
|
|
|
+ //创建一个最底层画布 高和宽为第一章图片的高和宽
|
|
|
+ BufferedImage image = new BufferedImage(imageFirst.getWidth(), imageFirst.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
|
+ //通过底图创建画笔
|
|
|
+ Graphics graphics = image.getGraphics();
|
|
|
+ //在底图上画第一张图
|
|
|
+ graphics.drawImage(imageFirst, 0, 0, null);
|
|
|
+ //在底图上画第二张图
|
|
|
+ graphics.drawImage(imageSecond, 300, 5, null);
|
|
|
+ //在图片上写文字
|
|
|
+ graphics.setFont(new Font("simsun", Font.PLAIN, 22));
|
|
|
+ graphics.setColor(Color.BLACK);
|
|
|
+ MyDrawString(productTitle, 100, 36, 1, graphics);
|
|
|
+// graphics.drawString(productTitle, 100, 36);
|
|
|
+ MyDrawString(productCode, 100, 90, 0.88, graphics);
|
|
|
+ MyDrawString(num, 100, 146, 0.88, graphics);
|
|
|
+ //输出图片
|
|
|
+ fileTwo.delete();
|
|
|
+ File outFile = new File(filePath, qr);
|
|
|
+ ImageIO.write(image, "png", outFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void MyDrawString(String str, int x, int y, double rate, Graphics g) {
|
|
|
+ String tempStr = new String();
|
|
|
+ int orgStringWight = g.getFontMetrics().stringWidth(str);
|
|
|
+ int orgStringLength = str.length();
|
|
|
+ int tempx = x;
|
|
|
+ int tempy = y;
|
|
|
+ while (str.length() > 0) {
|
|
|
+ tempStr = str.substring(0, 1);
|
|
|
+ str = str.substring(1, str.length());
|
|
|
+ g.drawString(tempStr, tempx, tempy);
|
|
|
+ tempx = (int) (tempx + (double) orgStringWight / (double) orgStringLength * rate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|