How to Use Android's Camera or Camera2 API to Support Old and New API Versions Without Deprecation Notes

How to use Android's camera or camera2 API to support old and new API versions without deprecation notes?

Even though the old camera API is marked as deprecated, it is still fully functional, and will remain so for quite a while (as nearly all camera-using applications on the Play Store use it currently).

You'll have to ignore Android Studio's complaints about it being deprecated, but if you want to support Android versions earlier than 21, you have to use the old API.

On API level 21, you can certainly use the new API and its new features, but currently you'll have to maintain a wholly separate flow in your app if you switch between the APIs. Unfortunately, the two APIs have a different enough of a worldview that it's hard to write a support library that would let you use something like the new API on older devices as well (where the library maps from the new API to the old API if not on API 21+).

android camera 2 must use only camera devices supporting backward compatibility

BACKWARD_COMPATIBLE devices are ones that support YUV and JPEG output and a bunch of basic camera behavior.

In general, only very few camera types won't list BACKWARD_COMPATIBLE; one such example is a pure depth camera, which won't produce JPEGs. For such devices, you have to manually check what output formats are actually supported via 'getOutputFormats', since it's likely something like JPEG won't be listed, or it may only support monochrome output and not color, which may make it impossible to use it with a video recorder.

If you're seeing a lot of devices get filtered out by excluding BACKWARD_COMPATIBLE, it'd be interesting to know, since in my experience they're very rare.

Only one APK with Camera2 and old camera API

Option A: Using Both APIs

Step #1: Set your compileSdkVersion to 21 or higher. Ideally, set it to 25, for the latest version of Android.

Step #2: Write code for both APIs.

Step #3: Only call the code that uses the android.hardware.camera2 classes when the device is running Android 5.0+:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
// use camera2
}
else {
// use Camera
}

Option B: Only Use the Classic Camera API

android.hardware.Camera works fine on Android 7.1 and older devices. Until such time as android.hardware.Camera is removed from the Android SDK, just use it. You will not be able to take advantage of any new features offered by the android.hardware.camera2 classes, though.



Related Topics



Leave a reply



Submit