136

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Gee
  • 1,465
  • 2
  • 10
  • 7

7 Answers7

154

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

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • 1
    Good answer. So if you want to support API level 16 and up, it's better to stick to the old camera for now, right? – Loolooii May 13 '15 at 12:08
  • 5
    so the only way is to use if statement and android.os.Build.VERSION.SDK_INT to seperate the code? – hadi Sep 27 '15 at 17:12
  • So for a developer, if you're only targeting API 21 and later, use Camera2 but if you need legacy support use Camera? Or would you recommend detecting build versions and coding 2 different methods using the different APIs? – john.weland Dec 08 '15 at 14:44
  • 2
    It depends on what your app does. If the camera functionality is straightforward point-and-shoot stuff, and you want to target old APIs, just use the old Camera API. But if you're looking to do something more than just grab JPEGs and draw preview, or if you're just targeting new APIs, go with camera2. In the (hard) middle are apps that want to offer fancy optional features on camera2, but work on old devices as well. There, you have to build two separate codepaths, one for each API. – Eddy Talvala Dec 10 '15 at 22:42
  • My phone running 5.1 (First gen moto x) crashes when attempting to use the old API – davkutalek May 31 '16 at 15:31
  • 22
    Deprecating the Camera API was a mistake, they should have introduced a Camera advanced API (for advanced apps such as full fledged camera apps) - otherwise (most) apps which uses the camera just to take a photograph would have to maintain 2 apis. Google should have at least introduced a compact library (as always) – Sudara Aug 15 '16 at 04:18
  • @EddyTalvala Hi Eddy, You say "You'll have to ignore Android Studio's complaints about it being deprecated" but how do you ignore this when it is all underlined red as an error and does not compile? –  Jun 13 '17 at 10:48
  • I agree with @S_Madushan comment, I have to face the issue while recording video and get picture simultaneously, this feature will available on nougat 7+. but how can I get it done? please help. – MohanRaj S Feb 07 '18 at 13:41
  • how to exclude device with LEGACY camera from Google Play? I want to use only Camera2 API and only devices with at least has LIMITED camera – user924 Apr 29 '22 at 16:58
39

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

slott
  • 3,266
  • 1
  • 35
  • 30
  • 14
    This is true. The camera 2 API actually divides camera devices into three categories: LEGACY, LIMITED and FULL. If the camera is classified as LEGACY then all the camera2 API calls are being translated into camera1 under the hood, so it's really not worth the bother. My suggestion is to call `CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraID); if (characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY)...` and choose the old API if it is true. – panonski Jul 08 '16 at 12:33
  • how to exclude legacy camera devices from Google Play? – user924 Apr 29 '22 at 16:56
8

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
 }
SimonT
  • 2,219
  • 1
  • 18
  • 32
user0770
  • 797
  • 3
  • 11
  • 22
  • 33
    this is hardly practical for a full camera implementation. plus, now you have to maintain two codepaths. the version check does have its use in android development, but this isn't it. – katzenhut May 29 '15 at 11:47
  • 5
    What happens if a user is running Build.VERSION_CODES.LOLLIPOP_MR1? Or something above that? I think your second check should be "else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)" – Ralph Pina Jun 12 '15 at 05:51
  • Dears, how can I build in the same apk camera2 and old api if my apps should be work in the 16 and newer api? Flavors is good for this work? – Mateus Dec 14 '16 at 14:11
  • You have to implement both apis. Just keep an interface and two classes, where camera functionality is implemented. Before creating one of the instances for running camera, call the method mentioned above, so it can find out which class and functionality to call – user0770 Dec 16 '16 at 08:12
3

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.

Serj
  • 49
  • 5
1

Use the support annotation

    @TargetApi(21)

to avoid checking

0

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.

teck wei
  • 1,375
  • 11
  • 22
0

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.
  • rajesh780
    • 11
    • 1