Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to flip to rear camera, still in mirror mode. #15

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ android {
}

dependencies {
implementation 'androidx.camera:camera-view:1.3.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.camera:camera-lifecycle:1.3.1'
implementation "androidx.camera:camera-camera2:1.3.1"
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.compose.material3:material3:1.1.2'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.camera:camera-view:1.4.1'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.camera:camera-lifecycle:1.4.1'
implementation 'androidx.camera:camera-camera2:1.4.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.compose.material3:material3:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
22 changes: 19 additions & 3 deletions app/src/main/java/com/polar/mirror/FreezeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class FreezeController {
private final Context mContext;
private boolean mCameraFrozen = false;
private final FloatingActionButton mFreezeButton;
private int selectedCamera = CameraSelector.LENS_FACING_FRONT;

FreezeController(Context context, FloatingActionButton freezeButton, PreviewView cameraView,
ImageView freezeView){
Expand All @@ -57,25 +58,27 @@ public class FreezeController {
*/
public void onCameraInitialized(@NonNull ProcessCameraProvider provider, LifecycleOwner lcOwner){
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.requireLensFacing(selectedCamera)
.build();
provider.bindToLifecycle(lcOwner, cameraSelector, mImageCapture);
Log.d(TAG, "completed onCameraInitialized");
}

private int getRotationAngleFromOrientation(int orientation){
int angle = 270;
int angle;
boolean rearCamera = getSelectedCamera() == CameraSelector.LENS_FACING_BACK;
switch (orientation) {
case Surface.ROTATION_90:
angle = 0;
break;
case Surface.ROTATION_180:
angle = 90;
angle = rearCamera ? 270 : 90;
break;
case Surface.ROTATION_270:
angle = 180;
break;
default:
angle = rearCamera ? 90 : 270;
break;
}
return angle;
Expand Down Expand Up @@ -151,4 +154,17 @@ public void toggleFreeze(){
mCameraFrozen = true;
}
}

public void toggleSelectedCamera() {
if (selectedCamera == CameraSelector.LENS_FACING_FRONT) {
selectedCamera = CameraSelector.LENS_FACING_BACK;
} else {
selectedCamera = CameraSelector.LENS_FACING_FRONT;
}
}

public int getSelectedCamera() {
return selectedCamera;
}

}
63 changes: 56 additions & 7 deletions app/src/main/java/com/polar/mirror/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.MirrorMode;
import androidx.camera.core.Preview;
import androidx.camera.view.PreviewView;
import androidx.camera.lifecycle.ProcessCameraProvider;
Expand All @@ -14,6 +15,7 @@
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
Expand All @@ -33,6 +35,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private final static String TAG = "MainActivity";
private Preview mPreview = null;
private static final int CAMERA_PERMISSION_CODE = 858;
private ProcessCameraProvider mCameraProvider = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -125,12 +128,15 @@ private void setupFloatingButtons(){
FloatingActionButton exitButton = findViewById(R.id.exit_button);
FloatingActionButton freezeButton = findViewById(R.id.freeze_button);
FloatingActionButton lowLightButton = findViewById(R.id.low_light_button);
FloatingActionButton cameraChooserButton = findViewById(R.id.camera_chooser_button);
exitButton.setClickable(true);
exitButton.setOnClickListener(this);
freezeButton.setClickable(true);
freezeButton.setOnClickListener(this);
lowLightButton.setClickable(true);
lowLightButton.setOnClickListener(this);
cameraChooserButton.setClickable(true);
cameraChooserButton.setOnClickListener(this);
}

/**
Expand All @@ -147,17 +153,22 @@ private void hideSystemUi(){
* @throws ExecutionException in case of task errors
* @throws InterruptedException in case of thread interruption
*/
@androidx.annotation.OptIn(markerClass = androidx.camera.core.ExperimentalMirrorMode.class)
private void startCamera() throws ExecutionException, InterruptedException {
mPreview = new Preview.Builder().build();
mCameraView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);
mCameraView.addOnLayoutChangeListener(this::onLayoutChange);
mPreview = new Preview.Builder()
.setMirrorMode(MirrorMode.MIRROR_MODE_ON)
.build();
mPreview.setSurfaceProvider(mCameraView.getSurfaceProvider());
ProcessCameraProvider cameraProvider = ProcessCameraProvider.getInstance(this).get();
mCameraProvider = ProcessCameraProvider.getInstance(this).get();
try {
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.requireLensFacing(mFreezeController.getSelectedCamera())
.build();
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, cameraSelector, mPreview);
mFreezeController.onCameraInitialized(cameraProvider, this);
mCameraProvider.unbindAll();
mCameraProvider.bindToLifecycle(this, cameraSelector, mPreview);
mFreezeController.onCameraInitialized(mCameraProvider, this);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -178,6 +189,19 @@ private void toggleLowLightMode(){
mLowLightController.toggleLowLightMode();
}

/**
* Toggles front/rear camera
*/
private void toggleSelectedCamera(){
mFreezeController.toggleSelectedCamera();
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(mFreezeController.getSelectedCamera())
.build();
mCameraProvider.unbindAll();
mCameraProvider.bindToLifecycle(this, cameraSelector, mPreview);
mFreezeController.onCameraInitialized(mCameraProvider, this);
}

@Override
public void onClick(@NonNull View v) {
int viewId = v.getId();
Expand All @@ -188,6 +212,8 @@ public void onClick(@NonNull View v) {
toggleCameraFreeze();
} else if(viewId == R.id.low_light_button){
toggleLowLightMode();
} else if(viewId == R.id.camera_chooser_button){
toggleSelectedCamera();
} else {
Log.w(TAG, "Unknown id of view: " + viewId);
}
Expand Down Expand Up @@ -218,5 +244,28 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}
}
}

private void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
int orientation = mCameraView.getDisplay().getRotation();
if (mFreezeController.getSelectedCamera() == CameraSelector.LENS_FACING_BACK) {
int o;
switch (orientation) {
case Surface.ROTATION_0:
o = Surface.ROTATION_0;
break;
case Surface.ROTATION_90:
o = Surface.ROTATION_270;
break;
case Surface.ROTATION_180:
o = Surface.ROTATION_180;
break;
default: // Surface.ROTATION_270:
o = Surface.ROTATION_90;
break;
}
mPreview.setTargetRotation(o);
} else {
mPreview.setTargetRotation(orientation);
}
}
}
24 changes: 24 additions & 0 deletions app/src/main/res/layout/action_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@

