b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

android ocr——识别的功能实现

电脑杂谈  发布时间:2019-09-01 11:01:54  来源:网络整理

validform验证号_asp 号验证_android+验证号码

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

ocr (optical character recognition ,光学字符识别) 是指电子仪器(例如扫描仪或数码相机)检查纸上的字符android+验证号码,通过检测暗,亮的方式确认其颜色,然后用字符识别方法将颜色翻译成计算机文字的过程。 这样就给我编程提供了接口,我们可以识别照片的文字了 (有些文档我们借助电脑拍照的android+验证号码,直接生成word )识别,银行卡识别等。

opencv 是哪个呢

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

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

Android 如果想使用OCR

android+验证号码_asp 号验证_validform验证号

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

github 下载地址:

今天我不讲怎么编译 ocr 这个东西

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

后面我会把它们编译成类库供你们使用的

ORC 识别方式早已封装成一个简单的类 OCR

validform验证号_asp 号验证_android+验证号码

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:670654904@qq.com 
 * 这个类 就是调用 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问题

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

下面这个对二维码扫描这个项目介绍的非常具体

//www.jb51.net/article/53487.htm

asp 号验证_validform验证号_android+验证号码

下面给你们介绍一下,ZXing库上面主要的类或者某些类的作用:

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

第一首先 我们怎么获取图像 即 bitmap 从里面主要功能的类可以看起来。

我需要去captureactivityhandler 解码处理处理中去找,不管识别二维码还是照片,啊。最终都是识别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(), 
            R.id.decode_succeeded, 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://www.pc-fly.com/a/jisuanjixue/article-121312-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...