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

前端 未结 2 326
滥情空心
滥情空心 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)

    0 讨论(0)
  • 2021-01-14 19:57

    I'd like to show a ProgressBar as the camera is loading into the Activity/Fragment

    I have no idea what "loading into the Activity/Fragment" means.

    After a user has taken a picture, I want to show a ProgressBar again overlay on the picture taking button, so they understand they can't immediately take a picture until the camera is ready again

    You know when you are calling takePicture(). You can know when saveImage() is called on your CameraHost by creating your own implementation of CameraHost or own subclass of SimpleCameraHost. You can do your ProgressBar work at those times. You can see this in the demo app, where I disable and re-enable the action bar item for the same purpose.

    0 讨论(0)
提交回复
热议问题