Camera需与SurfaceView配合使用
Camera类常用方法:
Camera.open() 创建Camera实例,打开相机 getParameters() 获取相机参数 release() 释放相机资源 setParameters(Camera.Parameters parameters) 设置相机参数 setPreviewDisplay(SurfaceHolder holder) 设置用于预览的SurfaceView setDisplayOrientation(int i) 设置默认角度,值为度数,如值为90即90° startPreview() 开始预览 stopPreview() 停止预览 takePicture(Camera.ShutterCallback shutter,Camera.PictureCallback raw, Camera.PictureCallback jepg) 用于拍摄,参数shutter为快门回调函数常用于音效等;参数raw为拍摄后回调提供照片数据;参数jpeg为拍摄后回调提供无损照片数据
相机参数常用方法
setPictureSize(int width,int height) 设置图片尺寸 setPictureFormat(int pixelFormat) 设置图片格式,如PixelFormat.JPEG set("jpeg-quality",int quality) 设置图片质量,quality值标准为65,一般为75左右,不超过100 setFocusMode(String focusMode) 设置对焦模式,如FOCUS_MODE_CONTINUOUS_PICTURE为连续图片对焦
//设置相机camera= Camera.open();Camera.Parameters parameters =camera.getParameters();parameters.setPictureSize(640*3,480*3);parameters.setPictureFormat(PixelFormat.JPEG);parameters.set("jpeg-quality",80);parameters.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE);camera.setParameters(parameters);camera.setDisplayOrientation(90);
//绑定SurfaceViewSurfaceHolder surfaceHolder=surfaceView.getHolder();surfaceHolder.addCallback(new SurfaceHolder.Callback() {public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {try {camera.setPreviewDisplay(surfaceHolder);//启动预览camera.startPreview();} catch (IOException e) {throw new RuntimeException(e);}}public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {try {camera.setPreviewDisplay(surfaceHolder);//启动预览camera.startPreview();} catch (IOException e) {throw new RuntimeException(e);}}public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {try {camera.setPreviewDisplay(surfaceHolder);//启动预览camera.startPreview();} catch (IOException e) {throw new RuntimeException(e);}}});//设置拍摄按钮监听器putButton.setOnClickListener(new View.OnClickListener() {public void onClick(View view) {camera.takePicture(null, null, new Camera.PictureCallback() {public void onPictureTaken(byte[] bytes, Camera camera) {camera.stopPreview();Bitmap bitmap_base=BitmapFactory.decodeByteArray(bytes,0,bytes.length);String sdPath= String.valueOf(Environment.getExternalStorageDirectory());//API29以上已不允许在SD卡目录下创建文件夹或文件,需在已有(自带)文件夹下创建文件夹或文件File file=new File(sdPath+"/DCIM/myCamera");boolean fileExists=false;if(!file.exists()){fileExists=file.mkdirs();}else {fileExists=true;}Log.d("OK","文件夹是否存在"+fileExists);int num=1;while (true){String imageName="image"+num+".jpeg";File imageFile=new File(file.getAbsolutePath()+"/"+imageName);if(!imageFile.exists()){//不存在,进行创建imageFile.getParentFile().mkdirs();try {imageFile.createNewFile();} catch (IOException e) {throw new RuntimeException(e+"地址"+imageFile.getAbsolutePath());}try {FileOutputStream outputStream = new FileOutputStream(imageFile);bitmap_base.compress(Bitmap.CompressFormat.JPEG,100,outputStream);Log.d("OK","保存完成"+imageFile.getAbsolutePath());outputStream.flush();outputStream.close();} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}camera.startPreview();break;}else {num++;}}}});}});
//要记得释放相机资源protected void onDestroy() {super.onDestroy();if(camera!=null){camera.release();}}protected void onPause() {super.onPause();camera.stopPreview();}protected void onRestart() {super.onRestart();camera.startPreview();}