open Selected Camera - Android Camera

Android examples for Camera:Open Camera

Description

open Selected Camera

Demo Code


import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.ViewGroup;
import android.view.WindowManager;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{
    public static Camera openSelectedCamera(int cameraId, Activity activity) {

        Camera camera = null;//from   w  ww  .j a  v a  2s  .c  o m
        if (cameraId != AppConstants.NOT_SET) {
            try {
                camera = Camera.open(cameraId);

                CameraPreview cameraPreview = (CameraPreview) activity
                        .findViewById(R.id.cameraPreview);
                ViewGroup.LayoutParams params = cameraPreview
                        .getLayoutParams();
                DisplayMetrics dm = new DisplayMetrics();
                activity.getWindowManager().getDefaultDisplay()
                        .getMetrics(dm);
                params.width = dm.widthPixels;
                params.height = dm.heightPixels;
                cameraPreview.setLayoutParams(params);

                // Connect the camera to our layout via the cameraPreview object.
                cameraPreview.connectCamera(camera);

                // Get the settings from the camera so that we can change them.
                Camera.Parameters cameraParameters = camera.getParameters();

                // Get the current resolution of the camera.
                Camera.Size size;

                ((MainActivity) activity).SupportedPictureSizes = cameraParameters
                        .getSupportedPictureSizes();

                if (((MainActivity) activity).SelectedPictureSize != null) {
                    size = ((MainActivity) activity).SelectedPictureSize;
                } else {
                    size = cameraParameters.getPictureSize();

                    size.width = 2592;
                    size.height = 1944;
                    ((MainActivity) activity).SelectedPictureSize = size;
                }

                int i = 0;

                boolean sizeFound = false;
                for (Camera.Size pictureSize : ((MainActivity) activity).SupportedPictureSizes) {
                    if (size.width == pictureSize.width
                            && size.height == pictureSize.height) {
                        ((MainActivity) activity).SelectedPictureSizeIndex = i;
                        sizeFound = true;
                        break;
                    }
                    i++;
                }

                if (sizeFound) {
                    cameraParameters
                            .setPictureSize(size.width, size.height);
                } else {

                }

                // set focus to auto
                cameraParameters
                        .setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                // set white balance to auto
                cameraParameters
                        .setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
                // set flash to auto
                cameraParameters
                        .setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

                ((MainActivity) activity).CameraParameters = cameraParameters;


            } catch (Exception ex) {
                //Log.e(LOG_TAG, "Error opening camera: " + ex.getMessage());
            }

        }
        return camera;
    }
}

Related Tutorials