android Camera相机类

Camera相机类相关的几个流程方法

Camera.open(cameraId) 打开相机

camera.setDisplayOrientation(0) 设置相机水平方向

mCamera.setPreviewDisplay(holder) 在SurfaceHolder上显示画面

mCamera.setPreviewCallback(callback) 设置回调,将Yuv数据传递到onPreviewFrame方法

previewRate mParams = mCamera.getParameters() 获取相机相关参数

mCamera.stopPreview() 停止预览

mCamera.release() 释放相机

实现Camera.PreviewCallback接口获取Yuv视频流

@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
       //获取实时的Yuv数据流
}

实现Camera.PictureCallback接口实现拍照

private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
         String pictureDir = Environment.getExternalStorageDirectory() + File.separator +"Videocmo/images";
         String pictureName = DateFormat.format("yyyyMMddHHmmss",new Date()).toString()+ ".jpg";
         String path=pictureDir+ File.separator + pictureName;
         try {
              File file = new File(pictureDir);
              if (!file.exists()) {
                   file.mkdirs();
              }
              FileOutputStream fos = new FileOutputStream(path);
              fos.write(data);
              fos.close();
         } catch (FileNotFoundException ignored) {
         } catch (IOException ignored) {
         }
   }
};
mCamera.takePicture(
null, null, mPictureCallback);

寻找相机

private int findCamera(boolean isfront) {
    int cameraCount = 0;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();

    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (isfront) {
            // CAMERA_FACING_FRONT前置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                return camIdx;
            }
        } else {
            // CAMERA_FACING_BACK后置
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                return camIdx;
            }
        }

    }
    return -1;
}

Camera.Parameters相机参数类

Camera.Parameters parameters = camera.getParameters(); //获取相机参数

List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes(); //获取摄像头支持的各种分辨率

parameters.setPictureFormat(PixelFormat.JPEG);//设置照片格式

parameters.setPreviewSize(witdh,height); //设置预览尺寸

parameters.setPictureSize(witdh,height); //设置分辨率

camera.setParameters(parameters);//提交相机参数

设置相机分辨率

private void initCamera(Camera mCamera){
    if(mCamera != null){
        try{
            mParams = mCamera.getParameters();
            Size previewSize = CamParaUtil.getInstance().getPropPreviewSize(mParams.getSupportedPreviewSizes(),1.333f, 640);
            Log.d("log", String.valueOf(previewSize.width) + "-" + previewSize.height);
            pWidth = previewSize.width;
            pHeight = previewSize.height;
            mParams.setPreviewSize(pWidth, pHeight);

            List<String> focusModes = mParams.getSupportedFocusModes();
            if(focusModes.contains("continuous-video")){
                mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
            }
            mCamera.setParameters(mParams);
            mCamera.startPreview();

            isPreviewing = true;
            mPreviwRate = previewRate;
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}

public Size getPropPreviewSize(List<Size> list, float th, int minWidth){
    Collections.sort(list, sizeComparator);

    int i = 0;
    for(Size s:list){
        if((s.width >= minWidth) && equalRate(s, th)){
            break;
        }
        i++;
    }
    if(i == list.size()){
    }
    return list.get(i);
}

 相关阅读

SurfaceView开启相机预览

posted @ 2018-08-17 17:44  YangBryant  阅读(1431)  评论(0编辑  收藏  举报