get Camera Info via reflection - Android Camera

Android examples for Camera:Camera Attribute

Description

get Camera Info via reflection

Demo Code


//package com.java2s;

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

import android.hardware.Camera;

import android.os.Build;

public class Main {

    @SuppressLint("NewApi")
    public static Object getCameraInfo(int cameraId) {
        Object result = null;/*  ww  w  .  j  a va  2s  .  c o m*/
        if (Build.VERSION.SDK_INT >= 9) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            result = info;
        } else {
            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();
                }
                Method getCameraInfoMethod = cameraClass.getMethod(
                        "getCameraInfo", Integer.TYPE, cameraInfoClass);
                if (getCameraInfoMethod != null && cameraInfoClass != null) {
                    getCameraInfoMethod.invoke(null, cameraId, cameraInfo);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}

Related Tutorials