如何文本识别仅检测重叠式摄像机内部而不是整个摄像机尺寸

时间:2019-07-26 14:09:30

标签: java android ocr text-recognition

我想制作可以识别文本的ocr应用程序,但我只想仅在矩形内检测文本。

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_transaksi, container, false);

        cameraView = view.findViewById(R.id.surface_view);
        txtView = view.findViewById(R.id.txtview);
        mButton = view.findViewById(R.id.button1);
        mButton.setOnClickListener(this);

        txtRecognizer = new TextRecognizer.Builder(listener.getApplicationContext()).build();
        detector = new FaceDetector.Builder(listener.getApplicationContext()).build();

        cameraSource = new CameraSource.Builder(listener.getApplicationContext(), detector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(360 , 640)
                .setRequestedFps(2.0f)
                .setAutoFocusEnabled(true)
                .build();
        cameraView.getHolder().addCallback(this);


        holder = cameraView.getHolder();

        holder.addCallback((SurfaceHolder.Callback) this);


        cameraView.setSecure(true);


        transparentView = view.findViewById(R.id.transparent_view);

        holderTransparent = transparentView.getHolder();

        holderTransparent.addCallback((SurfaceHolder.Callback) this);
        holderTransparent.setFormat(PixelFormat.TRANSLUCENT);
        transparentView.setZOrderMediaOverlay(true);


        deviceWidth=getScreenWidth();
        deviceHeight=getScreenHeight();

        return view;
    }

    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }

    private void Draw() {
        Canvas canvas = holderTransparent.lockCanvas(null);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(3);

        RectLeft = 40;
        RectTop = 200 ;
        RectRight = RectLeft+ deviceWidth-100;
        RectBottom =RectTop+ 200;
        Rect rec=new Rect((int) RectLeft,(int)RectTop,(int)RectRight,(int)RectBottom);

        canvas.drawRect(rec,paint);
        holderTransparent.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (ActivityCompat.checkSelfPermission(listener,
                    Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                  ActivityCompat.requestPermissions(listener, new String[]{Manifest.permission.CAMERA},1);

                return;
            }

            Draw();
            cameraSource.start(cameraView.getHolder());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

0 个答案:

没有答案