JAVA 将文字转换为图片
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;
}
相关推荐
基于Java编程语言开发的图像文字识别系统_利用OCR技术实现图片中字符的自动提取与转换_适用于文档数字化车牌识别票据处理和图像内容分析等多种场景_集成Tesseract引擎.zip
基于Java编程语言开发的图像文字识别系统_利用OCR技术实现图片中字符的自动提取与转换_适用于文档数字化车牌识别票据处理和图像内容分析等多种场景_集成Tesseract引擎.zip
Java 将xml模板动态填充数据转换为word文档
实现思路: 1、先将word文档另存为 : Word 2003 XML文档(*.xml) 格式的模板文件; 2、通过Java程序,使用freemarker.jar包转换为xml格式的word文档文件 3、使用jacob包,将xml格式的word文档文件,转换为docx格式的文档文件具体实现步骤:一、模板编辑 1、将word文档另存为xml模板: 2、模板中需要动...
Java实现emf转jpg png 图片转换
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、GraphicsMagick的安装二、结合im4java使用实现图片转换1.引入maven依赖2.图片转换3.测试代码参考链接 前言 本文旨在帮助需要处理emf转png, jpg, 目前网上能搜到emf转jpg的文章, 博客基本上都是12年写的, 用的是freehep, 但是用freehep有个问题就是转出来的图片出现文字丢失, 或者颜色异常, 因此freehep无法满足满足我的需求. 本文主要使用GraphicsMa
Aspose pdf文件操作工具类
实现功能:Excel转pdf,Word转pdf,多个pdf文件合并,给pdf文件添加文字水印,给pdf添加图片水印 package com.jeesite.modules.jss.Utils; import com.aspose.cells.Workbook; import com.aspose.words.Document; import com.lowagie.text.Image; import com.lowagie.text.pdf.*; import java.io.File; import
图片处理工具类(缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等)
NULL 博文链接:https://zhaoshijie.iteye.com/blog/1849296


Web 开发
