wzy
2020-12-26 2cf928a22b6cf8ea7a29f29127ffc333c73b0bca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.matrix.component.tools;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class ImageUtil {
 
    public static void main(String[] args) throws Exception {
 
        // drawString();
         fonts();
        // System.out.println(4 % 2);
 
    }
 
    public static void drawString(BufferedImage bimg, ImageMerge imgMerger) throws IOException, FileNotFoundException {
        
        // 得到Graphics2D 对象
        Graphics2D g2d = (Graphics2D) bimg.getGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置颜色和画笔粗细57bec0
        g2d.setColor(toColorFromString(imgMerger.getColor()));
        g2d.setFont(new Font(imgMerger.getFontFamily(), Font.ITALIC, imgMerger.getFontSize()));
        // 绘制图案或文字
        // g2d.drawString("红星国际会展中心", 445, 630);
        drawStringWithSpan(g2d, imgMerger.getText(), imgMerger.getX(), imgMerger.getY(), imgMerger.getWordSpacing());
        
    }
 
    /**
     * 绘制字符间距
     */
    private static void drawStringWithSpan(Graphics2D g2d, String text, int i, int y, int k) {
        List<String> txtList = new ArrayList<>();
        // 匹配中英文
        String s = "\\d+.\\d+|\\w+|[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(s);
        Matcher ma = pattern.matcher(text);
 
        while (ma.find()) {
            txtList.add(ma.group());
        }
        int x = i;
        for (int m = 0; m < txtList.size(); m++) {
            String target = txtList.get(m);
            g2d.drawString(target, x, y);
            // 没写一个字就增加一个间距,2个哦英文单词占用1个中文字符空间
            int b = (target.length() / 2) + (target.length() % 2);
            x += b * (g2d.getFont().getSize() + k);
        }
 
    }
 
    /**
     * 小图片贴到大图片形成一张图(合成)
     * @author:liuyc
     * @throws IOException
     * @time:2016年5月27日 下午5:51:20
    
    public static final void overlapImage(ImageMerge imgMerge) throws IOException {
        BufferedImage big = ImageIO.read(new File(imgMerge.getSrcPath()));
        BufferedImage small = ImageIO.read(new File(imgMerge.getSmallPath()));
        Graphics2D g = big.createGraphics();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, imgMerge.getAlpha()));
        g.drawImage(small, imgMerge.getX(), imgMerge.getY(), imgMerge.getWidth(), imgMerge.getHeight(), null);
        g.dispose();
        ImageIO.write(big, imgMerge.getOutFilePath().split("\\.")[1], new File(imgMerge.getOutFilePath()));
    }
 */
    public static Color toColorFromString(String colorStr) {
        Color color = new Color(Integer.parseInt(colorStr, 16));
        return color;
    }
 
    public static void fonts() {
        String[] fonts = GraphicsEnvironment // GraphicsEnvironment(抽象类) 图形环境类
                .getLocalGraphicsEnvironment() // 获取本地图形环境
                .getAvailableFontFamilyNames(); // 获取可用字体family名
 
        int fontCount = 0; // 字体数统计
        for (String font : fonts) {
            fontCount++;
            System.out.println(font);
        }
        System.out.println("系统字体数:" + fontCount);
    }
 
}