Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.Context; import android.content.pm.PackageManager; import android.hardware.Camera; import android.util.Log; public class Main { private static String TAG = "com.example.asm.CameraUtils"; /** * A safe way to get an instance of the Camera object. */ public static Camera getCameraInstance(Context context, int CameraId) { Camera c = null; if (!checkCameraHardware(context)) { return c; // device has no camera } try { c = Camera.open(CameraId); // attempt to get a Camera instance Log.d(TAG, "open camera succeed"); } catch (Exception e) { // Camera is not available (in use or does not exist) Log.d(TAG, "open camera failed"); } return c; // returns null if camera is unavailable } /** Check if this device has a camera */ private static boolean checkCameraHardware(Context context) { if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { // this device has a camera return true; } else { // no camera on this device Log.d(TAG, "no camera on this device"); return false; } } }