20

For the Android API version 2.1 and higher, we can use context:

getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)

But before version 2.1, how can we perform the same operation? Is there anything like this that does not involve invoking Camera.open and then getParameters?

Donut
  • 110,061
  • 20
  • 134
  • 146
user441316
  • 483
  • 2
  • 5
  • 13

5 Answers5

23
List<String> supportedFocusModes = camera.getParameters().getSupportedFocusModes();
boolean hasAutoFocus = supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)
Elhanan Mishraky
  • 2,736
  • 24
  • 26
6

i'm guessing: Do not use the unknown constant.

getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)

Should be:

getPackageManager().hasSystemFeature("android.hardware.camera.autofocus")

It was a short sight of the developers to use constants here. It solves the problem of knowing if a device, running an API that knows about a feature has a feature. but fails on the case you just mentioned... they really make supporting multiple api levels difficult.

Updated: just tested it myself... PackageManager.hasSystemFeature() only showed up at API level 5. I was trying to add that check to my code that can very well support API level 3 (1.5) but which could benefit from camera's auto focus...seems like i have to choose support 1.5 or be able to use auto focus... or move my backward compatibility to level 5... or implement this http://www.java.net/forum/topic/java-tools/java-development-tools/wwyt-conditional-compilation-pre-process ...yeah, right.

they really make it difficult to support multiple versions. So sorry 1.5 and 1.6 and 2.0 users. since my device is on 2.2 that will be my bottom line.

gcb
  • 13,901
  • 7
  • 67
  • 92
  • Using "unknown constant" is fine since it will be inlined at compilation time. So it'll be equivalent to your "should be" in the .class/.dex file. Run `lint --show InlinedApi` in your Android SDK's `tools` folder for more info. `hasSystemFeature` is one of those places where inlining doesn't hurt. – TWiStErRob Oct 26 '14 at 12:41
  • 1
    Beware; this doesn't let you specify which camera, and some devices have autofocus only on the front-facing camera, and not the rear-facing one. – Bill Fraser May 10 '17 at 22:43
3
   private void getSuppourtedFocusedModes(Camera camera) 
   {
        final Camera.Parameters parameters = camera.getParameters();
        List<String> supportedFocusModes = parameters.getSupportedFocusModes();
        LogUtils.infoMsg("supportedFocusModes " + supportedFocusModes);
        for (String mode : supportedFocusModes) {
            LogUtils.infoMsg("supportedFocusModes " + mode);
        }
    }
Salmaan
  • 3,543
  • 8
  • 33
  • 59
Jaya
  • 999
  • 11
  • 8
2
CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(CAMERA_SERVICE);

int[] afModes = cameraManager.getCameraCharacteristics("0").get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);

if (afModes.length <= 1)
{Log.d(TAG, "Camera doesn't have autofocus");}
else
{Log.d(TAG, "Camera has autofocus");}

        Log.d(TAG, "CONTROL_AF_AVAILABLE_MODES:");
        for (int position : afModes) {
            switch (afModes[position]) {
                case 0:
                    Log.d(TAG, "CONTROL_AF_MODE_OFF (0)");
                    break;
                case 1:
                    Log.d(TAG, "CONTROL_AF_MODE_AUTO (1)");
                    break;
                case 2:
                    Log.d(TAG, "CONTROL_AF_MODE_MACRO (2)");
                    break;
                case 3:
                    Log.d(TAG, "CONTROL_AF_MODE_CONTINUOUS_VIDEO (3)");
                    break;
                case 4:
                    Log.d(TAG, "CONTROL_AF_MODE_CONTINUOUS_PICTURE (4)");
                    break;
                case 5:
                    Log.d(TAG, "CONTROL_AF_MODE_EDOF (5)");
                    break;
                default:
                    Log.d(TAG, String.valueOf(afModes[position]));
            }
        }
Marek Suma
  • 51
  • 2
0

There are a number of methods of the Camera.Parameters class added in API Level 5 (I believe that maps to Android 2.0) that will return a list of supported features. Call getSupportedFocusModes on the Camera.Parameters object retrieved from camera.getParameters()

http://developer.android.com/reference/android/hardware/Camera.Parameters.html

Rich
  • 36,270
  • 31
  • 115
  • 154
  • 1
    I believe he wants to know for android 1.5 and such. – blindstuff Dec 06 '10 at 16:40
  • Yes, but you have to invoke Camera.open first, which I don't want to ? What I want is just an easy way to get the information of whether the phone's camera support auto-focus – user441316 Dec 06 '10 at 16:47