修改字体库位“思源字体”
This commit is contained in:
@@ -463,7 +463,7 @@ public class NameplateAdminService extends BaseService {
|
||||
|
||||
public Font loadWordFont() throws IOException {
|
||||
Font font = null;
|
||||
ClassPathResource resource = new ClassPathResource("fonts/HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
//InputStream inputStream2 = this.getClass().getClassLoader().getResourceAsStream("fonts/simsun.ttc");
|
||||
try {
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.accompany.common.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import sun.font.FontDesignMetrics;
|
||||
@@ -13,22 +14,19 @@ import java.io.*;
|
||||
/**
|
||||
* 自定义铭牌样式优化
|
||||
*/
|
||||
@Slf4j
|
||||
public class PicMoonAddTextMarkUtils {
|
||||
|
||||
//文字字体
|
||||
public static final Font font = new Font("宋体", Font.PLAIN, 16);
|
||||
public static final Color color = new Color(255, 244, 227, 255);
|
||||
//铭牌以两个汉字为基准的文字长度
|
||||
public static final int LEN_CHARACTERS_2 = 45;
|
||||
//铭牌以三个汉字为基准的文字长度
|
||||
public static final int LEN_CHARACTERS_3 = 67;
|
||||
//铭牌以四个汉字为基准的文字长度
|
||||
public static final int LEN_CHARACTERS_4 = 86;
|
||||
|
||||
//铭牌以汉字为基准对应的x坐标
|
||||
public static final int MP_TCHARACTER_LOCATION_X = 32;
|
||||
public static final int MP_T_CHARACTER_LOCATION_X = 32;
|
||||
//铭牌以汉字为基准对应的y坐标
|
||||
public static final int MP_TCHARACTER_LOCATION_Y = 93;
|
||||
public static final int MP_T_CHARACTER_LOCATION_Y = 93;
|
||||
|
||||
public static BufferedImage addTextToPic(InputStream inputStream, String text) throws IOException {
|
||||
BufferedImage bufImg = null;
|
||||
@@ -44,15 +42,14 @@ public class PicMoonAddTextMarkUtils {
|
||||
Font wordFontS = loadIconFont();
|
||||
g.setFont(wordFontS); //设置字体
|
||||
//消除文字锯齿
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
//消除画图锯齿字符串
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
//计算文字对应的坐标
|
||||
ImmutablePair<Integer, Integer> location = calculateLocaton(text, wordFontS);
|
||||
ImmutablePair<Integer, Integer> location = calculateLocation(text, wordFontS);
|
||||
g.drawString(text, location.getLeft(), location.getRight());
|
||||
g.dispose();
|
||||
}finally {
|
||||
} finally {
|
||||
if (inputStream != null) inputStream.close();
|
||||
}
|
||||
return bufImg;
|
||||
@@ -61,31 +58,16 @@ public class PicMoonAddTextMarkUtils {
|
||||
|
||||
/**
|
||||
* 计算徽章或铭牌坐标
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public static ImmutablePair<Integer, Integer> calculateLocaton(String text, Font fontS) {
|
||||
|
||||
//铭牌坐标计算 => 目前还没有两个字及三个字的铭牌图片数据,文字坐标还未进行确定
|
||||
// 因此此时默认都是4个字的铭牌图片
|
||||
/*
|
||||
int x = 0;
|
||||
if (text.length() == 2) {
|
||||
x = (LEN_CHARACTERS_2 - getTextWidth(fontS, text)) / 2;
|
||||
}
|
||||
if (text.length() == 3) {
|
||||
x = (LEN_CHARACTERS_3 - getTextWidth(fontS, text)) / 2;
|
||||
}
|
||||
if (text.length() == 4) {
|
||||
x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
}*/
|
||||
|
||||
int x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
return ImmutablePair.of(MP_TCHARACTER_LOCATION_X + x , MP_TCHARACTER_LOCATION_Y);
|
||||
public static ImmutablePair<Integer, Integer> calculateLocation(String text, Font fontS) {
|
||||
int x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
return ImmutablePair.of(MP_T_CHARACTER_LOCATION_X + x, MP_T_CHARACTER_LOCATION_Y);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static int getTextWidth(Font fontS, String content) {
|
||||
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(fontS);
|
||||
int width = 0;
|
||||
@@ -97,61 +79,47 @@ public class PicMoonAddTextMarkUtils {
|
||||
|
||||
private static Font loadIconFont() throws IOException {
|
||||
Font font = null;
|
||||
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("fonts/simsun.ttf");
|
||||
// InputStream inputStream = new FileInputStream("C:\\Users\\xy\\IdeaProjects\\yinyou-java\\accompany-business\\accompany-business-service\\src\\main\\resources\\fonts\\HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (inputStream != null) inputStream.close();
|
||||
} catch (Exception e) {
|
||||
inputStream.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
assert font != null;
|
||||
return font.deriveFont(16f);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
testAddTextToPic();
|
||||
/*String system = System.getProperty("os.name");
|
||||
if(system.toLowerCase().startsWith("win")){
|
||||
System.out.println("当前系统为:"+system);
|
||||
}else System.out.println("当前系统为linux");*/
|
||||
}
|
||||
|
||||
|
||||
public static void testAddTextToPic() throws Exception {
|
||||
// try(InputStream input = new FileInputStream("C:\\Users\\xy\\Desktop\\moon.png")) {
|
||||
try(InputStream input = FileUtils.downloadFileInputStream("http://img.uat.lecheng163.com/火腿月饼.png")) {
|
||||
try (InputStream input = FileUtils.downloadFileInputStream("http://img.uat.lecheng163.com/火腿月饼.png")) {
|
||||
InputStream uploadStream = null;
|
||||
FileOutputStream downloadFile = null;
|
||||
try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageOutputStream imOut = ImageIO.createImageOutputStream(baos)) {
|
||||
Font loadIconFont = loadIconFont();
|
||||
BufferedImage bufImg = PicMoonAddTextMarkUtils.addTextToPic(input, "豆沙豆沙");
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageOutputStream imOut = ImageIO.createImageOutputStream(baos)) {
|
||||
BufferedImage bufImg = PicMoonAddTextMarkUtils.addTextToPic(input, "號號號");
|
||||
if (bufImg == null) return;
|
||||
//ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);
|
||||
ImageIO.write(bufImg, "png", imOut);
|
||||
uploadStream = new ByteArrayInputStream(baos.toByteArray());
|
||||
|
||||
|
||||
int index;
|
||||
byte[] bytes = new byte[1024];
|
||||
downloadFile = new FileOutputStream("C:\\Users\\xy\\Desktop\\moon1.png");
|
||||
downloadFile = new FileOutputStream("/Users/liaozetao/Downloads/001.png");
|
||||
while ((index = uploadStream.read(bytes)) != -1) {
|
||||
downloadFile.write(bytes, 0, index);
|
||||
downloadFile.flush();
|
||||
}
|
||||
}finally {
|
||||
} finally {
|
||||
if (downloadFile != null) downloadFile.close();
|
||||
if (uploadStream != null) uploadStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println("结束运行.....");
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.accompany.common.utils;
|
||||
|
||||
import com.accompany.common.constant.Constant;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import sun.font.FontDesignMetrics;
|
||||
@@ -14,6 +15,7 @@ import java.io.*;
|
||||
/**
|
||||
* 粉丝团铭牌生成
|
||||
*/
|
||||
@Slf4j
|
||||
public class PicAddTextAnchorFansMpMarkUtils {
|
||||
|
||||
//文字字体
|
||||
@@ -24,14 +26,9 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
public static final int LEN_CHARACTERS_4 = 86;
|
||||
|
||||
//铭牌以汉字为基准对应的x坐标
|
||||
// public static final int MP_TCHARACTER_LOCATION_X = 50;
|
||||
// //铭牌以汉字为基准对应的y坐标
|
||||
// public static final int MP_TCHARACTER_LOCATION_Y = 28;
|
||||
|
||||
//铭牌以汉字为基准对应的x坐标
|
||||
public static final int MP_TCHARACTER_LOCATION_X = 45;
|
||||
public static final int MP_T_CHARACTER_LOCATION_X = 45;
|
||||
//铭牌以汉字为基准对应的y坐标
|
||||
public static final int MP_TCHARACTER_LOCATION_Y = 28;
|
||||
public static final int MP_T_CHARACTER_LOCATION_Y = 28;
|
||||
|
||||
//4字等级坐标
|
||||
public static final int LEVEL_LOCATION_X_4 = 15;
|
||||
@@ -41,20 +38,18 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
//等级基准长度9
|
||||
public static final int LEN_LEVEL = 14;
|
||||
|
||||
//public static final int LINUX_LEFT_DIFF_LEN = 15;
|
||||
|
||||
public static String OS_SYSTEM = System.getProperty("os.name");
|
||||
|
||||
/**
|
||||
* @param srcImgPath 源图片路径
|
||||
* @param tarImgPath 保存的图片路径
|
||||
* @param srcImgPath 源图片路径
|
||||
* @param tarImgPath 保存的图片路径
|
||||
* @param waterMarkContent 水印内容
|
||||
* @param markContentColor 水印颜色
|
||||
* @param font 水印字体
|
||||
* @param font 水印字体
|
||||
*/
|
||||
public void localTest(Byte mpStatus, String srcImgPath, String tarImgPath, String waterMarkContent,Color markContentColor,Font font, Byte type, int level, ClassPathResource classPathResource) throws IOException {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = classPathResource.getInputStream();
|
||||
public void localTest(Byte mpStatus, String srcImgPath, String tarImgPath, String waterMarkContent, Color markContentColor, Font font, Byte type, int level, ClassPathResource classPathResource) throws IOException {
|
||||
try (InputStream inputStream = classPathResource.getInputStream()) {
|
||||
// 读取原图片信息
|
||||
File srcImgFile = new File(srcImgPath);//得到文件
|
||||
Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
|
||||
@@ -65,25 +60,17 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
Graphics2D g = bufImg.createGraphics();
|
||||
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
|
||||
g.setColor(markContentColor); //根据图片的背景设置水印颜色
|
||||
|
||||
g.setFont(Font.createFont(Font.TRUETYPE_FONT, inputStream)); //设置字体
|
||||
//消除文字锯齿
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
//消除画图锯齿字符串
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
|
||||
//计算文字对应的坐标
|
||||
ImmutablePair<Integer, Integer> location = calculateLocaton( waterMarkContent, font);
|
||||
ImmutablePair<Integer, Integer> location = calculateLocation(waterMarkContent, font);
|
||||
//计算等级对应的坐标
|
||||
ImmutablePair<Integer, Integer> levelLocation = calculateLevelLocaton(level, font, mpStatus);
|
||||
if (location == null || levelLocation == null) return;
|
||||
ImmutablePair<Integer, Integer> levelLocation = calculateLevelLocation(level, font, mpStatus);
|
||||
g.drawString(waterMarkContent, location.getLeft(), location.getRight()); //画出水印
|
||||
|
||||
// if (Constant.GuardsBadgeType.namplate_type.equals(type)) {
|
||||
g.drawString(String.valueOf(level), levelLocation.getLeft(), levelLocation.getRight());
|
||||
// }
|
||||
|
||||
g.dispose();
|
||||
// 输出图片
|
||||
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
|
||||
@@ -91,11 +78,7 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
System.out.println("添加水印完成");
|
||||
outImgStream.flush();
|
||||
outImgStream.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}finally {
|
||||
if (inputStream != null) inputStream.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,60 +96,55 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
g.setColor(color); //根据图片的背景设置水印颜色
|
||||
g.setFont(wordFontS); //设置字体
|
||||
//消除文字锯齿
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
//消除画图锯齿字符串
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
//计算文字对应的坐标
|
||||
ImmutablePair<Integer, Integer> location = calculateLocaton( text, wordFontS);
|
||||
ImmutablePair<Integer, Integer> location = calculateLocation(text, wordFontS);
|
||||
//计算等级对应的坐标
|
||||
ImmutablePair<Integer, Integer> levelLocation = calculateLevelLocaton(level, levelFontS,mpStatus);
|
||||
if (location == null || levelLocation == null) return null;
|
||||
ImmutablePair<Integer, Integer> levelLocation = calculateLevelLocation(level, levelFontS, mpStatus);
|
||||
g.drawString(text, location.getLeft(), location.getRight());
|
||||
g.setFont(levelFontS);
|
||||
g.drawString(String.valueOf(level), levelLocation.getLeft(), levelLocation.getRight());
|
||||
|
||||
g.dispose();
|
||||
}finally {
|
||||
} finally {
|
||||
if (inputStream != null) inputStream.close();
|
||||
}
|
||||
|
||||
return bufImg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算徽章或铭牌文字坐标
|
||||
* @param type
|
||||
*
|
||||
* @param text
|
||||
* @param fontS
|
||||
* @return
|
||||
*/
|
||||
public static ImmutablePair<Integer, Integer> calculateLocaton( String text, Font fontS) {
|
||||
public static ImmutablePair<Integer, Integer> calculateLocation(String text, Font fontS) {
|
||||
//铭牌坐标计算
|
||||
int x = 0;
|
||||
x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
return ImmutablePair.of(MP_TCHARACTER_LOCATION_X + x , MP_TCHARACTER_LOCATION_Y);
|
||||
int x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
return ImmutablePair.of(MP_T_CHARACTER_LOCATION_X + x, MP_T_CHARACTER_LOCATION_Y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算等级坐标
|
||||
*
|
||||
* @param levelSeq
|
||||
* @param mpStatus
|
||||
* @return
|
||||
*/
|
||||
public static ImmutablePair<Integer, Integer> calculateLevelLocaton( Integer levelSeq, Font fonts, Byte mpStatus) {
|
||||
public static ImmutablePair<Integer, Integer> calculateLevelLocation(Integer levelSeq, Font fonts, Byte mpStatus) {
|
||||
int len = getTextWidth(fonts, String.valueOf(levelSeq));
|
||||
|
||||
int x = LEVEL_LOCATION_X_4;
|
||||
if (len < LEN_LEVEL) {
|
||||
x = x + (LEN_LEVEL - len) / 2;
|
||||
}
|
||||
if (Constant.AnchorFansMemberMpStatus.active.equals(mpStatus)) {
|
||||
return ImmutablePair.of(x,LEVEL_LOCATION_Y_ACTIVE);
|
||||
}else {
|
||||
return ImmutablePair.of(x, LEVEL_LOCATION_Y_ACTIVE);
|
||||
} else {
|
||||
return ImmutablePair.of(x, LEVEL_LOCATION_Y_NORMAL_NEGATIVE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int getTextWidth(Font fontS, String content) {
|
||||
@@ -178,66 +156,55 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
return width;
|
||||
}
|
||||
|
||||
private static Font loadIconFont() throws FileNotFoundException {
|
||||
private static Font loadIconFont() throws IOException {
|
||||
Font font = null;
|
||||
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("fonts/simsun.ttf");
|
||||
// InputStream inputStream = new FileInputStream("C:\\Users\\Q\\Desktop\\字体\\aa\\msyh.ttc");
|
||||
InputStream inputStream = new FileInputStream("C:\\Users\\demo\\Desktop\\2022-03\\mp\\HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (inputStream != null) inputStream.close();
|
||||
} catch (Exception e) {
|
||||
inputStream.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
assert font != null;
|
||||
return font.deriveFont(22f);
|
||||
}
|
||||
|
||||
private static Font loadLevelFont() throws FileNotFoundException {
|
||||
private static Font loadLevelFont() throws IOException {
|
||||
Font font = null;
|
||||
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("fonts/simsun.ttf");
|
||||
InputStream inputStream = new FileInputStream("C:\\Users\\demo\\Desktop\\2022-03\\mp\\Gats.otf");
|
||||
//InputStream inputStream = new FileInputStream("C:\\Users\\demo\\Desktop\\2022-03\\youshebiaotihei.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (inputStream != null) inputStream.close();
|
||||
} catch (Exception e) {
|
||||
inputStream.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
assert font != null;
|
||||
return font.deriveFont(24f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
testAddTextToPic();
|
||||
/*String system = System.getProperty("os.name");
|
||||
if(system.toLowerCase().startsWith("win")){
|
||||
System.out.println("当前系统为:"+system);
|
||||
}else System.out.println("当前系统为linux");*/
|
||||
}
|
||||
|
||||
|
||||
public static void testAddTextToPic() throws Exception {
|
||||
//try(InputStream input = FileUtils.downloadFileInputStream("http://image.uat.zhongjialx.com/FipEF17Seo5ET-54GFjRjqlrs-bd?imageslim")) {
|
||||
try(InputStream input = FileUtils.downloadFileInputStream("http://img.uat.lecheng163.com/FsffrTt6ittnFttLVPVxsQnF4VCB?imageslim")) {
|
||||
try (InputStream input = FileUtils.downloadFileInputStream("http://img.uat.lecheng163.com/FsffrTt6ittnFttLVPVxsQnF4VCB?imageslim")) {
|
||||
InputStream uploadStream = null;
|
||||
FileOutputStream downloadFile = null;
|
||||
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);) {
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);) {
|
||||
Font levelFont = loadLevelFont();
|
||||
Font loadIconFont = loadIconFont();
|
||||
BufferedImage bufImg = PicAddTextAnchorFansMpMarkUtils.addTextToPic( (byte)2, input, "粉丝高级", 8, loadIconFont,levelFont);
|
||||
BufferedImage bufImg = PicAddTextAnchorFansMpMarkUtils.addTextToPic((byte) 2, input, "粉丝高级", 8, loadIconFont, levelFont);
|
||||
if (bufImg == null) return;
|
||||
//ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);
|
||||
ImageIO.write(bufImg, "png", imOut);
|
||||
uploadStream = new ByteArrayInputStream(baos.toByteArray());
|
||||
|
||||
|
||||
int index;
|
||||
byte[] bytes = new byte[1024];
|
||||
downloadFile = new FileOutputStream("C:\\Users\\demo\\Desktop\\2022-03\\mp\\mppptest.png");
|
||||
@@ -245,13 +212,11 @@ public class PicAddTextAnchorFansMpMarkUtils {
|
||||
downloadFile.write(bytes, 0, index);
|
||||
downloadFile.flush();
|
||||
}
|
||||
}finally {
|
||||
} finally {
|
||||
if (downloadFile != null) downloadFile.close();
|
||||
if (uploadStream != null) uploadStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println("结束运行.....");
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package com.accompany.common.utils;
|
||||
|
||||
import com.accompany.common.constant.Constant;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import sun.font.FontDesignMetrics;
|
||||
@@ -14,6 +14,7 @@ import java.io.*;
|
||||
/**
|
||||
* 自定义铭牌样式优化
|
||||
*/
|
||||
@Slf4j
|
||||
public class PicAddTextMarkUtils {
|
||||
|
||||
//文字字体
|
||||
@@ -27,9 +28,9 @@ public class PicAddTextMarkUtils {
|
||||
public static final int LEN_CHARACTERS_4 = 88;
|
||||
|
||||
//铭牌以汉字为基准对应的x坐标
|
||||
public static final int MP_TCHARACTER_LOCATION_X = 46;
|
||||
public static final int MP_T_CHARACTER_LOCATION_X = 46;
|
||||
//铭牌以汉字为基准对应的y坐标
|
||||
public static final int MP_TCHARACTER_LOCATION_Y = 28;
|
||||
public static final int MP_T_CHARACTER_LOCATION_Y = 28;
|
||||
|
||||
public static BufferedImage addTextToPic(InputStream inputStream, String text, Font wordFontS) throws IOException {
|
||||
BufferedImage bufImg = null;
|
||||
@@ -44,49 +45,32 @@ public class PicAddTextMarkUtils {
|
||||
g.setColor(color); //根据图片的背景设置水印颜色
|
||||
g.setFont(wordFontS); //设置字体
|
||||
//消除文字锯齿
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
//消除画图锯齿字符串
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
//计算文字对应的坐标
|
||||
ImmutablePair<Integer, Integer> location = calculateLocaton(text, wordFontS);
|
||||
ImmutablePair<Integer, Integer> location = calculateLocation(text, wordFontS);
|
||||
g.drawString(text, location.getLeft(), location.getRight());
|
||||
g.dispose();
|
||||
}finally {
|
||||
} finally {
|
||||
if (inputStream != null) inputStream.close();
|
||||
}
|
||||
|
||||
return bufImg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算徽章或铭牌坐标
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public static ImmutablePair<Integer, Integer> calculateLocaton(String text, Font fontS) {
|
||||
|
||||
//铭牌坐标计算 => 目前还没有两个字及三个字的铭牌图片数据,文字坐标还未进行确定
|
||||
// 因此此时默认都是4个字的铭牌图片
|
||||
/*
|
||||
int x = 0;
|
||||
if (text.length() == 2) {
|
||||
x = (LEN_CHARACTERS_2 - getTextWidth(fontS, text)) / 2;
|
||||
}
|
||||
if (text.length() == 3) {
|
||||
x = (LEN_CHARACTERS_3 - getTextWidth(fontS, text)) / 2;
|
||||
}
|
||||
if (text.length() == 4) {
|
||||
x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
}*/
|
||||
|
||||
public static ImmutablePair<Integer, Integer> calculateLocation(String text, Font fontS) {
|
||||
int x = (LEN_CHARACTERS_4 - getTextWidth(fontS, text)) / 2;
|
||||
return ImmutablePair.of(MP_TCHARACTER_LOCATION_X + x , MP_TCHARACTER_LOCATION_Y);
|
||||
return ImmutablePair.of(MP_T_CHARACTER_LOCATION_X + x, MP_T_CHARACTER_LOCATION_Y);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static int getTextWidth(Font fontS, String content) {
|
||||
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(fontS);
|
||||
int width = 0;
|
||||
@@ -96,47 +80,37 @@ public class PicAddTextMarkUtils {
|
||||
return width;
|
||||
}
|
||||
|
||||
private static Font loadIconFont() throws FileNotFoundException {
|
||||
private static Font loadIconFont() throws IOException {
|
||||
Font font = null;
|
||||
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("fonts/simsun.ttf");
|
||||
// InputStream inputStream = new FileInputStream("C:\\Users\\Q\\Desktop\\字体\\aa\\msyh.ttc");
|
||||
InputStream inputStream = new FileInputStream("C:\\Users\\demo\\Desktop\\2022-03\\mp\\HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (inputStream != null) inputStream.close();
|
||||
} catch (Exception e) {
|
||||
inputStream.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
assert font != null;
|
||||
return font.deriveFont(22f);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
testAddTextToPic();
|
||||
/*String system = System.getProperty("os.name");
|
||||
if(system.toLowerCase().startsWith("win")){
|
||||
System.out.println("当前系统为:"+system);
|
||||
}else System.out.println("当前系统为linux");*/
|
||||
}
|
||||
|
||||
|
||||
public static void testAddTextToPic() throws Exception {
|
||||
//try(InputStream input = FileUtils.downloadFileInputStream("http://image.uat.zhongjialx.com/FipEF17Seo5ET-54GFjRjqlrs-bd?imageslim")) {
|
||||
try(InputStream input = FileUtils.downloadFileInputStream("http://img.uat.lecheng163.com/mppptest.png")) {
|
||||
try (InputStream input = FileUtils.downloadFileInputStream("http://img.uat.lecheng163.com/mppptest.png")) {
|
||||
InputStream uploadStream = null;
|
||||
FileOutputStream downloadFile = null;
|
||||
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);) {
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);) {
|
||||
Font loadIconFont = loadIconFont();
|
||||
BufferedImage bufImg = PicAddTextMarkUtils.addTextToPic(input, "王者荣耀", loadIconFont);
|
||||
BufferedImage bufImg = PicAddTextMarkUtils.addTextToPic(input, "王者荣耀", loadIconFont);
|
||||
if (bufImg == null) return;
|
||||
//ImageOutputStream imOut = ImageIO.createImageOutputStream(baos);
|
||||
ImageIO.write(bufImg, "png", imOut);
|
||||
uploadStream = new ByteArrayInputStream(baos.toByteArray());
|
||||
|
||||
|
||||
int index;
|
||||
byte[] bytes = new byte[1024];
|
||||
downloadFile = new FileOutputStream("C:\\Users\\demo\\Desktop\\2022-03\\mp\\mp.png");
|
||||
@@ -144,13 +118,11 @@ public class PicAddTextMarkUtils {
|
||||
downloadFile.write(bytes, 0, index);
|
||||
downloadFile.flush();
|
||||
}
|
||||
}finally {
|
||||
} finally {
|
||||
if (downloadFile != null) downloadFile.close();
|
||||
if (uploadStream != null) uploadStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println("结束运行.....");
|
||||
}
|
||||
}
|
||||
|
@@ -398,7 +398,7 @@ public class AnchorFansTeamServiceImpl extends ServiceImpl<AnchorFansTeamMapper,
|
||||
@Override
|
||||
public Font loadWordFont() throws IOException {
|
||||
Font font = null;
|
||||
ClassPathResource resource = new ClassPathResource("fonts/HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
//InputStream inputStream2 = this.getClass().getClassLoader().getResourceAsStream("fonts/simsun.ttc");
|
||||
//InputStream inputStream = new FileInputStream("C:\\Users\\Q\\Desktop\\字体\\宋体\\simsun.ttc");
|
||||
|
@@ -590,7 +590,7 @@ public class UserNameplateService extends BaseService {
|
||||
|
||||
public Font loadWordFont() throws IOException {
|
||||
Font font = null;
|
||||
ClassPathResource resource = new ClassPathResource("fonts/HYWenHei-85W.ttf");
|
||||
ClassPathResource resource = new ClassPathResource("fonts/SourceHanSansCN-Regular.otf");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user