-
-
Notifications
You must be signed in to change notification settings - Fork 461
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
Changes from 3 commits
d3f8605
84dfbed
21f2c53
08134d6
b80c619
74a9389
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,7 +75,11 @@ | |
| <activity android:name=".FrameDataForSpansActivity" | ||
| 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--> | ||
| <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" /> | ||
|
|
||
| <!-- how to enable Sentry's debug mode--> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| 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 { | ||
| ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); | ||
| bindPreview(cameraProvider); | ||
| } catch (ExecutionException | InterruptedException e) { | ||
| Log.e(TAG, "Error switching camera", e); | ||
| Sentry.captureException(e); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Camera Initialization Timing IssueThe
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
| } | ||
| } | ||
| 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> |
Uh oh!
There was an error while loading. Please reload this page.