光学字符识别 (英语: O ptical C haracter R ecognition, OCR
)是指对文本资料的图像文件进行分析识别处理,获取文字及版面信息的过程。
然后将训练好的eng.traineddata放入android项目的assets文件夹中,就可以识别英文了。
- compile 'com.rmtheis:tess-two:8.0.0'
取景框.JPG 拍完照后,调用startOCR方法。
- private void prepareTesseract() {
- try {
- prepareDirectory(DATA_PATH + TESSDATA);
- } catch(Exception e) {
- e.printStackTrace();
- }
- copyTessDataFiles(TESSDATA);
- }
- /**
- * Prepare directory on external storage
- *
- * @param path
- * @throws Exception
- */
- private void prepareDirectory(String path) {
- File dir = new File(path);
- if (!dir.exists()) {
- if (!dir.mkdirs()) {
- Log.e(TAG, "ERROR: Creation of directory " + path + " failed, check does Android Manifest have permission to write to external storage.");
- }
- } else {
- Log.i(TAG, "Created directory " + path);
- }
- }
- /**
- * Copy tessdata files (located on assets/tessdata) to destination directory
- *
- * @param path - name of directory with .traineddata files
- */
- private void copyTessDataFiles(String path) {
- try {
- String fileList[] = getAssets().list(path);
- for (String fileName: fileList) {
- // open file within the assets folder
- // if it is not already there copy it to the sdcard
- String pathToDataFile = DATA_PATH + path + "/" + fileName;
- if (! (new File(pathToDataFile)).exists()) {
- InputStream in =getAssets().open(path + "/" + fileName);
- OutputStream out = new FileOutputStream(pathToDataFile);
- // Transfer bytes from in to out
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- } in .close();
- out.close();
- Log.d(TAG, "Copied " + fileName + "to tessdata");
- }
- }
- } catch(IOException e) {
- Log.e(TAG, "Unable to copy files to tessdata " + e.toString());
- }
- }
extractText()会调用tess-two的api来实现ocr文字识别。
- private void startOCR(Uri imgUri) {
- try {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 4; // 1 - means max size. 4 - means maxsize/4 size. Don't use value <4, because you need more memory in the heap to store your data.
- Bitmap bitmap = BitmapFactory.decodeFile(imgUri.getPath(), options);
- String result = extractText(bitmap);
- resultView.setText(result);
- } catch(Exception e) {
- Log.e(TAG, e.getMessage());
- }
- }
最后,显示识别的效果,此时的效果还算可以。 简单地识别英文.JPG
- private String extractText(Bitmap bitmap) {
- try {
- tessBaseApi = new TessBaseAPI();
- } catch(Exception e) {
- Log.e(TAG, e.getMessage());
- if (tessBaseApi == null) {
- Log.e(TAG, "TessBaseAPI is null. TessFactory not returning tess object.");
- }
- }
- tessBaseApi.init(DATA_PATH, lang);
- tessBaseApi.setImage(bitmap);
- String extractedText = "empty result";
- try {
- extractedText = tessBaseApi.getUTF8Text();
- } catch(Exception e) {
- Log.e(TAG, "Error in recognizing text.");
- }
- tessBaseApi.end();
- return extractedText;
- }
在这里,使用cv4j来实现图像的二值化处理。
- private void startOCR(Uri imgUri) {
- try {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 4; // 1 - means max size. 4 - means maxsize/4 size. Don't use value <4, because you need more memory in the heap to store your data.
- Bitmap bitmap = BitmapFactory.decodeFile(imgUri.getPath(), options);
- CV4JImage cv4JImage = new CV4JImage(bitmap);
- Threshold threshold = new Threshold();
- threshold.adaptiveThresh((ByteProcessor)(cv4JImage.convert2Gray().getProcessor()), Threshold.ADAPTIVE_C_MEANS_THRESH, 12, 30, Threshold.METHOD_THRESH_BINARY);
- Bitmap newBitmap = cv4JImage.getProcessor().getImage().toBitmap(Bitmap.Config.ARGB_8888);
- ivImage2.setImageBitmap(newBitmap);
- String result = extractText(newBitmap);
- resultView.setText(result);
- } catch(Exception e) {
- Log.e(TAG, e.getMessage());
- }
- }
图像二值化就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果。图像的二值化有利于图像的进一步处理,使图像变得简单,而且数据量减小,能凸显出感兴趣的目标的轮廓。 cv4j的github地址:https://github.com/imageprocessor/cv4j
- CV4JImage cv4JImage = new CV4JImage(bitmap);
- Threshold threshold = new Threshold();
- threshold.adaptiveThresh((ByteProcessor)(cv4JImage.convert2Gray().getProcessor()), Threshold.ADAPTIVE_C_MEANS_THRESH, 12, 30, Threshold.METHOD_THRESH_BINARY);
- Bitmap newBitmap = cv4JImage.getProcessor().getImage().toBitmap(Bitmap.Config.ARGB_8888);
cv4j再来试试效果,图片中间部分是二值化后的效果,此时基本能识别出代码的内容。 先做二值化再识别代码.JPG
是gloomyfish和我一起开发的图像处理库,纯java实现。
前面的例子都是识别英文的,所以原先的lang值为"eng",现在要识别简体中文的话需要将其值改为"chi_sim"。 识别中文.JPG
- tessBaseApi.init(DATA_PATH, lang);
来源: http://suo.im/1WJ9O2