美文网首页
使用Tesseract识别图片中的股票代码

使用Tesseract识别图片中的股票代码

作者: KUN叔 | 来源:发表于2017-08-20 15:54 被阅读342次

    概述

    Tesseract是一个OCR(Optical Character Recognition,光学字符识别)引擎,在这里我用来开发Android上能识别一张图片上的股票代码APP功能。

    Github地址

    https://github.com/tesseract-ocr/tesseract

    这个库非常庞大,反正我是看不出怎么使用在Android开发上,于是我找了另一个库,https://github.com/rmtheis/tess-two ,应该是基于前面的库制作的。

    添加依赖

    dependencies {
        compile 'com.rmtheis:tess-two:8.0.0'
    }
    

    布局

    布局非常简单,只有右上角一个导入按钮:


    布局

    xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
    
    <!--显示识别结果-->
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <!--识别过程中的进度条-->
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:visibility="gone"
            android:layout_centerInParent="true"/>
    <!--显示识别图片前处理过后的图片-->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/imageView" />
    </RelativeLayout>
    

    导入识别库

    先去这里下载识别库,少了这个识别库没有使用的,而且不同的识别库识别准确率也是不一样的,当你发现准确率低是可以尝试换一个识别库或许会改善,里面有很多语言的识别库,其他语言的不需要关心,我们只需要记住开头chi_sim的是简体中文,chi_tra是繁体中文,eng是英语,eus应该是美式英语。我使用的是eus.traineddata。
    先在项目里新建assert目录-tessdata目录-eus.traineddata。

    image.png

    可以编写代码了

    直接看代码

    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = MainActivity.class.getSimpleName();
        private static final int REQUEST_PICK_PHOTO = 1;
        private TessBaseAPI tessBaseAPI;
        private static final String lang = "eus";//识别库
        //private static final String lang = "chi_sim";
        private static final String DATA_PATH =     Environment.getExternalStorageDirectory().toString() + "/Tesseract/";
        private static final String TESSDATA = "tessdata";
        String result = "empty";
        private TextView text;
        private ProgressBar progressBar;
        private ImageView imageView;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            text = (TextView) findViewById(R.id.text);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            imageView = (ImageView) findViewById(R.id.imageView);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.dao_ru) {
                //打开图库选择图片
                pickPhoto();
            }
            return super.onOptionsItemSelected(item);
        }
    
        private void pickPhoto() {
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, REQUEST_PICK_PHOTO);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode,resultCode,data);
            if (requestCode == REQUEST_PICK_PHOTO && resultCode == RESULT_OK) {
                //首先需要把assert目录中的识别库拷贝到手机中
                prepareTesseract();
                Uri uri = data.getData();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                Bitmap bitmap = BitmapFactory.decodeFile(getRealImageFilePath(this,uri));
                //把图片处理成黑白的,有利于识别
                bitmap = toHeibai(bitmap);
                //识别耗时,放在异步处理
                new MyAsyckTask().execute( bitmap);
            }
        }
    
    
        public static String getRealImageFilePath( Context context,Uri uri) {
            if( uri == null ) {
                return null;
            }
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
            if (cursor!=null){
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String yourRealPath = cursor.getString(columnIndex);
                    return yourRealPath;
                }
            cursor.close();
            }
            return uri.getPath();
        }
        //在手机中新建目录
        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);
            }
        }
        
        private void prepareTesseract() {
            try {
                prepareDirectory(DATA_PATH + TESSDATA);
            } catch (Exception e) {
                e.printStackTrace();
            }
            copyTessDataFiles(TESSDATA);
        }
        //拷贝识别库到手机
        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());
            }
        }
    
        //真正从图片提取内容的方法
        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);
    
    //       //EXTRA SETTINGS 提取设置
    //        //For example if we only want to detect numbers    白名单
            tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_");
            //tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "0123456789");
    //
    //        //blackList Example   黑名单
    //        tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, "!@#$%^&*()_+=-qwertyuiop[]}{POIU" +
    //                "YTRWQasdASDfghFGHjklJKLl;L:'\"\\|~`xcvXCVbnmBNM,./<>?");
    
            Log.d(TAG, "Training file loaded");
            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;
        }
    
        //提取图片内容采用异步执行
        private class MyAsyckTask extends AsyncTask<Bitmap,Void,String>{
    
            @Override
            protected void onPreExecute() {
                progressBar.setVisibility(View.VISIBLE);
                super.onPreExecute();
            }
    
            @Override
            protected String doInBackground(final Bitmap... params) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(params[0]);
                    }
                });
                return extractText(params[0]);
            }
    
            @Override
            protected void onPostExecute(String s) {
                progressBar.setVisibility(View.GONE);
    //            String pattern = "\\d{5,6}\\b|\\b[A-Z_]+\\b";//正则表达式过滤
                String pattern = "\\d{5,6}\\b";//正则表达式过滤
                Pattern p = Pattern.compile(pattern);
                Matcher m = p.matcher(s);
                StringBuilder formatStringBuilder = new StringBuilder();
                while (m.find()) {
                    formatStringBuilder.append(m.group()).append("\n");
    //                Log.i(TAG,"formatStringBuilder---------"+formatStringBuilder.toString());
                }
    
                text.setText(formatStringBuilder);
            }
        }
    
        //转换成黑白照片,更利于识别图片
        public static Bitmap toHeibai(Bitmap mBitmap) {
            int mBitmapWidth = 0;
            int mBitmapHeight = 0;
            //截取图片宽度的3分之一
            mBitmapWidth = mBitmap.getWidth() / 3;
            mBitmapHeight = mBitmap.getHeight();
            Bitmap bmpReturn = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,
                    Bitmap.Config.ARGB_8888);
            Bitmap resizeBmp;
            int iPixel = 0;
            int wTime = 0;//用于判断是白色背景的图片
            int bTime = 0;//用于判断是黑色背景的图片
            for (int i = 0; i < mBitmapWidth; i++) {
                for (int j = 0; j < mBitmapHeight; j++) {
                    int curr_color = mBitmap.getPixel(i, j);
                    int avg = (Color.red(curr_color) + Color.green(curr_color) + Color
                            .blue(curr_color)) / 3;
                    if (avg >= 190)//修改这个值会影响字体颜色的深浅,这个项目的截图的股票代码字体比较暗,设置成190有利于识别,
                    {
                        iPixel = 255;
                        wTime++;
                    } else if (avg < 190 && avg > 100) {
                        if (wTime > bTime) {//当为白色的背景图片时
                            iPixel = 0;
                        } else {
                            iPixel = 255;
                        }
                    } else {
                        iPixel = 0;
                        bTime++;
                    }
                    int modif_color = Color.argb(255, iPixel, iPixel, iPixel);
    
                    bmpReturn.setPixel(i, j, modif_color);
                }
            }
            if (mBitmap != null) {
                mBitmap.recycle();
                mBitmap = null;
            }
            resizeBmp = ThumbnailUtils.extractThumbnail(bmpReturn, mBitmapWidth, mBitmapHeight);
            return resizeBmp;
        }
    }
    
    

    相信注释已经很明白。来看图(不知道用什么工具制作效果图,有小伙伴知道告诉我一声)


    选择图片 识别结果和处理过后的图片

    会出现一些识别错误的东西,但是没有关系,可以完善正则去匹配,也可以完善功能让用户选择需要的。

    项目地址

    github地址

    相关文章

      网友评论

          本文标题:使用Tesseract识别图片中的股票代码

          本文链接:https://www.haomeiwen.com/subject/trsenttx.html