android ocr――身份z识别的功能实现

android ocr――身份z识别的功能实现,第1张

概述ocrOpenCV想必做过程图像识别的同学们都对这两个词不陌生吧。ocr(opticalcharacterrecognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上的字符,通过检测暗,亮的模式确定其形状,然后用字符

ocr OpenCV 想必做过程图像识别的同学们都对这两个词不陌生吧。

ocr (optical character recognition ,光学字符识别) 是指电子设备(例如扫描仪或数码相机)检查纸上的字符,通过检测暗,亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。 这样就给我编程提供了接口,我们可以识别图片的文字了 (有些文档我们通过手机拍照的,直接生成word )身份z识别,yhk识别等。

opencv 是什么呢

OpenCV的全称是:Open Source Computer Vision library。OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在linux、windows和Mac OS *** 作系统上。它轻量级而且高效――由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

上面是 百度百科给出的定义说白了就是给我们编程提供的类库而已

AndroID 如果想使用OCR 

我们可以使用Google 开源的项目tesseract-ocr

github 下载地址:https://github.com/justin/tesseract-ocr

今天我不讲如何编译 ocr 这个东西 

主要说下,识别二维码的这个项目和tesseract-ocr 整合成一个识别身份z号码的 过程

后面我会把他们编译成类库供大家使用的

ORC 识别方法已经封装成一个简单的类 OCR

package com.dynamsoft.tessocr;  import androID.content.Context; import androID.content.res.AssetManager; import androID.graphics.Bitmap; import androID.os.Environment;  import com.Googlecode.tesseract.androID.TessBaseAPI;  import java.io.file;  /**  * Created by CYL on 2016/3/26.  * email:[email protected]  * 这个类 就是调用 ocr 的接口  * 这个是识别过程是耗时的  *** 作 请放到线程  *** 作  */ public class OCR {   private TessBaseAPI mTess;   private boolean flag;   private Context context;   private AssetManager assetManager;    public OCR() {     // Todo auto-generated constructor stub      mTess = new TessBaseAPI();     String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";     String language = "eng";     //请将你的语言包放到这里 sd 的 tessseract 下的tessdata 下     file dir = new file(datapath + "tessdata/");     if (!dir.exists())       dir.mkdirs();     flag = mTess.init(datapath,language);   }    /**    * 识别出来bitmap 上的文字    * @param bitmap 需要识别的图片    * @return    */   public String getoCRResult(Bitmap bitmap) {     String result = "dismiss langues";     if(flag){       mTess.setimage(bitmap);       result = mTess.getUTF8Text();     }      return result;   }    public voID onDestroy() {     if (mTess != null)       mTess.end();   } } 

方法很简单 :

创建对象,调用getocrResult方法就行了,注意这个识别过程是耗时,放到线程去 *** 作。避免ANR问题

然后我们需要把识别集成到二维码扫描里面 

下面这个对二维码扫描这个项目介绍的比较详细

https://www.oudahe.com/p/27399/

下面给大家介绍一下,ZXing库里面主要的类以及这些类的作用:

CaptureActivity。这个是启动Activity 也就是扫描器。 CaptureActivityHandler 解码处理类,负责调用另外的线程进行解码。 DecodeThread 解码的线程。 com.Google.zxing.clIEnt.androID.camera 包,摄像头控制包。 VIEwfinderVIEw 自定义的VIEw,就是我们看见的拍摄时中间的框框了。

 我可以简单考虑一下  图片识别,我们需要先获取图片才能识别,当识别成功以后应该将数据返回  并反馈给用户我们已经完成了识别。

第一首先 我们如何获取图像 即 bitmap 从上面主要功能的类可以看出来。

我应该去captureactivityhandler 解码处理处理中去找,不管识别二维码还是图片,身份z啊。最终都是识别bitmap

所以我们这里可以找到相机捕捉到的图像;

DecodeHandler

 /*  * copyright (C) 2010 ZXing authors  *  * licensed under the Apache license,Version 2.0 (the "license");  * you may not use this file except in compliance with the license.  * You may obtain a copy of the license at  *  *   http://www.apache.org/licenses/liCENSE-2.0  *  * Unless required by applicable law or agreed to in writing,software  * distributed under the license is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implIEd.  * See the license for the specific language governing permissions and  * limitations under the license.  */  package com.sj.app.deCoding;  import androID.graphics.Bitmap; import androID.os.Bundle; import androID.os.Handler; import androID.os.Looper; import androID.os.Message; import androID.util.Log;  import com.dynamsoft.tessocr.OCR; import com.Google.zxing.BinaryBitmap; import com.Google.zxing.DecodeHintType; import com.Google.zxing.MultiFormatReader; import com.Google.zxing.ReaderException; import com.Google.zxing.Result; import com.Google.zxing.common.HybrIDBinarizer; import com.sj.app.camera.CameraManager; import com.sj.app.camera.PlanarYUVluminanceSource; import com.sj.app.utils.IDMatch; import com.sj.erweima.MipcaActivityCapture; import com.sj.erweima.R;  import java.util.Hashtable; import java.util.List;  final class DecodeHandler extends Handler {    private static final String TAG = DecodeHandler.class.getSimplename();    private final MipcaActivityCapture activity;   private final MultiFormatReader multiFormatReader;    DecodeHandler(MipcaActivityCapture activity,Hashtable<DecodeHintType,Object> hints) {     multiFormatReader = new MultiFormatReader();     multiFormatReader.setHints(hints);     this.activity = activity;   }    @OverrIDe   public voID handleMessage(Message message) {     switch (message.what) {       case R.ID.decode:         // Log.d(TAG,"Got decode message");         decode((byte[]) message.obj,message.arg1,message.arg2);         break;       case R.ID.quit:         Looper.myLooper().quit();         break;     }   }    /**    * Decode the data within the vIEwfinder rectangle,and time how long it    * took. For efficIEncy,reuse the same reader objects from one decode to    * the next.    *    * @param data    *      The YUV prevIEw frame.    * @param wIDth    *      The wIDth of the prevIEw frame.    * @param height    *      The height of the prevIEw frame.    */   private voID decode(byte[] data,int wIDth,int height) {     long start = System.currentTimeMillis();     Result rawResult = null;      // modify here     byte[] rotatedData = new byte[data.length];     for (int y = 0; y < height; y++) {       for (int x = 0; x < wIDth; x++)         rotatedData[x * height + height - y - 1] = data[x + y * wIDth];     }     int tmp = wIDth; // Here we are swapPing,that's the difference to #11     wIDth = height;     height = tmp;      PlanarYUVluminanceSource source = CameraManager.get()         .buildluminanceSource(rotatedData,wIDth,height);     BinaryBitmap bitmap = new BinaryBitmap(new HybrIDBinarizer(source));     try {       //相机中捕捉到的       Bitmap image = source.renderCroppedGreyscaleBitmap();       doorc(source);       rawResult = multiFormatReader.decodeWithState(bitmap);     } catch (ReaderException re) {       // continue     } finally {       multiFormatReader.reset();     }      if (rawResult != null) {       long end = System.currentTimeMillis();       Log.d(TAG,"Found barcode (" + (end - start) + " ms):\n"           + rawResult.toString());       Message message = Message.obtain(activity.getHandler(),R.ID.decode_succeeded,rawResult);       Bundle bundle = new Bundle();       bundle.putParcelable(DecodeThread.barCODE_BITMAP,source.renderCroppedGreyscaleBitmap());       message.setData(bundle);       // Log.d(TAG,"Sending decode succeeded message...");       message.sendToTarget();     } else {       Message message = Message.obtain(activity.getHandler(),R.ID.decode_Failed);       message.sendToTarget();     }   }   private Handler handler = new Handler(){     public voID handleMessage(Message msg) {       CardID cardID = (CardID) msg.obj;       if(cardID != null){         Message message = Message.obtain(activity.getHandler(),cardID.ID);         Bundle bundle = new Bundle();         bundle.putParcelable(DecodeThread.barCODE_BITMAP,cardID.bitmap);         message.setData(bundle);         // Log.d(TAG,"Sending decode succeeded message...");         message.sendToTarget();       }     };   };   private voID doorc(final PlanarYUVluminanceSource source) {     new Thread(new Runnable() {       @OverrIDe       public voID run() {         Bitmap bitmap = source.renderCroppedGreyscaleBitmap();         String ID = new OCR().getoCRResult(bitmap);         if(ID != null){           List<String> List = IDMatch.machID(ID);           if(List!= null && List.size()>0){             String cardID = List.get(0);             if(cardID != null){               Message msg = Message.obtain();               CardID cardID2 = new CardID(cardID,bitmap);               msg.obj = cardID2;               handler.sendMessage(msg);             }           }         }        }     }).start();   }   public class CardID{     private String ID;     private Bitmap bitmap;     public CardID(String ID,Bitmap bitmap) {       super();       this.ID = ID;       this.bitmap = bitmap;     }     public String getID() {       return ID;     }     public voID setID(String ID) {       this.ID = ID;     }     public Bitmap getBitmap() {       return bitmap;     }     public voID setBitmap(Bitmap bitmap) {       this.bitmap = bitmap;     }    }  } 

当解析成功的时候就将结果通过handler 返回到UI 线程中去了,对于 扫描框我们可以响应调节。

CameraManager 这个类 控制扫描框的大小。

public Rect getFramingRect() {   Point screenResolution = configManager.getScreenResolution();   if (framingRect == null) {    if (camera == null) {     return null;    }    int wIDth = screenResolution.x * 7 / 8;    if (wIDth < MIN_FRAME_WIDTH) {     wIDth = MIN_FRAME_WIDTH;    } else if (wIDth > MAX_FRAME_WIDTH) { //    wIDth = MAX_FRAME_WIDTH;    }    int height = screenResolution.y * 3 / 4;    if (height < MIN_FRAME_HEIGHT) {     height = MIN_FRAME_HEIGHT;    } else if (height > MAX_FRAME_HEIGHT) {     height = MAX_FRAME_HEIGHT;    }    int leftOffset = (screenResolution.x - wIDth) / 2;    int topOffset = (screenResolution.y - height) / 2;    framingRect = new Rect(leftOffset,topOffset,leftOffset + wIDth,topOffset + height);    Log.d(TAG,"Calculated framing rect: " + framingRect);   }   return framingRect;  } 

改变这个方法就可以改变这个扫描框的大小了。

需要提示的是 如果您的手机是androID 6.0以上 请查看 sd卡根目录是否存在tesseract/tessdata目录 以及下面的文件  如果没有存在说明 应用没有获取到存储权限。

原文链接:http://blog.csdn.net/tiandiyinghun/article/details/50985961

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android ocr――身份z识别的功能实现全部内容,希望文章能够帮你解决android ocr――身份z识别的功能实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147678.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存