Is there any way to listen for when the camera has loaded in? cwac-camera

前端 未结 2 358
滥情空心
滥情空心 2021-01-14 19:12

I\'ve got my CWAC Camera fragment setup, and everything is working nicely. There\'s a few customizations I\'d like to do.

  1. I\'d like to show a ProgressBar as th
2条回答
  •  一个人的身影
    2021-01-14 19:53

    To be notified when the camera is loaded and preview started, use this code:

    public class MyCameraFragment extends CameraFragment {
    
        CameraView cameraView;
    
        Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                // camera is "loaded" and first preview is sent to the screen
                // do whatever you want to do
            }
        };
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            cameraView = // code to find your CameraView
        }
    
        @Override
        public void onResume() {
            super.onResume();
            Camera camera = cameraView.getCamera();
            if (null != camera) {
                camera.setOneShotPreviewCallback(previewCallback);
            }
        }
    
        private void somehowRestartedCameraViewForWhateverReason() {
            if (null != cameraView) {
                cameraView.onResume(); // force it to restart
                Camera camera = cameraView.getCamera();
                if (null != camera) {
                    camera.setOneShotPreviewCallback(previewCallback);
                }
            }
        }
    }
    

    I used this code to update the UI, so that flash button only appears when camera supported:

    camera.getParameters().getSupportedFlashModes()
    

    return non-null List and size > 0. Usually Front Facing Camera has no flash but who knows?

    If you want to switch camera, please also see my other answer here: CWAC Camera - Multiple camera views in one layout (and switching between them)

提交回复
热议问题