-
-
Notifications
You must be signed in to change notification settings - Fork 458
chore(samples): Add CameraX screen #4774
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
Merged
+251
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d3f8605
chore(samples): Add CameraX screen
romtsn 84dfbed
Revert crash button
romtsn 21f2c53
Formatting
romtsn 08134d6
Merge branch 'main' into rz/chore/add-camerax-sample
romtsn b80c619
Add null check
romtsn 74a9389
Merge branch 'main' into rz/chore/add-camerax-sample
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,10 @@ | |
<activity android:name=".FrameDataForSpansActivity" | ||
android:exported="false"/> | ||
|
||
<activity | ||
android:name=".CameraXActivity" | ||
android:exported="false" /> | ||
|
||
<!-- NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard--> | ||
<meta-data android:name="io.sentry.dsn" android:value="https://[email protected]/5428559" /> | ||
|
||
|
166 changes: 166 additions & 0 deletions
166
...mples/sentry-samples-android/src/main/java/io/sentry/samples/android/CameraXActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package io.sentry.samples.android; | ||
|
||
import android.Manifest; | ||
import android.content.ContentValues; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.provider.MediaStore; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.camera.core.Camera; | ||
import androidx.camera.core.CameraSelector; | ||
import androidx.camera.core.ImageCapture; | ||
import androidx.camera.core.ImageCaptureException; | ||
import androidx.camera.core.Preview; | ||
import androidx.camera.lifecycle.ProcessCameraProvider; | ||
import androidx.camera.view.PreviewView; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import io.sentry.Sentry; | ||
import io.sentry.samples.android.databinding.ActivityCameraxBinding; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class CameraXActivity extends AppCompatActivity { | ||
private static final String TAG = "CameraXActivity"; | ||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 1001; | ||
|
||
private ActivityCameraxBinding binding; | ||
private PreviewView previewView; | ||
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture; | ||
private ImageCapture imageCapture; | ||
private Camera camera; | ||
private CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
binding = ActivityCameraxBinding.inflate(getLayoutInflater()); | ||
setContentView(binding.getRoot()); | ||
|
||
previewView = binding.previewView; | ||
|
||
if (allPermissionsGranted()) { | ||
startCamera(); | ||
} else { | ||
ActivityCompat.requestPermissions( | ||
this, new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE); | ||
} | ||
|
||
binding.captureButton.setOnClickListener(view -> takePhoto()); | ||
binding.switchCameraButton.setOnClickListener(view -> switchCamera()); | ||
binding.backButton.setOnClickListener(view -> finish()); | ||
} | ||
|
||
private void startCamera() { | ||
cameraProviderFuture = ProcessCameraProvider.getInstance(this); | ||
cameraProviderFuture.addListener( | ||
() -> { | ||
try { | ||
ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); | ||
bindPreview(cameraProvider); | ||
} catch (ExecutionException | InterruptedException e) { | ||
Log.e(TAG, "Error starting camera", e); | ||
Sentry.captureException(e); | ||
} | ||
}, | ||
ContextCompat.getMainExecutor(this)); | ||
} | ||
|
||
private void bindPreview(ProcessCameraProvider cameraProvider) { | ||
Preview preview = new Preview.Builder().build(); | ||
imageCapture = | ||
new ImageCapture.Builder() | ||
.setTargetRotation(previewView.getDisplay().getRotation()) | ||
.build(); | ||
|
||
preview.setSurfaceProvider(previewView.getSurfaceProvider()); | ||
|
||
cameraProvider.unbindAll(); | ||
camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture); | ||
} | ||
|
||
private void takePhoto() { | ||
if (imageCapture == null) return; | ||
|
||
String timeStamp = | ||
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); | ||
String fileName = "CameraX_" + timeStamp + ".jpg"; | ||
|
||
ContentValues contentValues = new ContentValues(); | ||
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); | ||
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg"); | ||
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Images"); | ||
|
||
ImageCapture.OutputFileOptions outputFileOptions = | ||
new ImageCapture.OutputFileOptions.Builder( | ||
getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) | ||
.build(); | ||
|
||
imageCapture.takePicture( | ||
outputFileOptions, | ||
ContextCompat.getMainExecutor(this), | ||
new ImageCapture.OnImageSavedCallback() { | ||
@Override | ||
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { | ||
String msg = "Photo saved successfully: " + outputFileResults.getSavedUri(); | ||
Toast.makeText(CameraXActivity.this, "Photo saved!", Toast.LENGTH_SHORT).show(); | ||
Log.d(TAG, msg); | ||
} | ||
|
||
@Override | ||
public void onError(@NonNull ImageCaptureException exception) { | ||
Log.e(TAG, "Photo capture failed", exception); | ||
Toast.makeText(CameraXActivity.this, "Photo capture failed", Toast.LENGTH_SHORT).show(); | ||
Sentry.captureException(exception); | ||
} | ||
}); | ||
} | ||
|
||
private void switchCamera() { | ||
cameraSelector = | ||
(cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA) | ||
? CameraSelector.DEFAULT_FRONT_CAMERA | ||
: CameraSelector.DEFAULT_BACK_CAMERA; | ||
|
||
try { | ||
if (cameraProviderFuture != null) { | ||
ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); | ||
bindPreview(cameraProvider); | ||
} | ||
} catch (ExecutionException | InterruptedException e) { | ||
Log.e(TAG, "Error switching camera", e); | ||
Sentry.captureException(e); | ||
} | ||
} | ||
|
||
private boolean allPermissionsGranted() { | ||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) | ||
== PackageManager.PERMISSION_GRANTED; | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult( | ||
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) { | ||
if (allPermissionsGranted()) { | ||
startCamera(); | ||
} else { | ||
Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show(); | ||
finish(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
binding = null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
sentry-samples/sentry-samples-android/src/main/res/layout/activity_camerax.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".CameraXActivity" | ||
> | ||
|
||
<androidx.camera.view.PreviewView | ||
android:id="@+id/previewView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
/> | ||
|
||
<Button | ||
android:id="@+id/captureButton" | ||
android:layout_width="80dp" | ||
android:layout_height="80dp" | ||
android:layout_marginBottom="32dp" | ||
android:background="@android:drawable/ic_menu_camera" | ||
android:text="" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
/> | ||
|
||
<Button | ||
android:id="@+id/switchCameraButton" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginEnd="16dp" | ||
android:layout_marginBottom="32dp" | ||
android:text="@string/switch_preview" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
/> | ||
|
||
<Button | ||
android:id="@+id/backButton" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="16dp" | ||
android:text="@string/back" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
/> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Camera Initialization Timing Issue
The
switchCamera()
method attempts to accesscameraProviderFuture
directly. This can lead to aNullPointerException
if called before camera permissions are granted andcameraProviderFuture
is initialized. Additionally, callingcameraProviderFuture.get()
synchronously on the main thread is a blocking operation that may cause ANRs.