1

我正在设计一个应用程序,我使用相机扫描文本并使用该文本获取更多详细信息。为此,我正在使用 Google 的视觉 API。但默认情况下,API 会读取图像上可用的所有文本,如下所示。

在此处输入图像描述

从上图中您可以看到,该应用程序正在识别相机前可用的所有文本。但我想从相机中扫描“Hello World” 。是否可以使用某种触摸事件来专注于所需的文本

请找到用于文本识别的代码

    private void startCameraSource() {
//Create the TextRecognizer
final TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

    if (!textRecognizer.isOperational()) {
    Log.w(TAG, "Detector dependencies not loaded yet");
} else {

    //Initialize camerasource to use high resolution and set Autofocus on.
    mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedPreviewSize(1280, 1024)
            .setAutoFocusEnabled(true)
            .setRequestedFps(2.0f)
            .build();

    /**
     * Add call back to SurfaceView and check if camera permission is granted.
     * If permission is granted we can start our cameraSource and pass it to surfaceView
     */
    mCameraView.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},
                            requestPermissionID);
                    return;
                }
                mCameraSource.start(mCameraView.getHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

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

    //Set the TextRecognizer's Processor.
    textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
        @Override
        public void release() {
        }

        /**
         * Detect all the text from camera using TextBlock and the values into a stringBuilder
         * which will then be set to the textView.
         * */
        @Override
        public void receiveDetections(Detector.Detections<TextBlock> detections) {
            final SparseArray<TextBlock> items = detections.getDetectedItems();
            if (items.size() != 0 ){

                mTextView.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");
                        }
                        mTextView.setText(stringBuilder.toString());
                    }
                });
            }
        }
    });
}
}
4

0 回答 0