Opens the camera with the given ID - Android android.hardware

Android examples for android.hardware:Camera ID

Description

Opens the camera with the given ID

Demo Code

import android.graphics.Bitmap;
import android.hardware.Camera;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

public class Main{

    /** Opens the camera with the given ID. If the Android API doesn't support multiple cameras (i.e. prior to Android 2.3), 
     * always opens the primary camera./*from w  w  w  . ja v  a 2  s. com*/
     */
    public static Camera openCamera(int cameraId) {
        if (cameraId >= 0) {
            Method openMethod = null;
            try {
                openMethod = Camera.class.getMethod("open", int.class);
            } catch (Exception ex) {
                openMethod = null;
            }
            if (openMethod != null) {
                try {
                    return (Camera) openMethod.invoke(null, cameraId);
                } catch (Exception ignored) {
                }
            }
        }
        return Camera.open();
    }

}

Related Tutorials