get Camera Facing by camera ID - Android Camera

Android examples for Camera:Camera Facing

Description

get Camera Facing by camera ID

Demo Code


//package com.java2s;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.annotation.SuppressLint;

import android.hardware.Camera;

import android.os.Build;

public class Main {
    public static final int CAMERA_FACING_BACK = 0;
    public static final int CAMERA_FACING_FRONT = 1;

    @SuppressLint("NewApi")
    public static int getCameraFacing(int cameraId) {
        int result = -1;
        Object info = getCameraInfo(cameraId);
        if (info != null) {
            if (Build.VERSION.SDK_INT >= 9) {
                result = ((Camera.CameraInfo) info).facing;
                if (result == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    result = CAMERA_FACING_FRONT;
                } else { //back-facing
                    result = CAMERA_FACING_BACK;
                }/*from  ww  w.  java2s.com*/
            } else {
                Object r = getCameraInfoField(info, "facing");
                if (r != null) {
                    result = (Integer) r;
                }
            }
        }

        return result;
    }

    @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;
    }

    protected static Object getCameraInfoField(Object info, String fieldKey) {
        Object result = null;
        try {
            Field field = info.getClass().getField("orientation");
            result = field.getInt(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related Tutorials