Portrait Mode in the Flex 3 Camera
In January 2023, Clover announced that the Flex 3 is available in the United States. The Flex 3 supports Android 10 and features a larger display — 5.99" IPS HD. The Flex 3 camera specifications have also changed.
In this blog, we share our new portrait mode in the Flex 3 camera preview and options to rotate images, if needed. If you plan on building an app or have built apps that use the Flex camera, check out these updates with the Flex 3 camera sensor. Let’s look around and see what’s new.
Camera, scan, action!
What’s new: camera orientation
Traditionally, the Clover device camera previews have had a landscape orientation. The most common use case of the camera in the Flex is for barcode scanning and stock, and the current orientation of the sensor continues to support these features. However, if your use cases include QR code or driver license scanning, keep reading to see best practices for working with the Flex 3 camera sensor. Clover devices continue to support all image types that Android allows.
Best practice: post-capture rotation
We’ll now go over the recommended method to rotate the image after capture. Later in this blog, we will explore rotating the camera sensor. We suggest you use the camera2
API for your Flex 3 apps, like in these examples.
Some details to consider before starting: Android has made tools that allow hardware manufacturers to specify the camera sensor orientation. This orientation is readable to developers through the following APIs.
Camera2
APIs
See Sensor Orientation section in Camera Characteristics:
- For Flex2 the CameraCharacteristics.SENSOR_ORIENTATION and Camera.CameraInfo.orientation will return 0
- For Flex3 the CameraCharacteristics.SENSOR_ORIENTATION and Camera.CameraInfo.orientation will return 270
Formula for image orientation
Along with the API information, we can get the correct orientation for an image using the calculations listed in the Camera Preview resource page for Android. The formula to get the correct orientation is:
Where:
- sensorOrientationDegrees is the camera sensor’s orientation
- deviceOrientationDegrees is the current device orientation (landscape/portrait orientation)
- sign is 1 for front-facing cameras -1 for rear-facing cameras
Flex 2 camera is listed as front-facing; Flex 3 camera is listed as back-facing.
Using the Camera2 API to rotate the image after capture
Get the current rotation of the image
We get the current rotation of the image to plug into the formula as deviceOrienationDegrees
so that we can then set the desired rotation of the image. With deviceOrienationDegrees
you can collect the surface rotation, which we’ll later use to calculate jpeg orientation.
camera2/CameraManager#getCameraCharacteristics(java.lang.String)
Set desired rotation of image
Next, we’ll use getJpegOrientation
to rotate the captured image to produce a jpeg image in the desired orientation. As mentioned, we’ll use this to calculate the angle to rotate the resulting image, regardless of the sensor data. If you would like to keep the image rotation based on sensor data instead, you can see Google samples in the android-Camera2Basic Github repository.
camera2/CaptureRequest.html#JPEG_ORIENTATION
Update the Exif data using the rotation value
Exifinterface
is used for many things, but for this example we are using it to handle orientation details when the image is saved to the Flex 3 device and to keep the view orientation of the device constant regardless of device orientation.
Use textureView
to rotate camera preview
The rotate camera preview method is provided for convenience, but Clover does not recommend this use case due to data loss and preview distortion when applying to the Flex 3 camera. Considering physics and the rectangular shape of the camera sensor, if we apply the same methods as for Flex 2, the Flex 3 model will not be uniform. The sensor image buffer is rotated clockwise for back-facing cameras.
Texture view rotates the sensor image buffer based on sensor orientation but does not handle device rotation or preview scaling. Scaling and rotation can be encoded in a Matrix transformation.
Information on this is available for Android Camera 2 in the Camera preview topic.
In summary, the Flex is still the device you know and love. You just need to now note the new camera sensor orientation when developing for Flex 3. General Clover Flex 3 device specifications are available on our Developer Docs portal in the Announcement page. If you are curious about additional Clover 3rd Generation devices, click here for Mini 3. We look forward to seeing what you build with Clover tools!