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;
|
}
|
|
/**
|
* 创建水滴图片
|
* @param key
|
* @param waterDrop
|
* @param scorceF
|
* @param member_id
|
* @param suffixDate
|
*/
|
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("ResponseFidelityDiagnosis_V2") != -1) {
|
CANVAS_H = 400;
|
waterDropImageX = -30;
|
scorce100 = 800;
|
barImageY = 5;
|
waterDropImageY = 0;
|
}
|
if (key.indexOf("ResponseFidelityDiagnosisSAQGs") != -1) {
|
CANVAS_H = 400;
|
waterDropImageX = -30;
|
scorce100 = 800;
|
barImageY = 5;
|
waterDropImageY = 0;
|
}
|
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 = "";
|
}
|
if(StringUtils.isBlank(barName)){
|
return;
|
}
|
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);
|
|
if (key.indexOf("ResponseFidelityDiagnosis_V2") != -1 || key.indexOf("ResponseFidelityDiagnosisSAQGs") != -1) {
|
setTextV2(String.valueOf(Math.round(scorceF)), g2d, waterDropOffsetX);
|
}else{
|
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 setTextV2(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, 16);
|
g2d.setFont(font);
|
|
int numX = 0;
|
int numY = 17;
|
if (value.length() == 2) {
|
numX = 36;
|
} else if (value.length() == 3) {
|
numX = 32;
|
} else if (value.length() == 1) {
|
numX = 42;
|
}
|
|
g2d.drawString(value, numX + waterDropOffsetX, numY);
|
}
|
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);
|
log.error("{}发生异常:{}",path, e);
|
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();
|
}
|
}
|