[测评系统]--测评系统核心代码库
linzhijie
2021-03-11 84fea994d2db7dc313ad1774f34eb12a45f8d6e7
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.ots.project.tool.exam;
import com.ots.common.utils.StringUtils;
import com.ots.framework.config.EssConfig;
import com.ots.framework.shiro.realm.UserRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
 
public class ImageUtil {
    
    public static int widthPic = 0;
    public static  int heigthPic = 0;
    public static String profile = EssConfig.getProfile();
    public static String  reportTemplates = EssConfig.getReportTemplates();
    private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
    public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color,
                                                Font font, Double degree, float alpha) {
        BufferedImage buffImg = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
        
        Graphics2D g2d = buffImg.createGraphics();
        
        buffImg = g2d.getDeviceConfiguration()
                .createCompatibleImage(width, heigth, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = buffImg.createGraphics();
        
        
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        
        if (null != degree) {
            
            
            g2d.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2,
                    (double) buffImg.getHeight() / 2);
        }
        
        g2d.setColor(color);
        
        g2d.setFont(font);
        
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        
        float realWidth = getRealFontWidth(text);
        float fontSize = font.getSize();
        
        
        float x = 0.5f * width - 0.5f * fontSize * realWidth;
        float y = 0.5f * heigth + 0.5f * fontSize;
        
        g2d.drawString(text, x, y);
        
        g2d.dispose();
        return buffImg;
    }
    public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color, float alpha) {
        
        Font font = new Font("Dialog", Font.ROMAN_BASELINE, 33);
        return waterMarkByText(width, heigth, text, color, font, -30d, alpha);
    }
    public static BufferedImage waterMarkByText(int width, int heigth, String text, float alpha) {
        return waterMarkByText(width, heigth, text, Color.GRAY, alpha);
    }
    public static BufferedImage waterMarkByText(int width, int heigth, String text) {
        return waterMarkByText(width, heigth, text, 0.2f);
    }
    public static BufferedImage waterMarkByText(String text) {
        return waterMarkByText(200, 150, text);
    }
    
    private static float getRealFontWidth(String text) {
        int len = text.length();
        float width = 0f;
        for (int i = 0; i < len; i++) {
            if (text.charAt(i) < 256) {
                width += 0.5f;
            } else {
                width += 1.0f;
            }
        }
        return width;
    }
    public static void creteWaterDropPic(String key, String waterDrop, float scorceF, String member_id, String suffixDate) {
        int CANVAS_W = 1000; 
        int CANVAS_H = 96; 
        int waterDropImageX = 6; 
        int waterDropImageY = 0;
        int scorce100 = 825; 
        int barImageX = 0;
        int barImageY = 30; 
        float scorce =  scorceF / 100;
        if (key.indexOf("ResponseFidelityDiagnosis") != -1) {
            CANVAS_H = 105;
            waterDropImageX = 2;
            scorce100 = 812;
            barImageY = 5;
        }
        if (key.indexOf("RuilinTestResultValidityDiagnosisContext") != -1) {
            CANVAS_H = 105;
            waterDropImageX = 2;
            scorce100 = 730;
            barImageY = 5;
        }
        if (key.indexOf("RuilinTestResultValidityDiagnosisContext") != -1) {
            CANVAS_H = 105;
            waterDropImageX = 2;
            scorce100 = 730;
            barImageY = 5;
        }
        if (Objects.equals(key,"Combined_waterDrops") || Objects.equals(key,"P_Abstract_context_waterDrops") || Objects.equals(key,"P_Numeric_context_waterDrops") ) {
            CANVAS_H = 105;
            waterDropImageX = 2;
            scorce100 = 811;
            barImageY = 5;
        }
        
 
 
        
        
        String barName = ""; 
        
        if (StringUtils.isNotEmpty(ExamUtil.getBarPictureMap(key))) {
            barName = ExamUtil.getBarPictureMap(key);
        } else {
            barName = "";
        }
        BufferedImage barImage = getImage(reportTemplates + barName);
        BufferedImage waterDropImage = getImage(reportTemplates + waterDrop);
        
        CANVAS_H = barImage.getHeight() + waterDropImage.getHeight();
        CANVAS_H = CANVAS_H - (CANVAS_H - barImageY - barImage.getHeight());
        CANVAS_W = barImage.getWidth();
 
 
        
        BufferedImage image = new BufferedImage(CANVAS_W, CANVAS_H, BufferedImage.TYPE_USHORT_565_RGB);
        
        Graphics2D g2d = image.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        image = g2d.getDeviceConfiguration().createCompatibleImage(CANVAS_W, CANVAS_H, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();
        
        
        setBarPicture( barImageX, barImageY, g2d, barImage);
        
        int waterDropOffsetX = setWaterDrop(waterDrop, waterDropImageX, waterDropImageY, scorce100, scorce, g2d, waterDropImage);
        
        setText(String.valueOf(Math.round(scorceF)), g2d, waterDropOffsetX);
        g2d.dispose();
        
        outputBarWaterImage(key + "_" + member_id + "_" + String.valueOf(scorceF)+"_" + suffixDate, image);
    }
    private static void outputBarWaterImage(String key, BufferedImage image) {
        try {
            File file = new File( profile + "/" + key + ".png");
            ImageIO.write(image, "png", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void setText(String value, Graphics2D g2d, int waterDropOffsetX) {
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        
        g2d.setColor(Color.BLACK);
        Font font = new Font("", Font.BOLD, 14);
        g2d.setFont(font);
        
        int numX = 0;
        int numY = 19;
        if (value.length() == 2) {
            numX = 12;
        } else if (value.length() == 3) {
            numX = 8;
        } else if (value.length() == 1) {
            numX = 18;
        }
        
        g2d.drawString(value, numX + waterDropOffsetX, numY);
    }
    private static int setWaterDrop(String waterDrop, int waterDropImageX, int waterDropImageY, int scorce100, float scorce, Graphics2D g2d, BufferedImage waterDropImage) {
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int waterDropOffsetX = (int) Math.round(scorce100 * scorce + waterDropImageX);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
        g2d.drawImage(waterDropImage, waterDropOffsetX, waterDropImageY, waterDropImage.getWidth(), waterDropImage.getHeight(), null);
        return waterDropOffsetX;
    }
    private static void setBarPicture(int barImageX, int barImageY, Graphics2D g2d, BufferedImage barImage) {
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
        g2d.drawImage(barImage, barImageX, barImageY, barImage.getWidth(), barImage.getHeight(), null);
    }
    public static BufferedImage getImage(String path) {
        try {
            File file = new File(path);
            
            BufferedImage image = ImageIO.read(file);
            return image;
        } catch (IOException e) {
            log.info("异常图片:" + path);
            throw new RuntimeException("获取图片资源异常", e);
        }
    }
    public static void testSpliStr(){
        String str = "{start.png}0{start.png}2{start.png}{start.png}{start.png}5{start.png}";
        int position = 0;
        int poiter = 0;
        int end = str.length();
        int starLength = "{start.png}".length();
        while (position < end) {
            position = str.indexOf("{start.png}", position);
            String value = "";
            if (position == -1) {
                value = str.substring(poiter, end);
                System.out.println("最后文本:" + value);
                break;
            }
            if (poiter == position) {
                position = position + starLength;
                System.out.println("图片:" + str.substring(poiter, position));
                poiter = position;
                continue;
            } else {
                System.out.println("文本:" + str.substring(poiter, position));
                poiter = position;
                position = poiter + starLength;
                System.out.println("图片:" + str.substring(poiter, position));
                poiter = position;
            }
        }
    }
    public static void main(String[] args) {
 
 
 
 
 
         ImageUtil.creteWaterDropPic("P_Combined_waterDrops", "waterDropOrange.png", 0,"222","2020");
         ImageUtil.creteWaterDropPic("P_Abstract_waterDrops", "waterDropOrange.png", 50,"222","2020");
         ImageUtil.creteWaterDropPic("P_Numeric_waterDrops", "waterDropOrange.png", 100,"222","2020");
        
        testSpliStr();
    }
}