111
k94314517
2025-02-28 32a43e602e4a78478781532d31fbc38755188df7
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
package com.doumee.core.utils;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
 
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
 
import javax.imageio.ImageIO;
 
public class ImageDesignerUtil {
 
    private static BufferedImage image;
 
    private static void createImage(String fileLocation,BufferedImage image) {
    try {
//        FileOutputStream fos = new FileOutputStream(fileLocation);
//        BufferedOutputStream bos = new BufferedOutputStream(fos);
//        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
//        encoder.encode(image);
//        bos.close();
        String formatName = fileLocation.substring(fileLocation.lastIndexOf(".") + 1);
        ImageIO.write(image, formatName , new File(fileLocation) );
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
    public static boolean graphicsGenerationIs(String bottom, InputStream imgurl,   String savePath) {
        try {
            int imageWidth = 320;// 图片的宽度
            int imageHeight = 370;// 图片的高度
            BufferedImage image = new BufferedImage(imageWidth, imageHeight,
                    BufferedImage.TYPE_INT_RGB);
            Graphics graphics = image.getGraphics();
            graphics.setColor(Color.black);
            graphics.fillRect(0, 0, imageWidth, imageHeight);
            // 改成这样:
            BufferedImage bimg = null;
            try {
                bimg = javax.imageio.ImageIO.read(imgurl);
            } catch (Exception e) {
            }
 
            if (bimg != null)
                graphics.drawImage(bimg, 15, 15, null);
            // ------------------------背景图画结束--------------
 
 
 
            graphics.setColor(Color.WHITE);
            graphics.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            graphics.drawString(bottom, 90, 345);
            // --------------------昵称画结束------------------
            graphics.dispose();
            createImage(savePath,image);
            return  new File(savePath).isFile();
 
        }catch (Exception e){
            return false;
        }
 
    }
    public static String graphicsGeneration(String name, String imgurl, String header,
        String qrCode,String savePath) {
 
    int imageWidth = 640;// 图片的宽度
    int imageHeight = 1020;// 图片的高度
    image = new BufferedImage(imageWidth, imageHeight,
        BufferedImage.TYPE_INT_RGB);
    Graphics graphics = image.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, imageWidth, imageHeight);
    // 改成这样:
    BufferedImage bimg = null;
    try {
        bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
    } catch (Exception e) {
    }
 
    if (bimg != null)
        graphics.drawImage(bimg, 0, 0, null);
    // ------------------------背景图画结束--------------
 
    // graphics.fillRect(0, 0, headWidth, headHeight);
    // 改成这样:
    bimg = null;
    try {
        bimg = javax.imageio.ImageIO.read(new java.io.File(header));
    } catch (Exception e) {
    }
 
    if (bimg != null)
        graphics.drawImage(bimg, 26, 34, 90, 90, null);
    // --------------头像画结束---------------
    graphics.setColor(Color.BLACK);
    graphics.setFont(new Font("微软雅黑", Font.PLAIN, 30));
    graphics.drawString(name, 134, 70);
    // --------------------昵称画结束------------------
 
    graphics.setColor(Color.WHITE);
    // graphics.fillRect(0, 0, codeWidth, codeHeight);
    // 改成这样:
    bimg = null;
    try {
        bimg = javax.imageio.ImageIO.read(new java.io.File(qrCode));
    } catch (Exception e) {
    }
 
    if (bimg != null)
        graphics.drawImage(bimg, 140, 547, 360, 360, null);
    // --------------二维码画结束---------------
    graphics.dispose();
    createImage(savePath,image);
 
    return savePath;
 
    }
 
    public static void main(String[] args) {
    ImageDesignerUtil cg = new ImageDesignerUtil();
    try {
        graphicsGeneration("我是新世界之神", "bg.png",
            "D:\\test\\head.png", "D:\\test\\code.png","D:\\test\\test0.png");
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}