Example usage for android.hardware Camera open

List of usage examples for android.hardware Camera open

Introduction

In this page you can find the example usage for android.hardware Camera open.

Prototype

public static Camera open() 

Source Link

Document

Creates a new Camera object to access the first back-facing camera on the device.

Usage

From source file:Main.java

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
    Camera camera = null;//from w w  w.  j  av a2  s  . co m
    try {
        camera = Camera.open(); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return camera; // returns null if camera is unavailable
}

From source file:Main.java

public static Camera getCameraInstance() {
    Camera c = null;/*  ww  w . java 2s  .  com*/
    try {
        Log.d("debug", "Trying to open camera");
        c = Camera.open();
    } catch (Exception e) {
        // Camera is not available or doesn't exist
        Log.d("debug", "getCamera failed " + e.getMessage());
    }
    return c;
}

From source file:Main.java

@Nullable
public static Camera getCamera() {
    Camera camera = null;/*ww w.java2  s.  c  o  m*/
    try {
        camera = Camera.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return camera;
}

From source file:Main.java

public static Camera openFlightLight() {
    try {//from w  w  w. j ava2  s.com
        Camera camera = Camera.open();
        if (null != camera) {
            Parameters parameters = camera.getParameters();
            List<String> list = parameters.getSupportedFlashModes();
            for (String string : list) {
                if (Parameters.FLASH_MODE_TORCH.equals(string)) {
                    parameters.setFlashMode(string);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    return camera;
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
    Camera c = null;//from  ww  w .j  a  va2 s .co m
    try {
        c = Camera.open(); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
        Log.e("ch.getCI", "Failed to get camera!", e);
    }
    return c; // returns null if camera is unavailable
}

From source file:Main.java

public static void open(Context context) {
    Camera cam = Camera.open();
    Camera.Parameters p = cam.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);//from   ww  w . j a va  2s . co  m
    cam.startPreview();

}

From source file:Main.java

/**
 * Return camera instance//from w  w w  .j a va2  s  .c om
 */
public static Camera getCameraInstance(int displayOrientation) {
    Camera cam = null;
    try {
        cam = Camera.open();

        // More efficient way to find available cameras. Nexus 7 needs this.
        if (cam == null) {
            int availableCameras = Camera.getNumberOfCameras();
            for (int i = 0; i < availableCameras; i++) {
                cam = Camera.open(i);
                if (cam != null)
                    break;
            }
        }

        cam.setDisplayOrientation(displayOrientation);
        Log.d(TAG, "Getting Camera: " + cam.toString());
    } catch (Exception e) {
        Log.e(TAG, "Camera is not available (in use or does not exist)");
        Log.e(TAG, e.toString());
    }
    return cam;
}

From source file:Main.java

public static Camera getCameraInstance(Context context) {
    Camera c = null;// w  w  w.  j a  v a2s.c  om
    try {
        if (checkCameraHardware(context)) {
            c = Camera.open();
        }
    } catch (Exception e) {
        Toast.makeText(context, "Camera is not available", Toast.LENGTH_SHORT).show();
    }
    return c;
}

From source file:Main.java

/** 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  www  .  j a  v  a 2s.  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();
}

From source file:Main.java

/**
 * @return the default camera on the device. Return null if there is no camera on the device.
 *//*from   w w w. j  av a 2 s  .c o m*/
public static Camera getDefaultCameraInstance() {
    return Camera.open();
}