ImageUtils excel 中 emf 转图片(解决图片上部分显示不全问题)图片转文字
excel 中ActiveX 工具 中的textbox ,以及公式 解析后为emf 图片,
emf 转图片(解决图片上部分显示不全问题) 图片转文字
/***************************
*<pre>
* @Project Name : base-case-test-service
* @Package : com.icil.pinpal.test
* @File Name : XX
* @Author : Sea
* @Date : 9/28/22 2:13 PM
* @Purpose :
* @History :
*</pre>
***************************/ import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.poi.util.IOUtils;
import org.freehep.graphicsio.emf.EMFInputStream;
import org.freehep.graphicsio.emf.EMFRenderer;
import org.freehep.graphicsio.emf.EMFTag;
import org.freehep.util.io.Tag;
import org.junit.Test; import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.List; /***************************
*<pre>
* <dependency>
* <groupId>net.sourceforge.tess4j</groupId>
* <artifactId>tess4j</artifactId>
* <version>4.6.1</version>
* </dependency>
* <dependency>
* <groupId>commons-httpclient</groupId>
* <artifactId>commons-httpclient</artifactId>
* <version>3.1</version>
* </dependency>
* <dependency>
* <groupId>org.freehep</groupId>
* <artifactId>freehep-graphicsio-emf</artifactId>
* <version>2.4</version>
* </dependency>
* @Project Name : base-case-test-service
* @Package : com.sea.base.test
* @File Name : ImageUtils
* @Author : Sea
* @Mail : lshan523@163.com
* @Date : 2022/9/19 16:09
* @Purpose :1.emf 文件转 png
* 2.图片转为文字
* 3.http 表单提交
* 4: 反射工具
* @History :
*</pre>
***************************/
@Slf4j
public class ImageUtils { private static ITesseract instance = null;
static
{
instance = new Tesseract();
instance.setDatapath("/home/sea/Desktop/FTP/tessdata-main");
instance.setLanguage("chi_sim+eng");
} /**
* 直接将emf 文件流转成文本
* @param emfBytes
*/
public static String emf2text(byte[] emfBytes) throws Exception
{
String tmpPath = "/tmp/"+UUID.randomUUID().toString().replaceAll("-", "")+".png";
File pngFile = emfToPng(emfBytes, tmpPath);
String text = img2Text(pngFile,true);
deleteFile(pngFile);
return text;
} public static String emf2text(File emfFile) throws Exception
{
byte[] emfBytes = file2byte(emfFile);
// emfFile.delete();
String tmpPath = "/tmp/"+UUID.randomUUID().toString().replaceAll("-", "")+".png";
File pngFile = emfToPng(emfBytes, tmpPath);
String text = img2Text(pngFile,true);
deleteFile(pngFile);
return text;
} /**
* @param picFile
* @return
* @throws Exception
*/
public static String img2Text(File picFile,boolean isBy3rd) throws Exception
{
String text = null;
if(isBy3rd==true)
{
String url= "http://192.168.186.129:5000/ocrimg";
HashMap<String, Object> formData = new HashMap<String, Object>() {{
put("language", "eng+chi_sim");
put("img", picFile);
}};
String s = postFormData(url, formData);
return s;
}else //local
{
BufferedImage img = ImageIO.read(picFile);
text = instance.doOCR(img);
}
return text;
} /**
* @param inEmfPath
* @param outPngPath
* @return
* @throws Exception
*/
public static File emfToPng( String inEmfPath,String outPngPath) throws Exception{
byte[] bytes = file2byte(inEmfPath);
// deleteFile(inEmfPath);
return emfToPng(bytes,outPngPath);
} /**
* 可以解决图片上部分显示不全问题
* @param emfBytes
* @param outPngPath
* @return
*/
public static File emfToPng( byte[] emfBytes,String outPngPath){
EMFInputStream eis = null;
File outFile = null;
try {
eis = new EMFInputStream(new ByteArrayInputStream(emfBytes), EMFInputStream.DEFAULT_VERSION);
EMFRenderer emfRender = new EMFRenderer(eis);
final int height = (int) eis.readHeader().getBounds().getHeight()+30;
final int width = (int) eis.readHeader().getBounds().getWidth()+30;
final BufferedImage bufImage = new BufferedImage(width, height ,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = bufImage.createGraphics();
// g2.setBackground(Color.RED);
// g2.drawString("嘻嘻嘻嘻",1,10); //可以添加水印
// emfRender.paint(g2);//原生方法<有问题,数据显示不全>
paint(g2,emfRender);
outFile = new File(outPngPath);
ImageIO.write(bufImage, "png",outFile);
return outFile; } catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(eis);
// deleteFile(outFile);
}
return outFile;
} /**
* @param g2
* @param emfRenderer
* @throws Exception
*/
public static void paint(Graphics2D g2, EMFRenderer emfRenderer) throws Exception{
emfRenderer.setViewportOrigin(new Point(-10, -15));
emfRenderer.setWindowOrigin(new Point(10, 10));
// emfRenderer.setBrushOrigin(new Point(10, 10)); g2.setBackground(Color.black); setFiledValue(emfRenderer,"g2",g2);
Shape clip = g2.getClip();
AffineTransform at = g2.getTransform();
Map hints = g2.getRenderingHints();
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
setFiledValue(emfRenderer,"initialTransform",g2.getTransform());
setFiledValue(emfRenderer,"path",null);
setFiledValue(emfRenderer,"figure",null);
setFiledValue(emfRenderer,"meterLimit",10);
setFiledValue(emfRenderer,"windingRule",0);
setFiledValue(emfRenderer,"bkMode",2);
setFiledValue(emfRenderer,"useCreatePen",true);
setFiledValue(emfRenderer,"scaleMode",4);
// setFiledValue(emfRenderer,"windowOrigin",null);
// setFiledValue(emfRenderer,"viewportOrigin",null);
setFiledValue(emfRenderer,"windowSize",null);
setFiledValue(emfRenderer,"viewportSize",null);
setFiledValue(emfRenderer,"mapModeIsotropic",false);
setFiledValue(emfRenderer,"mapModeTransform",AffineTransform.getScaleInstance(emfRenderer.TWIP_SCALE, emfRenderer.TWIP_SCALE));
runMethod(emfRenderer,"resetTransformation",g2,Graphics2D.class);
setFiledValue(emfRenderer,"initialClip",g2.getClip());
Vector tags = (Vector)getPrivateFiledValue(emfRenderer, "tags");
for(int i = 0; i < tags.size(); ++i) {
Tag tag = (Tag)tags.get(i);
if (tag instanceof EMFTag) {
((EMFTag)tags.get(i)).render(emfRenderer);
} else {
log.warn("unknown tag: " + tag);
}
}
g2.setRenderingHints(hints);
g2.setTransform(at);
g2.setClip(clip);
} /**
* <dependency>
* <groupId>commons-httpclient</groupId>
* <artifactId>commons-httpclient</artifactId>
* <version>3.1</version>
* </dependency>
* @param url
* @param formData
* @Author Sea
* @return
*/
public static String postFormData(String url,Map<String,Object> formData)
{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
try{
List<Part> formArgsList = new ArrayList<>();
for (Map.Entry<String, Object> kv :formData.entrySet()) {
String key = kv.getKey();
Object value = kv.getValue();
if(value instanceof File){
FilePart fp = new FilePart(key, (File) value);
fp.setCharSet("utf-8");
formArgsList.add(fp);
}else
{
formArgsList.add(new StringPart(key, value+"", "UTF-8"));
}
}
Part[] parts = formArgsList.toArray(new Part[formArgsList.size()]);
MultipartRequestEntity entity = new MultipartRequestEntity(parts, new HttpMethodParams());
post.setRequestEntity(entity);
client.executeMethod(post);
//释放连接,以免超过服务器负荷
String responseBodyAsString = post.getResponseBodyAsString();
return responseBodyAsString;
}
catch (Exception e)
{
e.printStackTrace();
}finally {
//释放连接,以免超过服务器负荷
post.releaseConnection();
}
return null;
} public static void deleteFile(File file){
if(file!=null&&file.exists()){
file.delete();
}
}
/**
* @param filePath
* @return
*/
public static byte[] file2byte(String filePath)throws Exception
{
File file = new File(filePath);
byte[] bytes = file2byte(file);
return bytes;
}
/**
* @param file
* @return
*/
public static byte[] file2byte(File file) throws Exception
{
FileInputStream fis = new FileInputStream(file);
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(fis);
fis.close();
return bytes;
} /**
* @param obj
* @param fileName
* @param value
* @throws Exception
*/
public static void setFiledValue(Object obj, String fileName, Object value) throws Exception {
Field name = obj.getClass().getDeclaredField(fileName);
name.setAccessible(true);
name.set(obj,value);
}
/**
* 获取类的私有属性 : eg:name
* @param objClss
* @param fileName
* @throws Exception
*/
public static Object getPrivateFiledValue(Object objClss, String fileName) throws Exception {
Field name = objClss.getClass().getDeclaredField(fileName);
name.setAccessible(true);
Object o = name.get(objClss);
return o;
} /**
* @param obj
* @param methodName
* @param args
* @param parameterTypes
* @return
* @throws Exception
*/
public static Object runMethod(Object obj, String methodName, Object args,Class<?>... parameterTypes) throws Exception {
Method declaredMethod = obj.getClass().getDeclaredMethod(methodName, parameterTypes);
declaredMethod.setAccessible(true);
Object invoke = declaredMethod.invoke(obj, args);
return invoke;
} @Test
public void testOcr() throws Exception {
String emfPath = "/home/sea/Desktop/FTP/TextBox2.emf";
String outPath ="/home/sea/CCCCCCCCCCC/AAAA_svn_new_AAAA/base-case-test-service/src/test/resources/TextBox1.png";
emfToPng(emfPath ,outPath );
BufferedImage img = ImageIO.read(new File(outPath));
ITesseract instance = new Tesseract();
instance.setDatapath("/home/sea/Desktop/FTP/tessdata-main");
instance.setLanguage("chi_sim+eng");
String result = instance.doOCR(img);
System.err.println(result); } }
ImageUtils excel 中 emf 转图片(解决图片上部分显示不全问题)图片转文字的更多相关文章
- javascript DOM(2) 一个网页上切换显示不同的图片或文本
摘自: javascript DOM 编程艺术 1. 在一个网页上切换显示不同的图片 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- 解决XP系统任务管理器显示不全
我们在使用电脑的时候有的时候打开任务管理器会发现任务管理器显示不全. 当碰到这种情况怎么解决呢?任务管理器显示不全的原因又是那些呢? 这里就来为大家分享下为什么任务管理器会显示不全以及如何解决这个问题 ...
- OAF_文件系列4_实现OAF上传显示数据库动态图片Image(案例)
20150805 Created By BaoXinjian
- 在Vue项目中,添加的背景图片在服务器上不显示,如何处理
遇到的问题: 在vue项目开发过程中,我们常常会在页面中添加背景图片.可是当我们在样式中添加了背景图片,编译打包部署到服务器上时,发现图片并不能显示出来,这是为什么呢~~~ 我们一般写的css样式如下 ...
- vue中图片返回404时,显示默认的图片
图片返回404时候的处理 <img :src="userMsg.portrait" ref="img" alt=""> _thi ...
- img标签中的图片加载异常时显示默认的图片
备忘:
- linux上不能显示Jfreechart的图片文件
出现错误: Jan 23, 2015 4:19:21 PM org.apache.catalina.core.StandardWrapperValve invokeSEVERE: Servlet.s ...
- JS使用默认图片代替页面上无法显示的图片
1.js方法: function replaceErrorImg(obj) { obj.src="images/common/error.bmp"; } 2.jquery绑定 $( ...
- 修改DEDECMS文章标题长度,解决DEDECMS文章标题显示不全
dedecms系统使用过程中,常遇到输入的标题原本输入的字数跟保存成功后的数字长度不一样,这个是因为 织梦dedecms系统默认的文章标题字数限制是60,也就是只能输入30个汉字,超过的会自动截断删除 ...
- 解决TextView最后一行显示不全
public class MultilineTextView extends TextView { private boolean calculatedLines = false; public Mu ...
随机推荐
- centos7最小化系统安装(ifconfig找不到)
先我们安装后centos7最小化系统后,并进入系统执行命令ifconfig,会发现系统提示命令未找到.具体展示效果如下图所示. 然后输入命令查看本机是否分配IP,执行命令ip addr ,可以发现系统 ...
- Linux 系统挂载 ntfs 移动硬盘无法写入的问题
linux 下挂载 ntfs 移动硬盘无法写入问题 在机房使用移动硬盘时发现无法写入硬盘,具体是 Ubuntu 21.04 ,移动硬盘是 SSD ,分区类型是 NTFS . 首先百度得知最优办法是安装 ...
- Pytest Fixture(一)
Fixture 是一些函数,pytest 会在执行测试函数之前(或之后)加载运行它们.我们可以用它做一些事情,比如数据库的链接操作之类的 import pytest @pytest.fixture() ...
- Oracle数据库简单常用语句
简单常用语句: 登录超级用户 sqlplus / as sysdba; 登录普通用户 connect username/password; 显示当前用户名 show user; 查询所有用户名 sel ...
- 如何在 Linux 上扫描/检测新的 LUN 和 SCSI 磁盘
当 Linux 系统连接到 SAN(存储区域网络)后,你需要重新扫描 iSCSI 服务以发现新的 LUN. 要做到这一点,你必须向存储团队提供 Linux 主机的 WWN 号和所需的 LUN 大小. ...
- MySQL经典45题
一.数据库字段说明 1.学生表 Student(SId,Sname,Sage,Ssex)SId :学生编号Sname:学生姓名Sage :出生年月Ssex:学生性别 2.课程表 Course(CId, ...
- 半成品 java 身份证校验
public static Boolean is18Card(String idCard18) { //证件省份 HashMap<String, String> aCity = new H ...
- c++学习 5 预处理
一 内存分区 内存的分区变量存储,一般可以分为以下五个区,它们分别是: 可读可写 堆区:使用malloc.calloc.realloc.free以及c++里面的new和delete去动态申请. ...
- 二叉树系列之Treap树
Treap是一棵拥有键值.优先级两种权值的树 struct node{ int size;//以这个结点为根的子树的结点总数量,用于名次树 int rank;//优先级 int key ...
- angular请求头部加XSRF-TOKEN
1.创建拦截器 import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, } from '@angular/common/http' ...