//将文字转换为图片
public static boolean saveStringToPic(String content, String filePath) {
boolean rs;
int width = 1024; //300;
int height = 600;
int lineLength = 47;//23;
int lineLength2 = 47;//24;
String content1 = content;
String content2 = "";
String content3 = "";
int cnLineLength = lineLength;
for (int i = 0;i < content.length();i++) {
char ca = content.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength = cnLineLength - 1;
}
if (i >= cnLineLength) break;
}
if (content.length() > cnLineLength) {
content1 = content.substring(0, cnLineLength);
content2 = content.substring(cnLineLength);
String line2 = content2;
int cnLineLength2 = lineLength2;
for (int i = 0;i < line2.length();i++) {
char ca = line2.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength2 = cnLineLength2 - 1;
}
if (i >= cnLineLength2) break;
}
if (line2.length() > cnLineLength2) {
content2 = line2.substring(0, cnLineLength2);
content3 = line2.substring(cnLineLength2);
}
}
BufferedImage bi = null;
Graphics2D g2 = null;
OutputStream outPutStream = null;
try{
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2 = (Graphics2D) bi.getGraphics();
//g2.setBackground(Color.white);
Color bg = new Color(255, 255, 255);
g2.setColor(bg);
g2.fillRect(0, 0, width, height);
g2.setColor(Color.black);
Font font = new Font("宋体", Font.BOLD, 40);
g2.setFont(font);
g2.drawString(content1, 10, 200);
g2.drawString(content2, 10, 300);
g2.drawString(content3, 10, 400);
outPutStream = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outPutStream);
encoder.encode(bi);
rs = true;
} catch (Exception e) {
e.printStackTrace();
rs = false;
} finally {
if (g2 != null){
g2.dispose();
}
if (outPutStream != null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return rs;
}
public static boolean saveStringToPic(String content, String filePath) {
boolean rs;
int width = 1024; //300;
int height = 600;
int lineLength = 47;//23;
int lineLength2 = 47;//24;
String content1 = content;
String content2 = "";
String content3 = "";
int cnLineLength = lineLength;
for (int i = 0;i < content.length();i++) {
char ca = content.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength = cnLineLength - 1;
}
if (i >= cnLineLength) break;
}
if (content.length() > cnLineLength) {
content1 = content.substring(0, cnLineLength);
content2 = content.substring(cnLineLength);
String line2 = content2;
int cnLineLength2 = lineLength2;
for (int i = 0;i < line2.length();i++) {
char ca = line2.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength2 = cnLineLength2 - 1;
}
if (i >= cnLineLength2) break;
}
if (line2.length() > cnLineLength2) {
content2 = line2.substring(0, cnLineLength2);
content3 = line2.substring(cnLineLength2);
}
}
BufferedImage bi = null;
Graphics2D g2 = null;
OutputStream outPutStream = null;
try{
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2 = (Graphics2D) bi.getGraphics();
//g2.setBackground(Color.white);
Color bg = new Color(255, 255, 255);
g2.setColor(bg);
g2.fillRect(0, 0, width, height);
g2.setColor(Color.black);
Font font = new Font("宋体", Font.BOLD, 40);
g2.setFont(font);
g2.drawString(content1, 10, 200);
g2.drawString(content2, 10, 300);
g2.drawString(content3, 10, 400);
outPutStream = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outPutStream);
encoder.encode(bi);
rs = true;
} catch (Exception e) {
e.printStackTrace();
rs = false;
} finally {
if (g2 != null){
g2.dispose();
}
if (outPutStream != null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return rs;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货