Commit 98afdbc6 by lilei

删除ocr相关代码

parent 67ee9f81
...@@ -55,13 +55,6 @@ ...@@ -55,13 +55,6 @@
<systemPath>${pom.basedir}/src/main/webapp/WEB-INF/lib/ocr-precision-1.0.jar</systemPath> <systemPath>${pom.basedir}/src/main/webapp/WEB-INF/lib/ocr-precision-1.0.jar</systemPath>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<scope>system</scope>
<version>5.5.0</version>
<systemPath>${pom.basedir}/src/main/webapp/WEB-INF/lib/jna-5.5.0.jar</systemPath>
</dependency>
<dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>${hutool.version}</version> <version>${hutool.version}</version>
......
package com.founder.controller;
import com.alibaba.fastjson.JSONObject;
import com.founder.service.OcrService;
import com.founder.util.OcrFounder;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Base64;
import java.util.Date;
import sun.misc.BASE64Encoder;
@RestController
@RequestMapping(value="/ocr",produces = "application/json; charset=utf-8")
public class OcrController {
@Autowired
private OcrService ocrjService;
@Autowired
private OcrFounder ocrFounder;
@ResponseBody
@CrossOrigin
@PostMapping("/InvoiceOcr")
public JSONObject query(HttpServletRequest request, MultipartFile file) {
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
Date date = new Date();
System.out.println(df2.format(date)+"开始时间");
byte[] fileByte = null;
try {
fileByte = file.getBytes();
} catch (IOException e) {
}
BASE64Encoder encoder = new BASE64Encoder();
String voiceBase64= encoder.encode(fileByte);
JSONObject j = new JSONObject();
j.put("msg",ocrjService.getOcr(voiceBase64));
Date date1 = new Date();
DateFormat df3 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df3.format(date1)+"结束时间");
return j;
}
@ResponseBody
@CrossOrigin
@PostMapping("/OcrFounder")
public JSONObject queryOcrFounder(HttpServletRequest request, MultipartFile file) {
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
Date date = new Date();
System.out.println(df2.format(date)+"开始时间");
String result="";
byte[] fileByte = null;
try {
fileByte = file.getBytes();
} catch (IOException e) {
}
String ocrRes="";
try {
ocrRes= ocrFounder.getOcrFounder(fileByte);
} catch (Exception e) {
e.printStackTrace();
}
//图片文字与简要案情比对
Float ocrCompJyaq=getSimilarityRatio(ocrRes,"立案");
//图片文字与嫌疑人比对
Float ocrCompZbfzxry=getSimilarityRatio(ocrRes,"王岩");
//图片文字与受害人人比对
Float ocrCompBhr=getSimilarityRatio(ocrRes,"大类");
//图片文字与受害人人比对
Float ocrCompws=getSimilarityRatio(ocrRes,"立案决定书");
if(ocrCompws<0.000001){
}else
if(ocrCompJyaq<0.000001&&ocrCompZbfzxry<0.000001&&ocrCompBhr<0.000001)
{
}
JSONObject j = new JSONObject();
j.put("msg",ocrRes);
Date date1 = new Date();
DateFormat df3 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df3.format(date1)+"结束时间");
return j;
}
@ResponseBody
@CrossOrigin
@PostMapping("/OcrFounderByte")
public JSONObject queryOcrFounderFlws(@RequestBody String fileByte) {
final Base64.Decoder decoder = Base64.getMimeDecoder();
byte[] data=decoder.decode(fileByte);
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
String result="";
try {
result= ocrFounder.getOcrFounder(data);
} catch (Exception e) {
e.printStackTrace();
}
JSONObject j = new JSONObject();
j.put("msg",result);
Date date1 = new Date();
DateFormat df3 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df3.format(date1)+"结束时间");
return j;
}
public static float getSimilarityRatio(String str, String target) {
int d[][]; // 矩阵
int n = str.length();
int m = target.length();
int i; // 遍历str的
int j; // 遍历target的
char ch1; // str的
char ch2; // target的
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
if (n == 0 || m == 0) {
return 0;
}
d = new int[n + 1][m + 1];
for (i = 0; i <= n; i++) { // 初始化第一列
d[i][0] = i;
}
for (j = 0; j <= m; j++) { // 初始化第一行
d[0][j] = j;
}
for (i = 1; i <= n; i++) { // 遍历str
ch1 = str.charAt(i - 1);
// 去匹配target
for (j = 1; j <= m; j++) {
ch2 = target.charAt(j - 1);
if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
temp = 0;
} else {
temp = 1;
}
// 左边+1,上边+1, 左上角+temp取最小
d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + temp);
}
}
return (1 - (float) d[n][m] / Math.max(str.length(), target.length())) * 100F;
}
}
package com.founder.util;
import cn.com.winmage.soft.ocr.OcrUtil;
import com.sun.jna.ptr.LongByReference;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.*;
import java.io.*;
@Component
public class OcrFounder {
private static String dllPath="/opt";
private static String dllName="libwmreader.so"; //lunix 使用 libwmreader.so
private static String image="D:\\ocr\\images\\img2.jpg";
public String getOcrFounder(byte[] data) throws Exception {
int code= OcrUtil.init(dllPath,dllName);
if(code!=0){
System.out.println("初始化失败,错误码:"+code);
System.exit(0);
}
code= OcrUtil.wm_set_image_stream(data,data.length,1);
if(code!=0){
System.out.println("加载图片路径失败,错误码:"+code);
System.exit(0);
}
code=OcrUtil.recognize();
if(code!=0){
System.out.println("识别图像失败,错误码:"+code);
System.exit(0);
}
String result=OcrUtil.getResult().replaceAll(" ", "");
return result;
}
public static void main(String[] args) throws Exception {
/*readImage(image);*/
int code= OcrUtil.init(dllPath,dllName);
if(code!=0){
System.out.println("初始化失败,错误码:"+code);
System.exit(0);
}
int bit_depth=0;
byte[] data = null;
int width=0;
int height=0;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(image));
BufferedImage sourceImg = ImageIO.read(new FileInputStream(new File(image)));
width=sourceImg.getWidth();
height=sourceImg.getHeight();
ColorModel color = sourceImg.getColorModel();
bit_depth=color.getPixelSize();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
code= OcrUtil.wm_set_image_stream(data,data.length,1);
if(code!=0){
System.out.println("加载图片路径失败,错误码:"+code);
System.exit(0);
}
/* ImageInfo imageInfo= loadTestImage(image);
data=imageInfo.getImage();
try {
FileInputStream inputfile = new FileInputStream(new File(image));
*//** he java.io.FileInputStream.available() method returns number of
* remaining bytes that can be read from this input stream without
* blocking by the next method call for this input stream. The next
* method call can also be the another thread.
* 通过available方法取得流的最大字符数*//*
byte[] b = new byte[inputfile.available()];
if (b.length == 0) {
System.out.print("the file is empty!!!");
return;
}
inputfile.read(b);
inputfile.close();
int b1 = b[0] & 0xff;
int b2 = b[1] & 0xff;
if (b1 == 0x42 && b2 == 0x4d) {
checkBmp(b);
} else if (b1 == 0x47 && b2 == 0x49) {
checkGif(b);
} else if (b1 == 0x89 && b2 == 0x50) {
checkPng(b);
} else if (b1 == 0xff && b2 == 0xd8) {
String type = "JPEG 图像";
int i = 2;
while (true) {
int marker = (b[i] & 0xff) << 8 | (b[i + 1] & 0xff);
int size = (b[i + 2] & 0xff) << 8 | (b[i + 3] & 0xff);
if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4
&& marker != 0xffc8) {
bit_depth = (b[i + 4] & 0xff) * (b[i + 9] & 0xff);
break;
} else {
i += size + 2;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
int DataSizePerLine= (width* bit_depth+31)/32*4;
code=OcrUtil.wm_set_image_data(data,width,height,bit_depth,DataSizePerLine,1);
if(code!=0){
System.out.println("加载图片路径失败,错误码:"+code);
System.exit(0);
}*/
/* code=OcrUtil.loadImage(image,1);
if(code!=0){
System.out.println("加载图片路径失败,错误码:"+code);
System.exit(0);
}*/
code=OcrUtil.recognize();
if(code!=0){
System.out.println("识别图像失败,错误码:"+code);
System.exit(0);
}
String result=OcrUtil.getResult();
System.out.println("识别图像成功,识别结果:"+result);
int total=OcrUtil.getCellNum();
System.out.println("识别到文本行数为:"+total);
LongByReference left=new LongByReference();
LongByReference top=new LongByReference();
LongByReference right=new LongByReference();
LongByReference bottom=new LongByReference();
byte b=OcrUtil.getCellPos(1,left,top,right,bottom);
/* if(b==0){
System.out.println("获取第一行位置信息失败,错误码:"+b);
System.exit(0);
}*/
System.out.println("第一行坐标获取成功,左:"+left.getValue()+"右:"+right.getValue()+"上:"+top.getValue()+"下:"+bottom.getValue());
OcrUtil.unInit();
}
/*public static ImageInfo loadTestImage(String path) throws Exception{
File img = new File(path);
if (!img.exists()) {
return null;
}
BufferedImage image = null;
try {
image = ImageIO.read(img);
} catch (Exception e) {
if(image == null){
String newpath= readImage(path);//将CMYK模式转为rgb模式
img = new File(newpath);
image = ImageIO.read(img);
}
if(image == null){
System.out.println("转换图片失败,源图片路径:"+path+ ";原因:该图片无法转换成rgb");
return null;//如果还是无法转化,则跳过
}
}
if (image == null) {
return null;
}
final int width = image.getWidth();
final int height = image.getHeight();
int[] pix = new int[width * height];
PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pix, 0, width);
try {
if (!pg.grabPixels()) {
return null;
}
} catch (InterruptedException e) {
System.out.println("转换图片失败,源图片路径:"+path+ ";原因:该图片无法转换成rgb.");
}
ImageInfo imageInfo = new ImageInfo();
imageInfo.setWidth(width);
imageInfo.setHeight(height);
byte[] rgb24 = new byte[width * height * 3];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int idx = width * i + j;
int color = pix[idx]; //获取像素
int red = ((color & 0x00FF0000) >> 16);
int green = ((color & 0x0000FF00) >> 8);
int blue = color & 0x000000FF;
int rgbIdx = idx * 3;
rgb24[rgbIdx] = (byte) red;
rgb24[rgbIdx + 1] = (byte) green;
rgb24[rgbIdx + 2] = (byte) blue;
}
}
imageInfo.setImage(rgb24);
return imageInfo;
}
public static String readImage(String filename) throws IOException {
File file = new File(filename);
ImageInputStream input = ImageIO.createImageInputStream(file);
Iterator readers = ImageIO.getImageReaders(input);
if(readers == null || !readers.hasNext()) {
throw new RuntimeException("1 No ImageReaders found");
}
ImageReader reader = (ImageReader) readers.next();
reader.setInput(input);
String format = reader.getFormatName() ;
BufferedImage image;
if ( "JPEG".equalsIgnoreCase(format) ||"JPG".equalsIgnoreCase(format) ) {
try {
// 尝试读取图片 (包括颜色的转换).
image = reader.read(0); //RGB
} catch (IIOException e) {
// 读取Raster (没有颜色的转换).
Raster raster = reader.readRaster(0, null);//CMYK
image = createJPEG4(raster);
}
image.getGraphics().drawImage(image, 0, 0, null);
String dstfilename = filename.substring(0,filename.lastIndexOf("."))+"_rgb"+filename.substring(filename.lastIndexOf("."));
String newfilename = filename;
File newFile = new File(dstfilename);
FileOutputStream out = new FileOutputStream(newFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
out.close();
return dstfilename;
}
return null;
}
private static BufferedImage createJPEG4(Raster raster) {
int w = raster.getWidth();
int h = raster.getHeight();
byte[] rgb = new byte[w * h * 3];
//彩色空间转换
float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i],
cr = 255 - Cr[i];
double val = y + 1.402 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);
val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);
val = y + 1.772 * (cb - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);
}
raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[]{0, 1, 2}, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, (WritableRaster) raster, true, null);
}*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment