安卓(Android) 怎么打开选定的相机(Camera)

2019年10月27日23:03:31安卓(Android) 怎么打开选定的相机(Camera)已关闭评论 277 1984字阅读6分36秒

1.简介

安卓(Android) 怎么打开选定的相机(Camera)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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;
        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);

                
                cameraPreview.connectCamera(camera);

                
                Camera.Parameters cameraParameters = camera.getParameters();

                
                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 {

                }

                
                cameraParameters
                        .setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                
                cameraParameters
                        .setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
                
                cameraParameters
                        .setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

                ((MainActivity) activity).CameraParameters = cameraParameters;


            } catch (Exception ex) {
                
            }

        }
        return camera;
    }
}

 
继续阅读
编程十万个怎么办