Gee
Gee

Reputation: 1465

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

The new camera2 API confuses me. I want to develop an app (for Android APIs 10 - 21) which uses the device's camera. As stated here, I should use the "Camera" API.

However, when I try to add the "Camera" API (android.hardware.Camera) to the manifest's user features, it is marked as deprecated. On the other hand, I cannot change it to the "camera2" API (android.hardware.camera2) since it is only compatible with Android API 21+ (Android 5 - Lollipop) - Would have linked it too, but I can only add 2 links.

Not only do I want my app to run on older versions of Android, but also the newest one...

Upvotes: 136

Views: 91648

Answers (7)

rajesh780
rajesh780

Reputation: 11

Plz read link Camera Version Support They state that....
Camera API1
Android 5.0 deprecated Camera API1, which continues to be phased out as new platform development focuses on Camera API2. However, the phase-out period will be lengthy, and Android releases will continue to support Camera API1 apps for some time. Specifically, support continues for:

  • Camera API1 interfaces for apps. Camera apps built on top of Camera API1 should work as they do on devices running earlier Android release versions.
  • Camera HAL versions. Includes support for Camera HAL1.0.
  • Upvotes: 0

    Serj
    Serj

    Reputation: 49

    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.

    Upvotes: 3

    Prudhvi Raj Kumar
    Prudhvi Raj Kumar

    Reputation: 81

    Use the support annotation

        @TargetApi(21)
    

    to avoid checking

    Upvotes: 1

    teck wei
    teck wei

    Reputation: 1385

    I found out the best option is to create two activity. Use the general way to check for the current device API

    Intent i;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        i = new Intent(context,camera2.class)
    } else {
        i = new Intent(context,camera.class);
    }
    startActivity(i);
    

    This way I don't have to having alot of confusion when look back the code. The code is easy to modify since it seperated.

    Upvotes: 0

    slott
    slott

    Reputation: 3335

    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!!

    Upvotes: 39

    user0770
    user0770

    Reputation: 807

    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
     }
    

    Upvotes: 8

    Eddy Talvala
    Eddy Talvala

    Reputation: 18107

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

    Upvotes: 155

    Related Questions