相机Camera

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为拍摄后回调提供无损照片数据
        //设置相机
        camera= Camera.open();
        Camera.Parameters parameters =camera.getParameters();
        parameters.setPictureSize(640,480);
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.set("jpeg-quality",80);
        camera.setParameters(parameters);
        camera.setDisplayOrientation(90);


        //绑定SurfaceView
        SurfaceHolder 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();
        }
    }

你可能感兴趣的:(android,java)