1
zj
2025-04-17 ff2d1f5acdadc466d7e199028ef385ae8ca277e7
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package kernel.util;
 
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
 
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
 
import org.springframework.util.Assert;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.awt.Image;
 
public class ImageUtils {
    /**
     * 图片合成
     *
     * @param backgroundPath 底图
     * @param smallPath      小图
     * @param type           生成图片类型jpg,png...
     * @param resultPaht     生成图片保存路径
     */
    public static void image(String backgroundPath, String smallPath, String type, String resultPaht) {
        try {
            Assert.hasText(backgroundPath, "底图路径为空");
            Assert.hasText(smallPath, "小图路径为空");
            BufferedImage small = getBufferedImageFromUrl(smallPath);
            BufferedImage image = getBufferedImageFromUrl(backgroundPath);
            // 生成画笔
            Graphics g = image.getGraphics();
            g.drawImage(small, image.getWidth() - small.getWidth(), image.getHeight() - small.getHeight(),
                    image.getWidth(), image.getHeight(), null);
            ImageIO.write(image, type, new File(resultPaht));
        } catch (IOException e) {
            throw new RuntimeException("合成图片失败", e);
        }
    }
 
    public static void image_usercode(String backgroundPath, String smallPath, String type, String resultPaht) {
        try {
            Assert.hasText(backgroundPath, "底图路径为空");
            Assert.hasText(smallPath, "小图路径为空");
            BufferedImage small = getBufferedImageFromUrl(smallPath);
            BufferedImage image = getBufferedImageFromUrl(backgroundPath);
            // 生成画笔
            Graphics g = image.getGraphics();
            int x = (int) Arith.sub(Arith.div(image.getWidth(), 2), 92);
            int y = (int) Arith.sub(image.getHeight(), Arith.add(small.getHeight(), 92));
            g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
            ImageIO.write(image, type, new File(resultPaht));
        } catch (IOException e) {
            throw new RuntimeException("合成图片失败", e);
        }
    }
 
    /**
     * 根据图片url获取图片
     *
     * @param url
     * @return
     * @throws IOException
     */
    private static BufferedImage getBufferedImageFromUrl(String url) throws IOException {
        if (url.startsWith("https://") || url.startsWith("http://")) {
            return ImageIO.read(new URL(url));
        } else {
            return ImageIO.read(new File(url));
        }
    }
 
    /**
     * 合成图片并返回base64字符串
     *
     */
    public static String image_usercodeBase64(String backgroundPath, String smallPath, String type, String resultPaht) {
        try {
            Assert.hasText(backgroundPath, "底图路径为空");
            Assert.hasText(smallPath, "小图路径为空");
            BufferedImage small = getBufferedImageFromUrl(smallPath);
            BufferedImage image = getBufferedImageFromUrl(backgroundPath);
            // 生成画笔
            Graphics g = image.getGraphics();
            int x = (int) Arith.sub(Arith.div(image.getWidth(), 2), 92);
            int y = (int) Arith.sub(image.getHeight(), Arith.add(small.getHeight(), 40));
 
            g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
 
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, type, outputStream);
            String data = DatatypeConverter.printBase64Binary(outputStream.toByteArray());
 
            return data;
 
//            ImageIO.write(image, type, new File(resultPaht));
        } catch (IOException e) {
            throw new RuntimeException("合成图片失败", e);
        }
    }
    
    /**
     *   文件转 base64
     * @param file
     * @return
     */
    public static String file2Base64(File file) {
        if (file == null) {
            return null;
        }
        String base64 = null;
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
            byte[] buff = new byte[fin.available()];
            fin.read(buff);
            base64 = Base64.encode(buff);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }
    
    /**
     * 指定图片宽度和高度或压缩比例对图片进行压缩
     * @param file    文件
     * @param rate    压缩比例(为空或者1,原比例压缩)
     */
    public static String reduceImg(File file,Float rate) throws Exception {
        String res = "";
        File srcfile = file;
        int widthdist = 0, heightdist = 0;
        // 检查图片文件是否存在
//        Float rate = null;
//        rate = 1f;
//        System.out.println(file.length());
        // 如果比例不为空则说明是按比例压缩
        if (rate != null && rate > 0) {
            // 获得源图片的宽高存入数组中
            int[] results = getImgWidthHeight(srcfile);
            if (results == null || results[0] == 0 || results[1] == 0) {
                return "";
            } else {
                // 按比例缩放或扩大图片大小,将浮点型转为整型
                widthdist = (int) (results[0] * rate);
                heightdist = (int) (results[1] * rate);
            }
        }
        // 开始读取文件并进行压缩
        Image src = ImageIO.read(srcfile);
 
        // 构造一个类型为预定义图像类型之一的 BufferedImage
        BufferedImage tag = new BufferedImage((int) widthdist, (int) heightdist, BufferedImage.TYPE_INT_RGB);
 
        // 绘制图像 getScaledInstance表示创建此图像的缩放版本,返回一个新的缩放版本Image,按指定的width,height呈现图像
        // Image.SCALE_SMOOTH,选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
        tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(tag, "jpg", outputStream);
        String base64Img = Base64.encode(outputStream.toByteArray());
        res = "data:image/jpg;base64," + base64Img.toString();
        outputStream.close();
        // 创建文件输出流
        return res;
    }
    public static int[] getImgWidthHeight(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int result[] = { 0, 0 };
        try {
            // 获得文件输入流
            is = new FileInputStream(file);
            // 从流里将图片写入缓冲图片区
            src = ImageIO.read(is);
            result[0] = src.getWidth(null); // 得到源图片宽
            result[1] = src.getHeight(null);// 得到源图片高
            is.close(); // 关闭输入流
        } catch (Exception ef) {
            ef.printStackTrace();
        }
 
        return result;
    }
}