问题
I'm designing an app (on version 2.2) that use the camera to capture photos.
I read from http://developer.android.com/sdk/android-2.3.html that version 2.3 and above have multiple cameras support. But i'm still a bit confused because when the camera is launched, it normally have a button that the user can click to switch between front-facing and rear cameras, right?
or
2.2 doesn't have this switch?
I want the user to be able to use both front and rear cameras. Is it possible on version 2.2 or should I use 2.3 and above?
回答1:
Yes, 2.2 can only manage rear camera. For frontal camera you need to upgrade to 2.3+.
private int getBestCameraId() {
PackageManager pm = m_mainThreadContext.getPackageManager();
try {
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
Log.i(TAG, "Phone has a frontal camera.");
return Camera.CameraInfo.CAMERA_FACING_FRONT;
} else {
Log.i(TAG, "Phone has only rear camera.");
return Camera.CameraInfo.CAMERA_FACING_BACK;
}
} catch (Exception e) {
return Camera.CameraInfo.CAMERA_FACING_BACK;
}
}
With this simple function it check whether the phone has a frontal or rear camera, and return the best. The id returned can be used in the Camera.open(id) to select the desired camera.
If in your Manifest you write:
<uses-sdk android:minSdkVersion="8" />
and you develop your project with libraries 2.3 (or +), the function i have posted works like a charm! I've used in a camera project, so trust me ;)
回答2:
Refer this this & this
For APIs >=9, you can use the Camera class to see if it has more than one camera, and query the CameraInfo
getNumberOfCameras
getCameraInfo:
And for Android 2.2 and lower versions, supports a single camera in its SDK.
来源:https://stackoverflow.com/questions/10867830/take-photos-from-front-facing-and-rear-cameras-on-android-2-2