制作文本扫描仪,但显示黑屏而不是相机

时间:2017-03-18 05:26:49

标签: android camera ocr surfaceview

我正在尝试使用可识别文本和屏幕显示的相机制作文本扫描仪应用程序。但是它不显示相机而是显示黑屏。我该怎么解决这个问题。 我的代码在这里

  package com.myapp.game.easynepalirecharge;

import android.Manifest;
import android.app.ActionBar;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
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 REQUESTCAMERAPERMISSION = 105;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull final int[] grantResults) {
        switch (requestCode) {

            case REQUESTCAMERAPERMISSION:


                              if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                                  if (ActivityCompat.checkSelfPermission(getApplication(), 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.surfaceView);

        textView = (TextView) findViewById(R.id.textView);

        TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

        if (!textRecognizer.isOperational()) {
            Log.v("haha", "error not operational");
        } else {

            cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer).
                    setFacing(CameraSource.CAMERA_FACING_BACK)
                    .setRequestedPreviewSize(3840, 2160)
                    .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) {
                                         // TODO: Consider calling


                                         ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUESTCAMERAPERMISSION);
                                         cameraSource.start(cameraView.getHolder());

                                         return;
                                     }


                                 } catch (Exception 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());


                                                        }
                                                    });


                                                }


                                            }


                                        }


            );


        }

    }
    }

我还想问另一件事。哪一个更好。这个还是tess两个库?

1 个答案:

答案 0 :(得分:0)

你正确地停了相机吗?尝试在线程中执行相机功能,这样您的单个任务就不会很昂贵。我认为问题的原因是在运行时获得您的许可。 试试这个

https://developer.android.com/training/permissions/requesting.html