基于ABBYY SDK 实现java版本 Hello 功能!
1、ABBYY安装完成后
这个目录下存放示例代码,C:\ProgramData\ABBYY\SDK\11\FineReader Engine\Samples
2、新建工程把Hello 里面的代码和SamplesConfig.java 放入工程中
3、直接贴代码
SamplesConfig.java 用于存放***,本地安装路径等信息
package com.abbyy.test;
// � 2013 ABBYY Production LLC
// SAMPLES code is property of ABBYY, exclusive rights are reserved.
//
// DEVELOPER is allowed to incorporate SAMPLES into his own APPLICATION and modify it under
// the terms of License Agreement between ABBYY and DEVELOPER.
// Auto-generated config-file for FineReader Engine Java samples
public class SamplesConfig {
/**
* Folder with FRE dll
* 设置ABBYY所需dll文件地址
*/
public static String GetDllFolder() {
if( is64BitJVMArchitecture() ) {
return "C:\\Program Files\\ABBYY SDK\\11\\FineReader Engine\\Bin64";
} else {
return "C:\\Program Files\\ABBYY SDK\\11\\FineReader Engine\\Bin";
}
}
/**
* 设置 ABBYY所需 秘钥
* Return developer serial number for FRE
*/
public static String GetDeveloperSN() {
return "SWTT-1101-0006-5071-4505-1208"; //申请试用版***
}
/**
* Return full path to Samples directory
* 设置ABBYY转换后生成路径
*/
public static String GetSamplesFolder() {
// return "Directory\\where\\samples\\reside";
return "D:\\zhjwFile\\05_abbyy";
}
/**
* Determines whether the JVM architecture is a 64-bit architecture
* 判断当前jdk 所属版本是64还是32
*/
private static boolean is64BitJVMArchitecture()
{
String jvmKeys [] = {
"sun.arch.data.model",
"com.ibm.vm.bitmode",
"os.arch"
};
for( String key : jvmKeys ) {
String property = System.getProperty( key );
if( property != null ) {
if( property.indexOf( "64" ) >= 0 ) {
return true;
} else if( property.indexOf( "32" ) >= 0 ) {
return false;
} else if( property.indexOf( "86" ) >= 0 ) {
return false;
}
}
}
return false;
}
}
Hello.java 单个文件的转换
package com.abbyy.test.Hello;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
// (c) 2013 ABBYY Production LLC
// SAMPLES code is property of ABBYY, exclusive rights are reserved.
//
// DEVELOPER is allowed to incorporate SAMPLES into his own APPLICATION and modify it under
// the terms of License Agreement between ABBYY and DEVELOPER.
// ABBYY FineReader Engine 11 Sample
// This sample shows basic steps of ABBYY FineReader Engine usage:
// Initializing, opening image file, recognition and export.
import com.abbyy.FREngine.*;
import com.abbyy.test.SamplesConfig;
public class Hello {
public static void main(String[] args) {
try {
Hello application = new Hello();
application.Run();
} catch (Exception ex) {
displayMessage(ex.getMessage());
ex.printStackTrace();
}
}
public void Run() throws Exception {
// 加载ABBYY转换引擎
loadEngine();
try {
// 使用ABBYY FineReader引擎进行处理
processWithEngine();
} finally {
// 卸载ABBYY FineReader引擎
unloadEngine();
}
}
/**
* 加载本地环境,创建转换引擎
* @throws Exception
*/
private void loadEngine() throws Exception {
displayMessage("初始化引擎…");
engine = Engine.GetEngineObject(SamplesConfig.GetDllFolder(), SamplesConfig.GetDeveloperSN());
}
/**
* 使用ABBYY FineReader引擎进行处理
*/
private void processWithEngine() {
try {
// 设置转换参数
setupFREngine();
// 转换样例图片
processImage();
} catch (Exception ex) {
displayMessage(ex.getMessage());
}
}
/**
* 设置转换参数
*/
private void setupFREngine() {
displayMessage("预定义的配置文件加载…");
engine.LoadPredefinedProfile("Default");
// Possible profile names are:
// "DocumentConversion_Accuracy", 文档转换精度,
// "DocumentConversion_Speed", 文档转换速度
// "DocumentArchiving_Accuracy", 文档归档准确性
// "DocumentArchiving_Speed", 文档归档的速度
// "BookArchiving_Accuracy", 书存档准确性
// "BookArchiving_Speed", 书存档速度
// "TextExtraction_Accuracy", 文本提取的准确性
// "TextExtraction_Speed", 文本提取的速度
// "FieldLevelRecognition", 字段级识别
// "BarcodeRecognition_Accuracy", 条形码识别精度
// "BarcodeRecognition_Speed", 条形码识别速度
// "HighCompressedImageOnlyPdf", 高度压缩图像Pdf
// "BusinessCardsProcessing", 名片处理
// "EngineeringDrawingsProcessing", 工程图纸处理
// "Version9Compatibility", Version9兼容性
// "Default" 默认
}
/**
* 转换图片
*/
private void processImage() {
String fileName = "Demo_ChinesePRC.tif"; //Demo_ChinesePRC.tif
String imagePath = SamplesConfig.GetSamplesFolder() + "\\"+fileName;
try {
// Don't recognize PDF file with a textual content, just copy it
// 不要识别带有文本内容的PDF文件,只需复制它
if (engine.IsPdfWithTextualContent(imagePath, null)) {
displayMessage("复制的结果……");
String resultPath = SamplesConfig.GetSamplesFolder() + "\\"+fileName+".pdf";
Files.copy(Paths.get(imagePath), Paths.get(resultPath), StandardCopyOption.REPLACE_EXISTING);
return;
}
// 创建文档
IFRDocument document = engine.CreateFRDocument();
try {
// Add image file to document
displayMessage("加载图片……");
document.AddImageFile(imagePath, null, null);
// Process document
displayMessage("过程……");
// 创建识别器参数类
IDocumentProcessingParams dpp = engine.CreateDocumentProcessingParams();
IPageProcessingParams ppp = dpp.getPageProcessingParams();
ppp.getRecognizerParams().SetPredefinedTextLanguage("ChinesePRC+English"); //设置语言为简体中文和英语
document.Process(dpp);
// Save results
displayMessage("保存结果……");
// 使用默认参数将结果保存到rtf
String rtfExportPath = SamplesConfig.GetSamplesFolder() + "\\"+fileName+".rtf";
document.Export(rtfExportPath, FileExportFormatEnum.FEF_RTF, null);
// Save results to pdf using 'balanced' scenario
// 使用“平衡”场景将结果保存为pdf
IPDFExportParams pdfParams = engine.CreatePDFExportParams();
pdfParams.setScenario(PDFExportScenarioEnum.PES_Balanced);
String pdfExportPath = SamplesConfig.GetSamplesFolder() + "\\"+fileName+".pdf";
document.Export(pdfExportPath, FileExportFormatEnum.FEF_PDF, pdfParams);
} finally {
// Close document
document.Close();
}
} catch (Exception ex) {
displayMessage(ex.getMessage());
}
}
/**
* 卸载ABBYY FineReader引擎
* @throws Exception
*/
private void unloadEngine() throws Exception {
displayMessage("卸载ABBYY FineReader引擎...");
engine = null;
Engine.DeinitializeEngine();
}
private static void displayMessage(String message) {
System.out.println(message);
}
private IEngine engine = null;
}