Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I enable Camera2 API?

prop in the /system partition of your Android device, you can enable the Camera2 API functionality. First you'll need a rooted phone, and a method of editing your build. prop file. You can either use a root file explorer app (like ES Explorer) to navigate to the /system partition on your phone and open build.

What is Camera2 API support?

Camera2 is the latest low-level Android camera package and replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations.

Is Camera2 API deprecated?

camera2 API for new applications. This interface was deprecated in API level 21. We recommend using the new android.

How do I know if my phone has Camera2 API?

Well, all you need to do is download a simple app called 'Camera2 API probe' from the Google Play Store and run it. The app gives detailed info about both the rear and front camera sensors of your Android phone. From that info, you can easily deduce whether your Android device supports Camera2 API or not.


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+).


Put all the methods from the camera that you need in an interface and then create a camera instance like this

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Log.d(TAG, "camera2 selected");
        this.camera = new Camera2(getContext());
    } else {
        Log.d(TAG, "camera1 selected");
        this.camera = new Camera1(getContext());
    }

This way you will have everything split up and it will make your life so much easier.

Word of advice - life with camera2 isn't that great. Venders still make crap implementations and you will thus have to add lots of conditions and workarounds.

Example 1 - S6 reports that it doesn't support flash :) Example 2 - An LG device reports back a list of supported image sizes - however not all of them are actually supported!!


To support api you want, use the code below. Just determine the appropriate names corresponded api levels. For example, API 21 is LOLLIPOP, and API 15 is ICE_CREAM_SANDWICH_MR1.

 if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)  
                                    && ((Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP))) {
           // your code here - is between 15-21

 } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           // your code here - is api 21
 }

Although, what Google recommend use Camera2 Api >= 21, but you could have problem with manual settings.

When you need implement app for taking photo with Auto Setting Mode, it'll work fine. But! If need create app with Manual Setting Mode implementation, for devices that have API >= 21, firstly, need check supported HARDWARE LEVEL:

Select camera(Front, Face), get it characteristics and check HARDWARE LEVEL.

mCameraCharacteristics = mCameraManager.getCameraCharacteristics(mCameraId)

val level = mCameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)

CameraCharacteristics represent next supported levels: LIMITED, FULL, LEGACY, LEVEL_3, EXTERNAL.

At a high level, the levels are:

LEGACY devices operate in a backwards-compatibility mode for older Android devices, and have very limited capabilities.

LIMITED devices represent the baseline feature set, and may also include additional capabilities that are subsets of FULL.

FULL devices additionally support per-frame manual control of sensor, flash, lens and post-processing settings, and image capture at a high rate.

LEVEL_3 devices additionally support YUV reprocessing and RAW image capture, along with additional output stream configurations.

If you got the LEGACY supprot level, you should use old Camera Api.


Use the support annotation

    @TargetApi(21)

to avoid checking