如何在屏幕中使用闪光灯和声音单击图像时,如何在表面视图中通知用户

时间:2017-08-30 15:03:47

标签: android surfaceview

我有什么:我有一个表面视图,我在拍摄图像

我要做的是:

  1. 一旦我拍摄了图像,我想表现出像闪光效果一样 指示图像被捕获,相机发出的声音
  2. 如何实现此目标
  3. public class FrgCamera extends Fragment implements SurfaceHolder.Callback {
    
    
        private Unbinder unbinder;
        private String mFileUriCreated;
    
        @BindView(R.id.surfaceview) SurfaceView surfaceview;
    
        private SurfaceHolder surfaceHolder;
        private Camera camera;
        private boolean previewing = false;
        private Camera.PictureCallback jpegCallback;
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return init(inflater, container);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    
            super.onActivityCreated(savedInstanceState);
            initOnActivityCreated();
    
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
    
            stopTheCamera(camera);
            unbinder.unbind();
    
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            initSurfaceView();
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            refreshCamera();
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            stopTheCamera(camera);
        }
    
    
        @OnClick(R.id.surfaceview)
        public void stopcamerapreview(View view) {
    
            camera.takePicture(null, null, jpegCallback);
    
        }
    
        /**
         * INIT OnCreateView
         */
        private View init(LayoutInflater inflater, ViewGroup container) {
    
            View rootView = inflater.inflate(R.layout.frg_camera, container, false);
            ButterKnife.bind(this, rootView);
            unbinder = ButterKnife.bind(this, rootView);
            return rootView;
        }
    
        /**
         * INIT OnActivityCreated
         */
        private void initOnActivityCreated() {
    
            getActivity().getWindow().setFormat(PixelFormat.UNKNOWN);
            surfaceHolder = surfaceview.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
            jpegCallback = new Camera.PictureCallback() {
                public void onPictureTaken(byte[] data, Camera camera) {
    
                    try {
                        FileUtil imgDevice = new FileUtil(getActivity());
                        mFileUriCreated = imgDevice.createFile(data);
                    } catch (IOException e) {
                        Timber.d(e, e.getMessage());
                    }
                    refreshCamera();
                }
            };
        }
    
    
        /**
         * Refresh Camera
         */
        public void refreshCamera() {
    
            if (surfaceHolder.getSurface() == null) {
                // preview surface does not exist
                return;
            }
    
            // stop preview before making changes
            try {
                camera.stopPreview();
            } catch (Exception e) {
                Timber.d(e, e.getMessage());
            }
    
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
            } catch (Exception e) {
                Timber.d(e, e.getMessage());
            }
        }
    
        /**
         * Initialize Surface view
         */
        private void initSurfaceView() {
            try {
                // open the camera
                camera = Camera.open();
                camera.setParameters(setCameraParams());
                camera.setDisplayOrientation(90);
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
            } catch (Exception e) {
                Timber.d(e, e.getMessage());
                return;
            }
    
        }
    
        /**
         * SET the camera params for the device
         */
        private Camera.Parameters setCameraParams() {
    
            Camera.Parameters param = camera.getParameters();
            ImgParmUtil imgParams = new ImgParmUtil(param);
    
            return imgParams.getImageParam();
    
        }
    
    
        /**
         * STOP the camera
         */
        private void stopTheCamera(Camera camera) {
    
            if (camera != null && this.previewing) {
                camera.stopPreview();
                camera.release();
                previewing = false;
            }
    
        }
    
    
    }
    

0 个答案:

没有答案