now i`m trying to make camera app which can recognize text. For this I use the information from guides by google. This site describes how to make a full-screen reader. But i need to set Mobile Vision text scanner active in small rectangle(like in picture).Screenshot of barcode reader app(i need the same solution for text). Please help me).
Asked
Active
Viewed 629 times
1
-
small rectangle(like in picture)- this is unclear. Is that rectangle in the image is controlled by the user through device screen, do we have coordinates information of that rectangle in the image? Please give some more detail in your question. – flamelite Nov 16 '17 at 17:37
-
I`ve added a picture of required result. You can see that in this case application does not try to read all the information, but only that in the rectangle.(the camera is full screen, but the reading works only in that area). – Qwerty Nov 16 '17 at 20:22
1 Answers
1
I have implemented this feature by integrating "filter" in front of the detector which crops the image (in my case a line of text from the center of image). Look at the code below:
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);
}
}
And then you can use it in a following way:
TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
LineDetector lineDetector = new LineDetector(textRecognizer);
lineDetector.setProcessor(...);
...
Camera2Source camera2Source = new Camera2Source.Builder(getContext(), lineDetector).build();
If you have any questions, just ask
Tom

Tomáš Pikous
- 36
- 3