RedApparat / Fotoapparat

Making Camera for Android more friendly. 📸
Apache License 2.0
3.81k stars 405 forks source link

Java friendly doc for switching cameras #294

Open petricf opened 5 years ago

petricf commented 5 years ago

I try to switch cameras. My start point was the demo as seen at github.

The doc says:

fotoapparat.switchTo(
    lensPosition = front(),
    cameraConfiguration = newConfigurationForFrontCamera
)

Due i don't have any knowledge on Kotlin - how to do it in Java only ?

Followup - how to obtain the Configuration from an existing Fotoapparat instance ?

My FA initialization:

Fotoapparat
                .with (this)
                .into (cameraView)
                .focusView (focusView)
                .previewScaleType (ScaleType.CenterCrop)
                .lensPosition (front ())
                .previewFpsRange (lowestFps ())
                .build ();

I also tried (in onCreate method of the Activity):

Fotoapparat fotoapparat = new Fotoapparat (this, cameraView, focusView);
fotoapparat.updateConfiguration (UpdateConfiguration.builder ().lensPosition (back ()). build ());

but what to use in place of the lensPostion for switching it ?

Context:

rsnitsch commented 5 years ago

I stumbled upon the same problem. When switching the camera, I do not want to change the camera configuration at all, but unfortunately it is not possible to provide a null value for the cameraConfiguration in the call to switchTo (it crashes, probably due to a NullPointerException). You can provide CameraConfiguration.standard(), but this will reset the camera configuration to the defaults. If you're using the defaults anyway, this will be your solution.

Otherwise, you have to dig deeper.

The solution is hidden in the sample code: https://github.com/RedApparat/Fotoapparat/blob/18d6ae38ad1887d6a4d8013a82e8362a88a18570/sample/src/main/java/io/fotoapparat/sample/ActivityJava.java

They create a cameraConfiguration field (line 66) where the camera configuration is created once and stored throughout the lifetime of the Activity. Then, this cameraConfiguration object can be reused in the call to switchTo (line 179 in the code).

On the other hand, I wonder why the cameraConfiguration is only used in the switchTo call and nowhere else. This seems to be a bug, because the cameraConfiguration won't be respected at all unless the camera is switched at least once.

Therefore I implemented it slightly differently:

private Fotoapparat createFotoapparat() {
        Fotoapparat result = Fotoapparat
                .with(this)
                .into(mCameraView)
                .previewScaleType(ScaleType.CenterCrop)
                // ...
                .build();

        // This is probably important. This way the cameraConfiguration is used from the start.
        result.updateConfiguration(cameraConfiguration);
        return result;
    }

The take home message for me is that the sample application is the true documentation of this library.