Android Camera — Set Orientation with Sensors

Hannah Olukoye
3 min readSep 13, 2021

Developers can control a number of system capabilities on Android devices in order to attain their own end objective. The sensor’s functionality is one of the most recent components I worked with while developing an Android app for AI.

Sensors are an essential part of any mobile device since they improve the overall user experience. Sensors such as gyroscopes, accelerometers, and others create a richer experience for consumers.

There are 3 types of sensors supported by Android:

1) Motion Detection — These are used to monitor acceleration and rotational forces, typically associated with three axes (x, y, z).

2) Position sensors — These are used to determine the device’s physical location.

3) Environmental Sensors — These monitor changes in the environment, such as temperature and humidity.

The why?

In Android applications’ greater usage of the camera in AI and other machine learning capabilities. When gathering data for training purposes, it is critical to capture accurate images. When the angle, orientation, and even size of a picture are all important, having the option to steer the viewer to the correct image is a huge plus.

In this article, I’ll show you how to change the camera orientation to a precise angle before taking a picture, all while giving the user helpful hints.

The what?

  1. Create a camera fragment file that will take an image when a button is pressed.
onAccuracyChanged

2. Set event listeners for the device’s accuracy and sensory when the camera is open/ready for capture. As the user begins a capture session, this will be able to track the device’s movement.

3. When the user is attempting to reach the correct angle, use crosshairs (which can be an android drawable).

onSensorChanged

4. If the orientation is incorrect, remember to display an error message with the right information.

Reference: Here’s the entire fragment file

Ready to try it out?

When your application is running well, you should see something like this: the white crosshair changes to blue when the camera is at a 90-degree angle.

Demo

Special credits to:

  1. https://dev.opera.com/articles/w3c-device-orientation-api/
  2. https://www.javatpoint.com/android-sensor-tutorial

--

--