package com.asprise.util.ocr;
import java.awt.image.RenderedImage;
import java.util.Vector;
public class OCR
{
private static final int MODE_CHARACTERS = 1;
private static final int MODE_BARCODES = 2;
private static final int MODE_ALL = 3;
private static final String TAG_UNKNOWN = "#&*!@";
private static final String TAG_BARCODE_START = "[BARCODESTART";
private static final String TAG_BARCODE_END = "[BARCODEEND";
private static final String TAG_PICTURE = "[PIC]";
private static boolean libraryLoaded = false;
private static String libPath = null;
public OCR()
{
loadLibrary();
}
public String recognizeEverything(RenderedImage image)
{
return (String)processString(image, 3, "[", "]");
}
public String recognizeEverything(RenderedImage image, String barcodePrefix, String barcodeSuffix)
{
return (String)processString(image, 3, barcodePrefix, barcodeSuffix);
}
public String recognizeCharacters(RenderedImage image)
{
return (String)processString(image, 1, null, null);
}
public String recognizeBarcode(RenderedImage image)
{
Vector v = (Vector)processString(image, 2, null, null);
if(v == null || v.size() == 0)
{
return null;
} else
{
return (String)v.elementAt(0);
}
}
public Vector recognizeBarcodes(RenderedImage image)
{
return (Vector)processString(image, 2, null, null);
}
private Object processString(RenderedImage image, int mode, String prefix, String suffix)
{
if(image == null || image.getHeight() <= 0 || image.getWidth() <= 0)
{
return null;
}
String s = performOCR(image, mode);
if(s == null)
{
return s;
}
StringBuffer buf = new StringBuffer();
int cursor;
int pos;
for(cursor = 0; cursor < s.length(); cursor = pos + "#&*!@".length())
{
pos = s.indexOf("#&*!@", cursor);
if(pos < 0)
{
buf.append(s.substring(cursor));
break;
}
buf.append(s.substring(cursor, pos));
buf.append(' ');
}
s = buf.toString();
cursor = 0;
int posStart = -1;
int posEnd = -1;
StringBuffer sb = new StringBuffer();
String barcode = null;
Vector barcodes = new Vector();
for(; cursor < s.length(); cursor = posEnd + "[BARCODEEND".length())
{
posStart = s.indexOf("[BARCODESTART", cursor);
if(posStart == -1 && cursor == 0)
{
switch(mode)
{
case 1: // '\001'
case 3: // '\003'
return s;
case 2: // '\002'
return barcodes;
}
}
if(posStart == -1)
{
switch(mode)
{
case 1: // '\001'
case 3: // '\003'
sb.append(s.substring(cursor));
return sb.toString();
case 2: // '\002'
return barcodes;
}
}
sb.append(s.substring(cursor, posStart));
cursor = posStart + "[BARCODESTART".length();
posEnd = s.indexOf("[BARCODEEND", cursor);
if(posEnd == -1)
{
throw new RuntimeException("Please contact Asprise support: support@asprise.com. Errro code: NO CLOSING BARC" +
"ODE END TAG!"
);
}
barcode = s.substring(posStart + "[BARCODESTART".length(), posEnd);
switch(mode)
{
case 3: // '\003'
sb.append(prefix + barcode + suffix);
break;
case 2: // '\002'
barcodes.add(barcode);
break;
}
}
throw new RuntimeException("Please contact Asprise support: support@asprise.com. SHOULD NOT REACH HERE!");
}
private native String performOCR(RenderedImage renderedimage, int i);
private static void loadLibrary()
{
if(libraryLoaded)
{
return;
}
if(libPath != null)
{
try
{
System.load(libPath);
libraryLoaded = true;
return;
}
catch(Throwable t)
{
t.printStackTrace();
}
}
System.loadLibrary("AspriseOCR");
libraryLoaded = true;
}
public static void setLibraryPath(String libraryPath)
{
libPath = libraryPath;
}
public static String getLibraryPath()
{
return libPath;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
OCR.rar (1个子文件)
OCR.java 5KB
共 1 条
- 1
资源评论
邓凌佳
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功