Android:如何在不拍照的情况下从相机捕获文本? [英] Android : How to capture text from camera without taking picture?

查看:90
本文介绍了Android:如何在不拍照的情况下从相机捕获文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用tess-two(在android和eclipse中)捕获用相机显示的文本和数字而不用拍照. 我不想保存图像文件.

I want to capture texts and numbers that showing with camera without taking picture using tess-two(in android and eclipse). I dont want to save image file.

类似的东西(它正在相机上实时捕获):

something like this (it is capturing live on camera):

我使用过tess-two,但是我必须先拍照然后捕获文本. (使用链接: https://stackoverflow.com/questions/19533273 /best-ocr-optical-character-recognition-example-in-android )

I have used tess-two , but i have to take picture first and then capture text. (using link : https://stackoverflow.com/questions/19533273/best-ocr-optical-character-recognition-example-in-android)

并且我已经使用了此( https://www.codeproject.com /Articles/840623/Android-Character-Recognition )来创建类似我上传的图片的行为,但它也应该拍摄图片.

and I have used this (https://www.codeproject.com/Articles/840623/Android-Character-Recognition) to create behaviour like picture I have uploaded but it should take picture too.

那我该怎么实现呢?

推荐答案

/* this is the code of main activity it will capture text without taking picture all you need to do is to make a text field in mainActivity.xml */
    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

> Blockquote

    import android.util.Log;
    import android.util.SparseArray;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.widget.TextView;

    import com.google.android.gms.vision.CameraSource;
    import com.google.android.gms.vision.Detector;
    import com.google.android.gms.vision.text.TextBlock;
    import com.google.android.gms.vision.text.TextRecognizer;

    import java.io.IOException;

    public class MainActivity extends AppCompatActivity {

        SurfaceView cameraView;
        TextView textView;
        CameraSource cameraSource;
        final int RequestCameraPermissionID = 1001;

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode)
            {
                case RequestCameraPermissionID:
                {
                    if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
                    {
                        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }
                        try {
                            cameraSource.start(cameraView.getHolder());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            cameraView = (SurfaceView) findViewById(R.id.surface_view);
            textView = (TextView) findViewById(R.id.text_view);

            TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
            if (!textRecognizer.isOperational()) {
                Log.w("MainActivity", "Detector dependencies are not yet available");
            } else {
                cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
                        .setFacing(CameraSource.CAMERA_FACING_BACK)
                        .setRequestedPreviewSize(1280, 1024)
                        .setRequestedFps(2.0f)
                        .setAutoFocusEnabled(true)
                        .build();
                cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
                    @Override
                    public void surfaceCreated(SurfaceHolder holder) {

                        try {
                            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                               ActivityCompat.requestPermissions(MainActivity.this,
                                       new String[]{Manifest.permission.CAMERA},
                                       RequestCameraPermissionID);
                                return;
                            }
                            cameraSource.start(cameraView.getHolder());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                    }

                    @Override
                    public void surfaceDestroyed(SurfaceHolder holder) {
                        cameraSource.stop();
                    }
                });

                textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
                    @Override
                    public void release() {

                    }

                    @Override
                    public void receiveDetections(Detector.Detections<TextBlock> detections) {
                        final SparseArray<TextBlock> items = detections.getDetectedItems();
                        if(items.size()!=0){
                            textView.post(new Runnable() {
                                @Override
                                public void run() {
                                    StringBuilder stringBuilder=new StringBuilder();
                                    for(int i=0;i<items.size();i++){
                                        TextBlock item = items.valueAt(i);
                                        stringBuilder.append(item.getValue());
                                        stringBuilder.append("\n");
                                    }
                                    textView.setText(stringBuilder.toString());
                                }
                            });
                        }
                    }
                });
            }
        }
    }

这篇关于Android:如何在不拍照的情况下从相机捕获文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