From a7dc19664f671159e9e4166185b58dc0d054935f Mon Sep 17 00:00:00 2001 From: florent champigny Date: Tue, 14 Feb 2017 09:35:15 +0100 Subject: [PATCH] Fixed back/front camera init --- app/src/main/AndroidManifest.xml | 6 ++++++ .../camerafragment/sample/MainActivity.java | 3 ++- .../controller/impl/Camera1Controller.java | 13 +++++++------ .../controller/impl/Camera2Controller.java | 18 +++++++++++++----- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index be48fc7..a7e0780 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,6 +24,12 @@ android:name="com.github.florent37.camerafragment.sample.MainActivityCustoms" android:screenOrientation="portrait" android:theme="@style/ThemeFullscreen" > + + + diff --git a/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java b/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java index 72534e5..29e39bb 100644 --- a/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java +++ b/app/src/main/java/com/github/florent37/camerafragment/sample/MainActivity.java @@ -146,7 +146,8 @@ public class MainActivity extends AppCompatActivity { addCameraButton.setVisibility(View.GONE); cameraLayout.setVisibility(View.VISIBLE); - final CameraFragment cameraFragment = CameraFragment.newInstance(new Configuration.Builder().build()); + final CameraFragment cameraFragment = CameraFragment.newInstance(new Configuration.Builder() + .setCamera(Configuration.CAMERA_FACE_FRONT).build()); getSupportFragmentManager().beginTransaction() .replace(R.id.content, cameraFragment, FRAGMENT_TAG) .commit(); diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java index 5b7e1f9..8a071ec 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera1Controller.java @@ -6,8 +6,6 @@ import android.support.annotation.Nullable; import android.util.Log; import android.view.SurfaceHolder; -import java.io.File; - import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.ConfigurationProvider; import com.github.florent37.camerafragment.internal.controller.CameraController; @@ -23,6 +21,8 @@ import com.github.florent37.camerafragment.internal.utils.CameraHelper; import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener; +import java.io.File; + /* * Created by memfis on 7/7/16. */ @@ -101,14 +101,15 @@ public class Camera1Controller implements CameraController, public void switchCamera(@Configuration.CameraFace final int cameraFace) { final Integer backCameraId = cameraManager.getFaceBackCameraId(); final Integer frontCameraId = cameraManager.getFaceFrontCameraId(); + final Integer currentCameraId = cameraManager.getCurrentCameraId(); - if(cameraManager.getCurrentCameraId().equals(frontCameraId)){ + if (cameraFace == Configuration.CAMERA_FACE_REAR && backCameraId != null && !backCameraId.equals(currentCameraId)) { setCurrentCameraId(backCameraId); - } else { + cameraManager.closeCamera(this); + } else if (frontCameraId != null && !frontCameraId.equals(currentCameraId)) { setCurrentCameraId(frontCameraId); + cameraManager.closeCamera(this); } - - cameraManager.closeCamera(this); } @Override diff --git a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java index d075daa..80fdcdd 100644 --- a/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java +++ b/camerafragment/src/main/java/com/github/florent37/camerafragment/internal/controller/impl/Camera2Controller.java @@ -8,8 +8,6 @@ import android.support.annotation.Nullable; import android.util.Log; import android.view.TextureView; -import java.io.File; - import com.github.florent37.camerafragment.configuration.Configuration; import com.github.florent37.camerafragment.configuration.ConfigurationProvider; import com.github.florent37.camerafragment.internal.controller.CameraController; @@ -25,6 +23,8 @@ import com.github.florent37.camerafragment.internal.utils.CameraHelper; import com.github.florent37.camerafragment.internal.utils.Size; import com.github.florent37.camerafragment.listeners.CameraFragmentResultListener; +import java.io.File; + /* * Created by memfis on 7/6/16. */ @@ -96,10 +96,18 @@ public class Camera2Controller implements CameraController, @Override public void switchCamera(final @Configuration.CameraFace int cameraFace) { - setCurrentCameraId(camera2Manager.getCurrentCameraId().equals(camera2Manager.getFaceFrontCameraId()) ? - camera2Manager.getFaceBackCameraId() : camera2Manager.getFaceFrontCameraId()); + final String currentCameraId = camera2Manager.getCurrentCameraId(); + final String faceFrontCameraId = camera2Manager.getFaceFrontCameraId(); + final String faceBackCameraId = camera2Manager.getFaceBackCameraId(); + + if (cameraFace == Configuration.CAMERA_FACE_REAR && faceBackCameraId != null && !faceBackCameraId.equals(currentCameraId)) { + setCurrentCameraId(faceBackCameraId); + camera2Manager.closeCamera(this); + } else if (faceFrontCameraId != null && !faceFrontCameraId.equals(currentCameraId)) { + setCurrentCameraId(faceFrontCameraId); + camera2Manager.closeCamera(this); + } - camera2Manager.closeCamera(this); } private void setCurrentCameraId(String currentCameraId){