原文地址
https://blog.csdn.net/junzia/article/details/52301199
拍照步骤
1.添加权限
2.开启相机时check一下是否有摄像头
3.对预览大小、照片大小和对焦方式进行设置;对相机预览方向进行设置;
4.相机预览前,必须设置预览的承载;对预览View大小进行设置
5.相机预览时拍照,图像回调,对拍照的结果进行裁剪,使图片结果和预览结果相同,对前摄像头拍摄的结果还需要做对称变换。
相机的控制类CameraKitKat源码
public class CameraKitKat extends ACamera{ private Camera camera; private SurfaceHolder holder; private float displayScale; public CameraKitKat(SurfaceView surfaceView) { super(surfaceView); init(); } private void init(){ holder=displayView.getHolder(); }
//获得一个相机实例 @Override public void open(int type){
//获取预览方向设置 int rotation=((WindowManager)displayView.getContext().getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay().getRotation();
//判断相机是否开启 if(!openCamera(type))return;
//修改相机设置 setParameters(camera,rotation);
//设置正确的预览方向 setDisplayOrientation(camera,rotation);
//设置预览的承载,如果没有这一步,相机无法开始预览 setPreviewDisplay(camera,holder);
//开启预览 camera.startPreview(); } @Override public void close(){ camera.stopPreview(); camera.release(); } //调整SurfaceView的大小 private void resizeDisplayView(){
//获得相机实例的默认设置 Camera.Parameters parameters=camera.getParameters();
//将预览View的Size和Camera的PreviewSize设置相同 Camera.Size size=parameters.getPreviewSize();
//返回布局参数的引用 FrameLayout.LayoutParams p= (FrameLayout.LayoutParams) displayView.getLayoutParams(); float scale=size.width/(float)size.height; displayScale=displayView.getHeight()/(float)displayView.getWidth(); if(scale>displayScale){ p.height= (int) (scale*displayView.getWidth()); p.width=displayView.getWidth(); }else{ p.width= (int) (displayView.getHeight()/scale); p.height=displayView.getHeight(); } Log.e("wuwang","-->"+size.width+"/"+size.height); Log.e("wuwang","--<"+p.height+"/"+p.width); displayView.setLayoutParams(p); displayView.invalidate(); }
//获得当前设备的Camera个数,为0表示不支持摄像头 private boolean checkCameraId(int cameraId){ return cameraId>=0&&cameraId<Camera.getNumberOfCameras(); } //相机使用第一步,打开相机,获得相机实例 private boolean openCamera(int cameraId){ if(!checkCameraId(cameraId))return false; camera=Camera.open(cameraId); return true; } //相机使用第二步,设置相机实例参数 //TODO :里面还存在问题,需要修改 private void setParameters(Camera camera,int rotation){ Camera.Parameters parameters=camera.getParameters(); //PreviewSize设置为设备支持的最高分辨率 final Camera.Size size=Collections.max(camera.getParameters().getSupportedPreviewSizes(),new Comparator<Camera.Size>() { @Override public int compare(Camera.Size lhs, Camera.Size rhs) { return lhs.width*lhs.height-rhs.width*rhs.height; } }); parameters.setPreviewSize(size.width,size.height); //PictureSize设置为和预览大小最近的 Camera.Size picSize=Collections.max(parameters.getSupportedPictureSizes(), new Comparator<Camera.Size>() { @Override public int compare(Camera.Size lhs, Camera.Size rhs) { return (int) (Math.sqrt(Math.pow(size.width-rhs.width,2)+Math.pow(size.height-rhs.height,2))- Math.sqrt(Math.pow(size.width-lhs.width,2)+Math.pow(size.height-lhs.height,2))); } }); parameters.setPictureSize(picSize.width,picSize.height); //如果相机支持自动聚焦,则设置相机自动聚焦,否则不设置 if(parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)){ parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } //设置颜色效果 // parameters.setColorEffect(Camera.Parameters.EFFECT_MONO); camera.setParameters(parameters); resizeDisplayView(); } //相机使用第三步,设置相机预览方向 private void setDisplayOrientation(Camera camera,int rotation){ if(rotation== Surface.ROTATION_0||rotation==Surface.ROTATION_180){ camera.setDisplayOrientation(90); }else{ camera.setDisplayOrientation(0); } } //相机使用第四步,设置相机预览载体SurfaceHolder private void setPreviewDisplay(Camera camera,SurfaceHolder holder){ try { camera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } } @Override public void measureSize(int width, int height) { super.measureSize(width, height); }
//拍照 @Override public void takePicture() { super.takePicture(); camera.takePicture(new Camera.ShutterCallback() { @Override
//原始图像数据回调 public void onShutter() { } }, new Camera.PictureCallback() { @Override
//展示图像数据的回调 public void onPictureTaken(byte[] data, Camera camera) { } }, new Camera.PictureCallback() { @Override
//JPEG图像数据的回调 public void onPictureTaken(byte[] data, Camera camera) { if(pictureCallback!=null){ pictureCallback.onPictureTaken(data,displayScale); } } }); } }