blinkinput / blinkinput-android

BlinkOCR SDK for Android
https://microblink.com/ocr
104 stars 29 forks source link

Get Quadrilateral size and position #6

Closed rachelfong closed 8 years ago

rachelfong commented 8 years ago

Hi,

I implemented a custom Activity and embed a RecognizerView in a FrameLayout.

However, the end result I want to achieve is to have a static Quadrilateral in the middle of the screen (using QuadViewPreset.DEFAULT_FROM_SCAN_CARD_ACTIVITY), where the area outside or surrounding the Quad is blurred/darkened and the area within the Quad is fully transparent, showing the actual display from RecognizerView.

So far I have managed to overlay a view on top of RecognizerView, all that is left is for me to "cut the quadrilateral hole" in the middle of my overlay view, however, I am not able to get any information on the presenting Quad.

Is there any API which provides a way of getting the Quad size and position for me to setup my overlay view? Or is there a way for me to achieve my end result without having to use overlays?

i1E commented 8 years ago

Hi @rachelfong,

with provided QuadViewManager it is not possible to achieve what you want. You can not get the quad size nor the quad position. You should create your own overlay view with the desired behaviour. If you want that scanning is not active outside of the shown scanning window, you should set the same scanning window on the RecognizerView (make sure that it is identical to the scan window that is shown to the user) by using method setScanningRegion.

As an example, you can use BlinkOCRSegmentDemo demo app and see how the scan overlay is defined in that app.

rachelfong commented 8 years ago

Hi @i1E ,

Thank you for pointing me to the right direction. I took a quick look at the demo app mentioned and managed to implement the overlay view on top of RecognizerView. The red/green edges which tells the user that it is a successful scan are no longer implemented? Also, can I keep changing the scanning region during runtime to accommodate orientation changes? Would there be any risks in doing so (memory leaks, concurrency/threading issues)?

i1E commented 8 years ago

Hi @rachelfong,

adding your custom overlay over the RecognizerVIew does not prevent you to add QuadView on top of it. You can take a look at the IDScanActivity from BlinkOCRDetectorDemo app and see how built in QuadViewManager is used to display successful detections. You can use OcrResultCharsView or OcrResultDotsView to indicate successfully recognized characters over the camera overlay (there is example for this in mentioned activity).

You can change the scanning region during runtime without worrying about memory leaks or concurrency issues, but if you want to change the scanning region only to accommodate orientation changes, you can achieve this with setScanningRegion method (pass true as the second argument to define that scanning region should be rotated with device).

rachelfong commented 8 years ago

Hi @i1E ,

I am unable to get the static QuadView to be displayed on my blurred overlay and have it to be transparent, showing the camera preview. So I opted to follow the CustomSegment way of doing things which is to build segments around a view which is transparent to get the user to position their documents within the frame. However, like I mentioned, one of the losses would be not being able to get the red/green edges. My project does not need the OcrResultsCharsView on top of the RecognizerView so I left that out.

I tried out the setScanningRegion method, but as most smart phones go, the ratio of the view I built for Portrait vastly differs from the Landscape.

This is what I was expecting, blue region is supposed to be blurred, red region supposed to be fully transparent to show the clear camera preview and get the user to position their document within this frame:

expected results

What I got was this upon rotating:

actual results

So I have no choice but to build two different overlays for each orientation to achieve what I needed, overlap them in my activity layout and hide one of them based on current orientation, hence my attempts to change the scanning region repeatedly, but apparent setScanningRegion does not allow repeated initializations.

I will keep trying to achieve it for a few more hours, otherwise, I will have no choice but to give up on Landscape and just force Portrait on the Activity.

Update: Apologies on the many questions on this. Let me rephrase my matter into a summary. Basically what I need is the functionality of the demo app you mentioned in your first answer, but with landscape orientation (without distorting the fully transparent frame, like the QuadViewPreset.DEFAULT_FROM_SCAN_CARD_ACTIVITY on another demo) and red/green edges of the viewfinder supported. Would it be possible?

DoDoENT commented 8 years ago

What you got is actually expected, because scanning region is defined with relative rectangle, so same percentages are used in both portrait and landscape. If you want to have different scanning regions in portrait and landscape then proceed as follows:

Note that if you allow sensor orientation changes for your activity in AndroidManifest.xml you cannot have smooth animated transition in UI from portrait rectangle to landscape rectangle (as is demonstrated with builtin QuadViewPreset.DEFAULT_FROM_SCAN_CARD_ACTIVITY) due to activity restarts that happen when activity configuration changes.

However, if you use onConfigurationChange callback to update your views when activity changes configuration from portrait to landscape and vice versa, first make sure that you call changeConfiguraion on RecognizerView and also update its scanning region in same callback.

rachelfong commented 8 years ago

Hi @i1E ,

Thanks for the advice. Followed your instructions and it seems to work towards what I needed. Thank you very much for this.

DoDoENT commented 8 years ago

If that has solved your problem, please close this issue.

rachelfong commented 8 years ago

Thank you very much. I shall close it now.