精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。
OCR是指光学字符识别,它其实是一种图片模式匹配查找。OCR开发难度大,涉及到图像和算法,对初学者来说不好掌握。通过一些现成的开发包来先掌握步骤和一些思路是一个不错的选择,本文介绍了用OCR Xpress进行OCR应用开发。
There were a few functionalities that I required before I channeled my inner Walt Whitman. The first was OCR, the second was word/phrase search, the third was redaction or, in my case, negated redaction, and the final thing was saving it to a file. In this article, I will briefly go over how I was able to create a small application using OCR Xpress.我需要开发一些功能。第一个是OCR,第二个是单词/短语搜索,第三个是编辑,或者在我的情况下,否定编辑,最后的事情是将其保存到文件中。在本文中,我将简要介绍一下如何使用OCR Xpress创建一个小应用程序。
Getting OCR up and running was my first step to becoming a modern-day poet. OCR is the conversion of an image (or images) that has typed / printed text to an electronic output. OCR has many use cases for many different companies and also has a practical need for the blind and visually impaired. My intent, however, was much more whimsical.让OCR正常运行是我成功的第一步。OCR是将具有打字/打印文本的图像(或多个图像)转换为电子输出。OCR为许多不同的公司提供了许多用例,并且对盲人和视障者也有实际需求。然而,我的意图更加异想天开。
//required accusoft dependencies
package com.accusoft.ocrxpress.samples;
import com.accusoft.ocrxpress.*;
//java dependencies
import java.awt.Color;
import java.awt.image.*:
import javax.imageio.*;
import java.io.*;
import java.util.*;
//Display Image
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Flowlayout:
I took the Memory sample that OCR Xpress was packaged with and added a NewspaperBlackout class. Above are the imports and the package that I needed within my class.
There are were a few different ways to code this application, but I decided to create a couple helper functions. Below I used InputString, DisplayImage, ConvertToBlack, and ConvertRect.我拿了OCR Xpress打包的Memory样本并添加了一个NewspaperBlackout类。以上是我导入包的代码。
有几种不同的方法来编写这个应用程序,但我决定创建一对辅助函数。下面我用InputString,DisplayImage,ConvertToBlack,和ConvertRect。
public class NewspaperBlackout
{
public static String InputString() {
Scanner scanner = new scanner (System.in);
System.out.prints "Type your poem. <Press Enter for default>");
return scanner.nextLine();
}
This helper function allows you to either hit "Enter" for the default poem, or type your own poem within the Console window.此助手功能允许您点击“输入”作为默认来源,或在控制台窗口中键入您自己的来源。
public static void DisplayImage(BufferedImage bi) {
ImageIcon icon = new ImageIcon(bi);
JFrame frame = new JFrame();
frame.setLayouts new FlowLayouts());
frame.setSize (bi.getWidth(), bi.getHeight());
JLabel lbl = new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setWisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_0N_close);
}
The helper function displays the final output in a JFrame.辅助函数在JFrame中显示最终输出。
public static void convertToBlack(BufferedImage bi) {
Color c = new Color (0,0,0); //black pixel
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y ++) {
bi.setRGB(x, y, c.getRGB());
}
}
}
This helper function converts the image to completely black.
There are many different ways to accomplish this, and I recommend using the method you feel most comfortable with.
此辅助函数将图像转换为完全黑色。有许多不同的方法可以实现这一点,我建议使用您觉得最舒服的方法。
public static java.awt.Rectangle ConvertRect
(com.accusoft.ocrxpress.Rectangle accusoftRect) {
java.awt.Rectangle javaRect = new java.awt.Rectangle();
javaRect.x = accusoftRect.getLeft();
javaRect.y = accusoftRect.getTop ();
javaRect.width = accusoftRect.getRight() - accusoftRect.getLeft();
javaRect.height = accusoftRect.getBottom() - accusoftRect.getTop();
return javaRect;
}
This helper function takes in an Accusoft rectangle, which has Top, Bottom, Right, and Left, and converts it to the standard Java rectangle, x, y, width, and height.
这个辅助函数接受一个Accusoft矩形,它有Top,Bottom,Right和Left,并将它转换为标准的Java矩形,x,y,width和height。
public static void main (String[] args) throws OcrxException {
String inputImagePath = "images/NP01.bmp";
BufferedImage originalImg = null, outputImg = null;
//Load and display original image
try {
originalImg = ImageIO.read(new File (inputImagePath));
}
catch (IOException e) {
e.addSuppressed(e);
return;
}
Once I had the newspaper scanned and saved as a BMP image, I was ready to load my image into a bufferedImage by calling ImageIO.read.
一旦我将报纸扫描并保存为BMP图像,我就可以通过调用ImageIO.read将图像加载到bufferedImage其中。
0crXpress ocrx = new 0crXpress();
initializeLicensing(ocrx);
RecognitionParameters parameters = new RecognitionParameters();
parameters.setLanguage(Language.ENGLISH);
Document document = ocrx.recognizeToMemory(parameters, original Img);
Image scanned to BMP? Check. BMP loaded as a bufferedimage? Check. Now onto the next step of my project. OCR Xpress provided a few options: output to file, output to PDF, and output to memory. For my needs, I wanted to use recognizeToMemory. I needed to initialize the OCR Xpress engine, and RecognitionParameters. Then I simply called recognizeToMemory to OCR the image to memory.图像扫描到BMP?。BMP加载为bufferedimage?。现在进入我项目的下一步。OCR Xpress提供了一些选项:输出到文件,输出到PDF,以及输出到内存。根据我的需要,我想用recognizeToMemory。我需要初始化OCR Xpress引擎,并且RecognitionParameters。然后我简单地recognizeToMemory将OCR图像调用到内存中。
Now that I had my newspaper in an electronic form, I could search for words/phrases. This also allowed me to find the rectangle area of each word/phrase that I searched for. I was able to add these to a list of RECTs, which I then merged with my "blackout" page.现在我以电子形式收到了报纸,我可以搜索单词/短语。这也让我找到了我搜索过的每个单词/短语的矩形区域。我能够将这些添加到RECT列表中,然后我将其与“突出”页面合并。
String searchString = InputString(); if (searchString.isEmpty()) { searchString = "You give something possible by psychic sign passed forth on networks?"; } String[] searchWords = searchString.split("\\s+");
Using the helper function InputString, all I needed to do was pass in a default value and populate the string array with Java’s split function using either the users input or the default value.
This part was the most difficult, I actually had to stop and think of a poem to write, using the words given in the article.
使用辅助函数InputString,我需要做的就是传入一个默认值,以用户输入或默认值为数据来源,使用Java的split函数填充字符串数组。
这部分是最难的,我实际上不得不停下来思考,用文章中给出的文字来写。
The next step (which seemed to be most difficult) was also made easy. First, let me explain redaction and what it’s used for. Redaction is the process of censoring or obscuring part of a text for legal or security purposes. When someone says redaction, you might think top secret "your eyes only" documents. However, every company has sensitive material that they may wish to redact, such as customer data, company data, or employee data to name a few. However, my redaction needs are more of the "I love you" variety. Once the text was redacted, I was then able to merge the redacted areas with the original image that was converted to black.下一步(似乎是最困难的)也很容易。首先,让我解释一下编辑及其用途。编辑是为了法律或安全目的而审查或模糊部分文本的过程。当有人说编辑时,你可能会认为绝密的“只有你的眼睛”文件。但是,每家公司都有他们可能希望编辑的敏感材料,例如客户数据,公司数据或员工数据等。但是,我的编辑需求更多的是“我爱你”。一旦文本被编辑,我就可以将编辑区域与转换为黑色的原始图像合并。
try { outputImg = ImageIO.read(new File (inputImagePath)); ConvertsToBlack(outputImg); } catch (IOException e) { e.addSuppressed(e); return; }
There are a few different ways to copy the original image, but for simplicity I created a new bufferedImageoutputImg. For this image, I needed to convert all pixels to black. This is where the ConvertToBlack helper function comes into play.有几种不同的方法来复制原始图像,但为了简单起见,我创建了一个新的bufferedImage outputImg。对于此图像,我需要将所有像素转换为黑色。这是ConvertToBlack帮助函数发挥作用的地方。
int curWord = 0;
for (Word word : document.getWords()) {
if (curWord >= searchWords.length)
break;
if (word.getText().equals(searchWords[curWord]) == false)
continue;
Raster img = originalImg.getData(ConvertRect(word.getArea()));
outputImg.setsData(img);
curWord++;
}
DisplayImage(outputImg);
}
}
I needed to search each word and extract the area corresponding to the image’s coordinates. OCR Xpress provided a getArea() function that did just that. I had the area of the word I was searching. I needed to convert the RECTs, get the pixel data, and then set the data to the outputImg file before moving to the next word.
我需要搜索每个单词并提取与图像坐标对应的区域。OCR Xpress提供了一个getArea()功能就是这样。我有我正在搜索的单词的区域。我需要转换RECT,获取像素数据,然后outputImg在移动到下一个单词之前将数据设置为文件。