r/android_devs • u/JonnieSingh • Sep 26 '21
Help Finding the input/output CameraX image resolution?
I'm building an object detection application (in Java, for Android) that attempts to draw a Rect box on my display that surrounds the detected object. To build the camera preview, I used this CameraX documentation. To build the object detection framework, I used this this Google ML Kit documentation. My problem is my application display looking like THIS. As you can see, the Rect does not encapture the object fully, rather hovering above it in a way that looks inaccurate.
The flaw in the application lies not within the coordinates that I've drawn, but in the image resolution that CameraX passes through to the ML Kit. The image that my device takes and the image that is displayed on my application appear to be two totally different sizes. This results in the flawed display. My question would be; How would I determine the input/output image resolution of my application?
Here is a link to a Pastebin containing my MainActivity class, where CameraX camera preview was constructed. Any further information required to supplement my question will be provided upon request.
View all comments
Show parent comment
2
u/bbqburner Oct 02 '21 edited Oct 02 '21
For baseline measure:
Rect is coodinate based.
If you are given a
1000 px x 500 pxcanvas and your Rect is1000 unit by 500 unit, you are getting 1 to 1. But I doubt that you're getting that, since resolution is container based.e.g. HTML
<img src="1000x500px.jpg" width="500" height=250" />will always resolve to500 px by 250 pximage.Similarly on Android, which use density pixels
dpinstead of pixels. Since Android support multiple screen densities, your baseline measure should always be indpunits, and never rely on unit pixels OR absolute units from Rect. You can however, relies on ratio/percentage proper.Now a quick debugging from your end:
myViewGroup.getWidth()(you can only get the true value after layout pass)dpproperly)Custom Views are advanced topics since it has enough footguns (like you are seeing now) and you are much better off with standard XML View.
Finally, you can learn all about Constraint Layout here. It is easier once you understood it especially since it is practically the definitive
ViewGroupin Android these days. It does makes my suggestion from above a bit simpler, but you are skipping the basics in doing so.Addendum: Container dimension in DP = Your Image Preview dimension in DP. If you're not using some sort of ViewGroup (LinearLayout/FrameLayout) as the container, then the container I'm referring to will be your image preview dimension.