Reputation: 667
I'm using Googles Mobile Vision API to recognize text (numbers) in a static Bitmap. Now I would like to zoom in to the place where the number was found.
So this is how I scan the Bitmap and obtain my x and y coordinates
Point[] p = textBlock.getCornerPoints();
public void Test(Bitmap bitmap) {
Context context = getApplicationContext();
TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
ByteBuffer byteBuffer = frame.getGrayscaleImageData();
if (ocrFrame.isOperational()) {
Log.e(TAG, "Textrecognizer is operational");
}
SparseArray<TextBlock> textBlocks = ocrFrame.detect(frame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
String value = textBlock.getValue();
Point[] p = textBlock.getCornerPoints();
Log.e(TAG, "something is happening");
}
}
Furthermore, I´m using the TouchImageView to display the bitmap. Now I'm calling the setZoom method with my obtained coordinates like this:
touchImageView.setZoom(1F, 210F, 748F, ImageView.ScaleType.CENTER);
But it zooms to the wrong place and I don't really know why. Can anybody give me some tips?
(https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/ortiz/touch/TouchImageView.java)
EDIT: Ok, I figured it out that scale type does something I don't get. The problem here is setZoom, I think. I have to convert the coordinates of the bitmap to the coordinates of the Touchimageview.
EDIT2: Solution: Mistake was to pass the x and y coordinate directly but setZoom take values between 0 and 1
int BitmapHeight = photo.getHeight();
int BitmapWidth = photo.getWidth();
int FoundX = p[0].x;
int FoundY = p[0].y;
float DividerX = BitmapWidth / (float)FoundX;
float DividerY = BitmapHeight / (float)FoundY;
float ZoomX = 1 / (float)DividerX;
float ZoomY = 1 / (float)DividerY;
touchImageView.setZoom(touchImageView.getMaxZoom(), ZoomX, ZoomY, ImageView.ScaleType.CENTER);
Upvotes: 5
Views: 768
Reputation: 330
You can use this google library https://developers.google.com/vision/android/text-overview . You can find example here https://codelabs.developers.google.com/codelabs/mobile-vision-ocr
by adding below in android gradle file
compile 'com.google.android.gms:play-services-vision:15.0.0'
Upvotes: 0