init
This commit is contained in:
154
ruoyi-dk/src/main/java/com/ruoyi/dk/util/ImageUtil.java
Normal file
154
ruoyi-dk/src/main/java/com/ruoyi/dk/util/ImageUtil.java
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
package com.bashi.dk.util;
|
||||
|
||||
import com.sun.image.codec.jpeg.JPEGCodec;
|
||||
import com.sun.image.codec.jpeg.JPEGEncodeParam;
|
||||
import com.sun.image.codec.jpeg.JPEGImageEncoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import sun.font.FontDesignMetrics;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class ImageUtil {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "d:\\Users\\004796\\桌面\\123\\hd_bg.png";
|
||||
Map<String,String> content = new LinkedHashMap<>();
|
||||
content.put(" 转账批次号","542838541");
|
||||
content.put(" 转出单位","招商银行股份有限公司");
|
||||
content.put(" 转出账户","2361293892173872198");
|
||||
content.put("转出账号地区","深圳市福田区深南大道708");
|
||||
content.put(" 收款人姓名","马中华");
|
||||
content.put(" 收款账户","1273891273897123718");
|
||||
content.put(" 币种","人民币元");
|
||||
content.put(" 转出金额","100000.00");
|
||||
content.put(" 转出时间","2023-11-27 09:30:12");
|
||||
content.put(" 转账类型","签约金融企业--网贷放款预约到账");
|
||||
content.put(" 执行方式","点击选择执行方式");
|
||||
content.put(" 状态","点击选择状态");
|
||||
content.put(" 银行备注","点击选择银行备注");
|
||||
content.put(" 处理结果","点击选择处理结果");
|
||||
content.put(" 用户备注","点击选择用户备注");
|
||||
String putPath = "d:\\Users\\004796\\桌面\\123\\1233.jpg";
|
||||
createStringMark(filePath,content,putPath);
|
||||
}
|
||||
|
||||
|
||||
//给jpg添加文字
|
||||
public static boolean createStringMark(String filePath, Map<String,String> content, String outPath) {
|
||||
ImageIcon imgIcon = new ImageIcon(filePath);
|
||||
Image theImg = imgIcon.getImage();
|
||||
int width = theImg.getWidth(null) == -1 ? 200 : theImg.getWidth(null);
|
||||
int height = theImg.getHeight(null) == -1 ? 200 : theImg.getHeight(null);
|
||||
int fontSize = 16;
|
||||
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = bimage.createGraphics();
|
||||
Color mycolor = Color.black;
|
||||
g.setColor(mycolor);
|
||||
g.setBackground(Color.black);
|
||||
g.drawImage(theImg, 0, 0, null);
|
||||
g.setFont(new Font("宋体", Font.PLAIN, fontSize)); //字体、字型、字号
|
||||
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g.setRenderingHints(hints);
|
||||
Graphics2D g1 = bimage.createGraphics();
|
||||
g1.setColor(mycolor);
|
||||
g1.setBackground(Color.black);
|
||||
g1.drawImage(theImg, 0, 0, null);
|
||||
g1.setFont(new Font("黑体", Font.PLAIN, fontSize)); //字体、字型、字号
|
||||
g1.setRenderingHints(hints);
|
||||
int widthFlag = 500;
|
||||
int heightFlag = 335;
|
||||
for (Map.Entry<String, String> entry : content.entrySet()) {
|
||||
g1.drawString(entry.getKey()+": "+entry.getValue(), widthFlag, heightFlag); //画文字
|
||||
heightFlag += 29;
|
||||
}
|
||||
g1.dispose();
|
||||
g.dispose();
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
out = new FileOutputStream(outPath); //先用一个特定的输出文件名
|
||||
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
|
||||
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
|
||||
param.setQuality(100, true); //
|
||||
encoder.encode(bimage, param);
|
||||
} catch (Exception e) {
|
||||
log.error("图片生成失败", e);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if(out != null) out.close();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static void gogo(){
|
||||
int fontSize = 14;
|
||||
// 获取当前系统所有字体
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
String[] fontNames = ge.getAvailableFontFamilyNames();
|
||||
String str = "转账批次号: 1231231";
|
||||
int height = 0;
|
||||
int width = 0;
|
||||
// 根据字体获取需要生成的图片的宽和高
|
||||
for (String fontName : fontNames) {
|
||||
Font font = new Font(fontName, Font.PLAIN, fontSize);
|
||||
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
|
||||
height += metrics.getHeight();
|
||||
int tmpWidth = metrics.stringWidth(fontName + " : " + str);
|
||||
if (tmpWidth > width) {
|
||||
width = tmpWidth;
|
||||
}
|
||||
}
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
|
||||
Graphics2D g2d = null;
|
||||
try {
|
||||
//创建画笔
|
||||
g2d = image.createGraphics();
|
||||
//设置背景颜色为白色
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
|
||||
//设置画笔颜色为黑色
|
||||
g2d.setColor(Color.BLACK);
|
||||
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||
// 开启文字抗锯齿
|
||||
hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g2d.setRenderingHints(hints);
|
||||
int startY = 0;
|
||||
for (String fontName : fontNames) {
|
||||
Font font = new Font(fontName, Font.PLAIN, fontSize);
|
||||
System.out.println(fontName);
|
||||
g2d.setFont(font);
|
||||
g2d.drawString(fontName + " : " + str, 0, startY);
|
||||
// 下一行文字的左上角纵坐标
|
||||
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
|
||||
startY += metrics.getHeight();
|
||||
}
|
||||
String savePath = "d:\\Users\\004796\\桌面\\123\\1111.jpg";
|
||||
ImageIO.write(image, "PNG", new File(savePath));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (g2d != null) {
|
||||
g2d.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user