|
| 1 | +package io.sentry.samples.android; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.content.ContentValues; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.provider.MediaStore; |
| 8 | +import android.util.Log; |
| 9 | +import android.widget.Toast; |
| 10 | +import androidx.annotation.NonNull; |
| 11 | +import androidx.appcompat.app.AppCompatActivity; |
| 12 | +import androidx.camera.core.Camera; |
| 13 | +import androidx.camera.core.CameraSelector; |
| 14 | +import androidx.camera.core.ImageCapture; |
| 15 | +import androidx.camera.core.ImageCaptureException; |
| 16 | +import androidx.camera.core.Preview; |
| 17 | +import androidx.camera.lifecycle.ProcessCameraProvider; |
| 18 | +import androidx.camera.view.PreviewView; |
| 19 | +import androidx.core.app.ActivityCompat; |
| 20 | +import androidx.core.content.ContextCompat; |
| 21 | +import com.google.common.util.concurrent.ListenableFuture; |
| 22 | +import io.sentry.Sentry; |
| 23 | +import io.sentry.samples.android.databinding.ActivityCameraxBinding; |
| 24 | +import java.text.SimpleDateFormat; |
| 25 | +import java.util.Date; |
| 26 | +import java.util.Locale; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | + |
| 29 | +public class CameraXActivity extends AppCompatActivity { |
| 30 | + private static final String TAG = "CameraXActivity"; |
| 31 | + private static final int CAMERA_PERMISSION_REQUEST_CODE = 1001; |
| 32 | + |
| 33 | + private ActivityCameraxBinding binding; |
| 34 | + private PreviewView previewView; |
| 35 | + private ListenableFuture<ProcessCameraProvider> cameraProviderFuture; |
| 36 | + private ImageCapture imageCapture; |
| 37 | + private Camera camera; |
| 38 | + private CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA; |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void onCreate(Bundle savedInstanceState) { |
| 42 | + super.onCreate(savedInstanceState); |
| 43 | + binding = ActivityCameraxBinding.inflate(getLayoutInflater()); |
| 44 | + setContentView(binding.getRoot()); |
| 45 | + |
| 46 | + previewView = binding.previewView; |
| 47 | + |
| 48 | + if (allPermissionsGranted()) { |
| 49 | + startCamera(); |
| 50 | + } else { |
| 51 | + ActivityCompat.requestPermissions( |
| 52 | + this, new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE); |
| 53 | + } |
| 54 | + |
| 55 | + binding.captureButton.setOnClickListener(view -> takePhoto()); |
| 56 | + binding.switchCameraButton.setOnClickListener(view -> switchCamera()); |
| 57 | + binding.backButton.setOnClickListener(view -> finish()); |
| 58 | + } |
| 59 | + |
| 60 | + private void startCamera() { |
| 61 | + cameraProviderFuture = ProcessCameraProvider.getInstance(this); |
| 62 | + cameraProviderFuture.addListener( |
| 63 | + () -> { |
| 64 | + try { |
| 65 | + ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); |
| 66 | + bindPreview(cameraProvider); |
| 67 | + } catch (ExecutionException | InterruptedException e) { |
| 68 | + Log.e(TAG, "Error starting camera", e); |
| 69 | + Sentry.captureException(e); |
| 70 | + } |
| 71 | + }, |
| 72 | + ContextCompat.getMainExecutor(this)); |
| 73 | + } |
| 74 | + |
| 75 | + private void bindPreview(ProcessCameraProvider cameraProvider) { |
| 76 | + Preview preview = new Preview.Builder().build(); |
| 77 | + imageCapture = |
| 78 | + new ImageCapture.Builder() |
| 79 | + .setTargetRotation(previewView.getDisplay().getRotation()) |
| 80 | + .build(); |
| 81 | + |
| 82 | + preview.setSurfaceProvider(previewView.getSurfaceProvider()); |
| 83 | + |
| 84 | + cameraProvider.unbindAll(); |
| 85 | + camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture); |
| 86 | + } |
| 87 | + |
| 88 | + private void takePhoto() { |
| 89 | + if (imageCapture == null) return; |
| 90 | + |
| 91 | + String timeStamp = |
| 92 | + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); |
| 93 | + String fileName = "CameraX_" + timeStamp + ".jpg"; |
| 94 | + |
| 95 | + ContentValues contentValues = new ContentValues(); |
| 96 | + contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); |
| 97 | + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg"); |
| 98 | + contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Images"); |
| 99 | + |
| 100 | + ImageCapture.OutputFileOptions outputFileOptions = |
| 101 | + new ImageCapture.OutputFileOptions.Builder( |
| 102 | + getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) |
| 103 | + .build(); |
| 104 | + |
| 105 | + imageCapture.takePicture( |
| 106 | + outputFileOptions, |
| 107 | + ContextCompat.getMainExecutor(this), |
| 108 | + new ImageCapture.OnImageSavedCallback() { |
| 109 | + @Override |
| 110 | + public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { |
| 111 | + String msg = "Photo saved successfully: " + outputFileResults.getSavedUri(); |
| 112 | + Toast.makeText(CameraXActivity.this, "Photo saved!", Toast.LENGTH_SHORT).show(); |
| 113 | + Log.d(TAG, msg); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onError(@NonNull ImageCaptureException exception) { |
| 118 | + Log.e(TAG, "Photo capture failed", exception); |
| 119 | + Toast.makeText(CameraXActivity.this, "Photo capture failed", Toast.LENGTH_SHORT).show(); |
| 120 | + Sentry.captureException(exception); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + private void switchCamera() { |
| 126 | + cameraSelector = |
| 127 | + (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA) |
| 128 | + ? CameraSelector.DEFAULT_FRONT_CAMERA |
| 129 | + : CameraSelector.DEFAULT_BACK_CAMERA; |
| 130 | + |
| 131 | + try { |
| 132 | + if (cameraProviderFuture != null) { |
| 133 | + ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); |
| 134 | + bindPreview(cameraProvider); |
| 135 | + } |
| 136 | + } catch (ExecutionException | InterruptedException e) { |
| 137 | + Log.e(TAG, "Error switching camera", e); |
| 138 | + Sentry.captureException(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private boolean allPermissionsGranted() { |
| 143 | + return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) |
| 144 | + == PackageManager.PERMISSION_GRANTED; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onRequestPermissionsResult( |
| 149 | + int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 150 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 151 | + if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) { |
| 152 | + if (allPermissionsGranted()) { |
| 153 | + startCamera(); |
| 154 | + } else { |
| 155 | + Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show(); |
| 156 | + finish(); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + protected void onDestroy() { |
| 163 | + super.onDestroy(); |
| 164 | + binding = null; |
| 165 | + } |
| 166 | +} |
0 commit comments