文字识别应用

时间:2017-11-14 23:14:10

标签: computer-vision ocr vision android-vision text-recognition

现在我正在尝试制作可识别文字的相机应用程序。为此,我使用了信息from guides by google。本网站介绍如何制作全屏阅读器。但我需要将Mobile Vision文本扫描仪设置为小矩形(如图片所示)。Screenshot of barcode reader app(i need the same solution for text). Please help me).

required result

1 个答案:

答案 0 :(得分:1)

我通过整合"过滤器"来实现此功能。在探测器前面拍摄图像(在我的例子中是来自图像中心的一行文字)。请看下面的代码:

public class LineDetector extends Detector { 
    Detector mDelegate;

    public LineDetector(Detector delegate) {
        mDelegate = delegate;
    }

    @Override
    public SparseArray detect(Frame frame) {
        int width = frame.getMetadata().getWidth();
        int height = frame.getMetadata().getHeight();

        int mBoxHeight = height;
        int mBoxWidth = Math.toIntExact(Math.round(mBoxHeight * ConstantsPool.CROP_BOX_ASPECT_RATIO));

        int right = (width / 2) + (mBoxWidth / 2);
        int left = (width / 2) - (mBoxWidth / 2);
        int bottom = height;
        int top = 0;

        YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(left, top, right, bottom), 100, byteArrayOutputStream);
        byte[] jpegArray = byteArrayOutputStream.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);

        Frame croppedFrame = new Frame.Builder()
            .setBitmap(bitmap)
            .setRotation(frame.getMetadata().getRotation())
            .build();
        return mDelegate.detect(croppedFrame);
    }

    @Override
    public boolean isOperational() {
        return mDelegate.isOperational();
    }

    @Override
    public boolean setFocus(int id) {
        return mDelegate.setFocus(id);
    }
}

然后您可以通过以下方式使用它:

TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
LineDetector lineDetector = new LineDetector(textRecognizer);
lineDetector.setProcessor(...);
...
Camera2Source camera2Source = new Camera2Source.Builder(getContext(), lineDetector).build();

如果您有任何疑问,请询问

汤姆