三个对外方法实现功能
// 设置图片样式 长度 高度 背景色 设置背景,为null是空白背景
public void setImage(int width, int height, Color color,String imagepath)
// 设置字体样式 字体大小 字体间距 行间距
public void setFont(int size, int leftsize, int lineheight)
文本格式demo:
“16.某几何体的三视图如图所示,则组成该几何体的小正方体的个数是( ).\n” +"[image18.jpeg]"
// 输入一张图片的文本,“文本+[图片名]+文本\n”格式,根据\n 或字体一行的长度换行
输入生成图片地址的对象
输入文本中拼接图片的图片存储地址,对应文本中的图片名
public void txttoimg(String string, File imageFile, String imagePath)
package edu.math.HandwritingCore.TestExample;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.regex.Pattern;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import sun.plugin.dom.core.CoreConstants;
import javax.imageio.ImageIO;
/**
* @Author:小党
* @Date:Created in 15:10 2022/2/14
* @Explain:
*/
public class TxtToImage {
/**
* 图片宽度
*/
private int IMAGE_WIDTH = 1000;
/**
* 图片高度
*/
private int IMAGE_HEIGHT = 600;
// 图片背景
private Color IMAGE_COLOR = Color.WHITE;//= Color.CYAN; //蓝色
/**
* 图片类型
*/
private final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
// 字体大小
private int SIZE = 20;
// 左间距
private int LEFTSIZE = 10;
// 行间距
private int Y_LINEHEIGHT = 10;
private BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_TYPE);
// BufferedImage image = ImageIO.read(new File("D:\\b\\背景图片\\4.jpg"));
public TxtToImage() throws IOException {
}
// 设置图片样式 长度 高度 背景色
public void setImage(int width, int height, Color color,String imagepath) throws IOException {
this.IMAGE_WIDTH = width;
this.IMAGE_HEIGHT = height;
this.IMAGE_COLOR = color;
if (image != null){
BufferedImage read = ImageIO.read(new File(imagepath));
this.image = ImageUtils.resizeImage(read,IMAGE_WIDTH,IMAGE_HEIGHT);
}
}
// 设置字体样式 字体大小 字体间距 行间距
public void setFont(int size, int leftsize, int lineheight) {
this.SIZE = size;
this.LEFTSIZE = leftsize;
this.Y_LINEHEIGHT = lineheight;
}
/**
* @Author: dang on 2022/2/18 15:45
* @Param string 输入一张图片的文本,“文本+[图片名]+文本\n”格式,根据\n 或字体一行的长度换行
* @Param imageFile 输入生成图片地址的对象
* @Param imagePath 输入文本中拼接图片的图片存储地址,对应文本中的图片名
* @return:void
* @description:
*/
public void txttoimg(String string, File imageFile, String imagePath) throws IOException {
//获取图像上下文 Graphics的接口说明 https://www.oschina.net/uploads/doc/javase-6-doc-api-zh_CN/java/awt/Graphics.html
Graphics g = createGraphics(image);
System.out.println(g.getFont());
System.out.println(g.getFontMetrics());
//图片中文本高低+行高
final int lineheight = SIZE + Y_LINEHEIGHT;
int lineNum = 1;
try {
assert string.contains("\n");
String[] split = string.split("\n");
double v = SIZE + (SIZE * 0.2);
// 计算一行的字符串的长度
double v1 = IMAGE_WIDTH / v;
int[] ints = new int[]{LEFTSIZE, lineheight, LEFTSIZE, lineheight};
for (String line : split) {
List<Integer> list = new ArrayList<>();
Map<Integer, String> map = new HashMap<>();
while (line.contains("[")) {
int i = line.indexOf("[");
int i1 = line.indexOf("]");
String substring = line.substring(i + 1, i1);
if (substring.contains(".")) {
if (i == 0) {
line = line.substring(0, i) + " " + line.substring(i1 + 1);
list.add(i);
map.put(i, substring);
} else {
line = line.substring(0, i) + line.substring(i1 + 1);
list.add(i - 1);
map.put(i - 1, substring);
}
}
}
int start = 0;
char[] chars = line.toCharArray();
if (list.size() == 0) {
for (int j = 0; j < chars.length; j++) {
// g.drawLine(ints[0], ints[1], ints[2], ints[3]-SIZE);
int car = (int) chars[j];
// System.out.print(chars[j] + "---");
// System.out.println(car);
getInts(v, ints, car);
if (ints[0] > IMAGE_WIDTH - v * 1) {
g.drawString(line.substring(start, j), LEFTSIZE, ints[1]);
System.out.println(line.substring(start, j));
System.out.println(Arrays.toString(ints));
start = j;
ints = new int[]{LEFTSIZE, ints[1] + lineheight, LEFTSIZE, ints[3] + lineheight};
j--;
} else if (j == chars.length - 1) {
// 添加文本 左边距 每行的高度
g.drawString(line.substring(start, j + 1), LEFTSIZE, ints[1]);
ints = new int[]{LEFTSIZE, ints[1] + lineheight, LEFTSIZE, ints[3] + lineheight};
break;
}
}
} else {
int num = ints[0];
for (int j = 0; j < chars.length; j++) {
if (list.contains(j)) {
// g.drawString(line.substring(start, j + 1), ints[0], ints[1]);
// System.out.println(line.substring(start, j));
for (int i = start; i < j + 1; i++) {
// g.drawLine(ints[0], ints[1], ints[2], ints[3]-SIZE);
int car = (int) chars[i];
// System.out.print(chars[i] + "---");
// System.out.println(car);
getInts(v, ints, car);
if (ints[0] > IMAGE_WIDTH - v * 1) {
g.drawString(line.substring(start, i), num, ints[1]);
// System.out.println(line.substring(start, i));
// System.out.println(Arrays.toString(ints));
start = i;
ints = new int[]{LEFTSIZE, ints[1] + lineheight, LEFTSIZE, ints[3] + lineheight};
num = ints[0];
i--;
} else if (i == j) {
// 添加文本 左边距 每行的高度
g.drawString(line.substring(start, i + 1), num, ints[1]);
System.out.println(line.substring(start, i + 1));
break;
}
}
start = j + 1;
String s = map.get(j);
if (s.contains("wmf")){
s = s.split("\\.")[0]+".png";
}
Image image1 = ImageIO.read(new File(imagePath + "\\" + s));
int width = image1.getWidth(null);
int height = image1.getHeight(null);
if (height > lineheight*3) {
g.drawImage(image1, ints[0], ints[1] +SIZE, null);
} else if (height > lineheight) {
g.drawImage(image1, ints[0], ints[1] - lineheight + (int) (Y_LINEHEIGHT * 0.6), null);
} else {
g.drawImage(image1, ints[0], ints[1] - SIZE, null);
}
ints[0] = num = ints[2] = ints[0] + width;
} else if (j == chars.length - 1) {
for (int i = start; i < j + 1; i++) {
// g.drawLine(ints[0], ints[1], ints[2], ints[3]-SIZE);
int car = (int) chars[i];
// System.out.print(chars[i] + "---");
// System.out.println(car);
getInts(v, ints, car);
}
g.drawString(line.substring(start, j + 1), num, ints[1]);
// g.drawString(line.substring(start, j + 1), ints[0], ints[1] + SIZE);
start = j + 1;
if (j == chars.length - 1) {
break;
}
}
}
}
}
g.dispose();
//保存为jpg/png图片
FileOutputStream fos = new FileOutputStream(imageFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
// 每行的高 字体大小 行间距 图片的宽度
int[] ints = {lineheight, SIZE, (int) (SIZE * 0.2), IMAGE_WIDTH};
}
private void getInts(double v, int[] ints, int car) {
if (car < 10000) {
if (car == 32) { // 32英文空格
ints[0] = ints[0] + (int) (v * 0.45);
ints[2] = ints[2] + (int) (v * 0.45);
} else if (car <= 47) { // 运算符,47是/
ints[0] = ints[0] + (int) (v * 0.5);
ints[2] = ints[2] + (int) (v * 0.5);
} else if (car == 91 || car == 93) {// 91[ 92]
ints[0] = ints[0] + (int) (v * 0.4);
ints[2] = ints[2] + (int) (v * 0.4);
} else if (car < 60) {// 数字
ints[0] = ints[0] + (int) (v * 0.7);
ints[2] = ints[2] + (int) (v * 0.7);
} else if (car > 60 && car < 97) { // 大写字母
ints[0] = ints[0] + (int) (v * 0.75);
ints[2] = ints[2] + (int) (v * 0.75);
} else if (car == 109) {
ints[0] = ints[0] + (int) (v * 0.95);
ints[2] = ints[2] + (int) (v * 0.95);
} else if (car > 100 && car < 109) {
ints[0] = ints[0] + (int) (v * 0.7);
ints[2] = ints[2] + (int) (v * 0.7);
} else {// 小写字母
ints[0] = ints[0] + (int) (v * 0.55);
ints[2] = ints[2] + (int) (v * 0.55);
}
} else if (car < 19967) {
//
ints[0] = ints[0] + (int) (v * 0.95);
ints[2] = ints[2] + (int) (v * 0.95);
} else if (19968 <= car && car < 40869) {
// if (car < 19968 ){
// ints[0] = ints[0] + (int) (v * 0.9);
// ints[2] = ints[2] + (int) (v * 0.9);
// }else {
ints[0] = ints[0] + (int) v;
ints[2] = ints[2] + (int) v;
// }
} else {
ints[0] = ints[0] + (int) v;
ints[2] = ints[2] + (int) v;
}
}
// 获取图片的宽高
private static void getImgHW(String path) throws IOException {
File file = new File(path);
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
long size = file.length() / 1024;
System.out.println(height);
System.out.println(width);
System.out.println(size);
}
/**
* 获取到图像上下文
*
* @param image 图片
* @return Graphics
*/
private Graphics createGraphics(BufferedImage image) {
Graphics g = image.createGraphics();
// g.setColor(IMAGE_COLOR); //设置背景色
// g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);//绘制背景
g.setColor(Color.BLACK); //设置前景色
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
Font font = new Font("微软雅黑", Font.PLAIN, SIZE);
// 设置行间距
attributes.put(TextAttribute.TRACKING, 0.2);
Font font2 = font.deriveFont(attributes);
// 字体大小
g.setFont(font2); //设置字体
return g;
}
}