如何在Google Vision Camera Source上设置矩形对焦区域?

时间:2018-03-11 08:10:49

标签: java android

我目前在使用谷歌愿景时遇到了问题。实际上图书馆没有任何问题,它的作品很棒。我现在想要完成的所有事情都设置了一个矩形区域,CameraSource将只关注它。目的是尝试仅捕获特定矩形框内的文本。我在StackOverflow上尝试了很多例子,但它们似乎都没有用。我目前使用Google Vision(11.8.0)的工作代码是

cameraView = findViewById(R.id.surfaceview);
        output = findViewById(R.id.output);

        TextRecognizer textRecognizer = new TextRecognizer.Builder(ScanVoucher.this).build();

        if(!textRecognizer.isOperational()) {
            show_alert("Text Recognition not supported on this device");
        } else {

            cameraSource = new CameraSource.Builder(ScanVoucher.this, textRecognizer)
                    .setAutoFocusEnabled(true)
                    .setFacing(CameraSource.CAMERA_FACING_BACK)
                    .setRequestedFps(2.0f)
                    .setRequestedPreviewSize(300, 300)
                    .build();

            cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder surfaceHolder) {
                    try {
                        cameraSource.start(cameraView.getHolder());
                    } catch (IOException e) {
                        show_alert("Unable to access camera");
                    } catch (SecurityException e) {
                        finish();
                    } catch(Exception e) {
                        //Kill
                        finish();
                    }
                }

                @Override
                public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                    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) {
                        output.post(new Runnable() {
                            @Override
                            public void run() {
                                StringBuilder builder = new StringBuilder();

                                for(int i = 0; i < items.size(); i++) {
                                    TextBlock item = items.valueAt(i);
                                    builder.append(item.getValue());
                                }

                                try {
                                    output.setText(builder.toString());
                                } catch (Exception e) {
                                    output.setText(e.getMessage());
                                }

                            }
                        });
                    }
                }
            });
        }

正如我所说,一切都很好。想知道如何将矩形框设置为预览区域,以便相机仅使用盒子捕获文本,就像QR或条形码扫描仪应用程序一样。提前谢谢。

1 个答案:

答案 0 :(得分:0)

请不要放弃项目。.您可以重置.setRequestedPreviewSize(80,24)以使其适合。 对我来说,这仅捕获第一行文本。我希望你看到这个