Android to determine whether there is a front-facing camera, rear-mounted camera method _android

Source: Internet
Author: User

Usually we do camera operations, such as scanning two-dimensional code needs to determine whether there is a rear-mounted camera (rear camera), such as the Nexus 71 generation has no rear-mounted camera, so in the attempt to use, we need to make a judgment for some hints or processing.

The following code is a series of methods used to determine whether a front-facing camera (Front Camera) is available, and the rear camera.

Copy Code code as follows:

private static Boolean checkcamerafacing (final int facing) {
if (Getsdkversion () < Build.version_codes. Gingerbread) {
return false;
}
Final int cameracount = Camera.getnumberofcameras ();
Camerainfo info = new Camerainfo ();
for (int i = 0; i < Cameracount; i++) {
Camera.getcamerainfo (i, info);
if (facing = = info.facing) {
return true;
}
}
return false;
}

public static Boolean Hasbackfacingcamera () {
Final int camera_facing_back = 0;
Return checkcamerafacing (Camera_facing_back);
}

public static Boolean Hasfrontfacingcamera () {
final int camera_facing_back = 1;
Return checkcamerafacing (Camera_facing_back);
}

public static int getsdkversion () {
return Android.os.Build.VERSION.SDK_INT;
}

Note: Since both Getnumberofcameras and Getcamerainfo are introduced as API 9, the method applies only to 2.3 and above.

Extended reading: http://developer.android.com/reference/android/hardware/Camera.html http://developer.android.com/reference /android/hardware/camera.camerainfo.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.