find Front Camera Id via Reflection - Android Camera

Android examples for Camera:Front Camera

Description

find Front Camera Id via Reflection

Demo Code


//package com.java2s;

import java.lang.reflect.Method;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;

import android.os.Build;

public class Main {
    public static final int CAMERA_FACING_FRONT = 1;
    public static final int NO_CAMERA = -1;

    @SuppressLint("NewApi")
    public static int findFrontCameraId(Context context) {
        int result = NO_CAMERA;
        if (!isSupportFrontCamera(context)) {
            return result;
        }//from w  w w  .  j a v a  2 s.com

        int level = android.os.Build.VERSION.SDK_INT;
        if (level >= 9) {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            int cameraCount = Camera.getNumberOfCameras();
            for (int i = 0; i < cameraCount; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    result = i;
                    break;
                }
            }
        } else {
            //TODO 2.3
        }

        return result;
    }

    public static boolean isSupportFrontCamera(Context context) {
        PackageManager pm = context.getPackageManager();
        return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
    }

    @SuppressLint("NewApi")
    public static Object getCameraInfo(int cameraId) {
        Object result = null;
        if (Build.VERSION.SDK_INT >= 9) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            result = info;
        } else {
            //????CameraInfo???????
            Class<?> cameraClass;
            try {
                cameraClass = Class.forName("android.hardware.Camera");
                Object cameraInfo = null;
                Class<?> cameraInfoClass = Class
                        .forName("android.hardware.Camera$CameraInfo");
                if (cameraInfoClass != null) {
                    cameraInfo = cameraInfoClass.newInstance();
                }
                //Field field = null;
                //                if (cameraInfo != null) {
                //                    //field = cameraInfo.getClass().getField("facing");
                //                }
                Method getCameraInfoMethod = cameraClass.getMethod(
                        "getCameraInfo", Integer.TYPE, cameraInfoClass);
                if (getCameraInfoMethod != null && cameraInfoClass != null) {
                    getCameraInfoMethod.invoke(null, cameraId, cameraInfo);
                }
                //                    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
                //                        getCameraInfoMethod.invoke(null, camIdx, cameraInfo);
                //                        int facing = field.getInt(cameraInfo);
                //                        if (facing == 1) { //Camera.CameraInfo.CAMERA_FACING_FRONT 
                //                        }
                //                    }
                //                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}

Related Tutorials