Is there any way to listen for when the camera has

2019-02-19 11:17发布

问题:

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 the camera fragment is loading into it's host Activity/Fragment
  2. 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

From looking into the documentation for CWAC Camera, I haven't found anything that supports this sort of callback. Is this sort of thing even possible to listen for? If so, does the library have an easy way to do it that I'm not seeing?

Thanks in advance for all your help.

回答1:

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)



回答2:

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.