</LinearLayout>

<!-- Wrapper for Low-level light Button and Label -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/camera_chooser_button"
style="@style/Widget.Mirror.FloatingActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/flip_camera"
android:src="@android:drawable/ic_menu_rotate" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/flip_camera"
android:textColor="@color/white" />

</LinearLayout>

</LinearLayout>

</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<string name="no_camera_permissions">Dostęp do kamery nie został przyznany. Użyj aplikacji Ustawienia, aby pryznać dostęp</string>
<string name="low_light">Tryb ciemności</string>
<string name="low_light_mode_overlay">Overlay trybu ciemności</string>
<string name="flip_camera">Obrót</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="no_camera_permissions">Доступ до камери не надано. Скористуйтесь застосунком Налаштування, аби надати доступ вручну</string>
<string name="low_light">Режим пітьми</string>
<string name="low_light_mode_overlay">Оверлей режим пітьми</string>
<string name="flip_camera">Фліп</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="no_camera_permissions">Camera permissions was not granted. Use Settings app to grant it.</string>
<string name="low_light">Dark mode</string>
<string name="low_light_mode_overlay">Low light mode overlay</string>
<string name="flip_camera">Flip</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.3.1' apply false
id 'com.android.application' version '8.7.3' apply false
}
7 changes: 6 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#Tue Oct 24 12:02:08 CEST 2023
# suppress inspection "Annotator"
distributionBase=GRADLE_USER_HOME
# suppress inspection "Annotator"
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
# suppress inspection "Annotator"
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
# suppress inspection "Annotator"
zipStoreBase=GRADLE_USER_HOME
# suppress inspection "Annotator"
zipStorePath=wrapper/dists
Loading