jyy
2021-03-11 f435b7cb4d5a078c5387c91888c782e1936a5881
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.matrix.component.tools;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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);
    }
 
 
    public static void downloadPicture(String imgUrl,String savePath) {
        URL url = null;
        int imageNumber = 0;
        try {
 
            InputStream inputStream = null;
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(imgUrl).openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
            httpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
            httpURLConnection.setRequestProperty("Referer","no-referrer");
            httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            httpURLConnection.setConnectTimeout(15000);
            httpURLConnection.setReadTimeout(20000);
            inputStream = httpURLConnection.getInputStream();
 
 
 
            FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            inputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
 
 
}