0

I'm having trouble taking a photo of the current camera image. The camera image is displayed in the view and when a button is pressed, the photo should be saved or processed later on.

I'm working with the CameraX API and Android 12 (API level 31).


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
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 java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
 private PreviewView viewFinder;
 private ProcessCameraProvider cameraProvider;
 private Camera camera;
 private ImageCapture imageCapture;
 private static final int CAMERA_PERMISSION_CODE = 1;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 viewFinder = findViewById(R.id.viewFinder);
 startCamera();
 Button takePictureButton = findViewById(R.id.takePictureButton);
 takePictureButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 takePicture();
 }
 });
 }
 private void startCamera() {
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
 // Berechtigung wurde erteilt, öffnen Sie die Kamera.
 } else {
 // Fordern Sie die Berechtigung an.
 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
 }
 ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
 cameraProviderFuture.addListener(() -> {
 try {
 // Verfügbare Kamera-Provider abrufen
 cameraProvider = cameraProviderFuture.get();
 // Kamera-Vorschau konfigurieren und starten
 bindPreview();
 } catch (ExecutionException | InterruptedException e) {
 // handle error
 }
 }, ContextCompat.getMainExecutor(this));
 }
 private void bindPreview() {
 imageCapture =
 new ImageCapture.Builder()
 .setTargetRotation(viewFinder.getDisplay().getRotation())
 .build();
 Preview preview = new Preview.Builder().build();
 preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
 // 
 CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
 // bind
 camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture);
 }
 private void takePicture() {
 Executor cameraExecutor = ContextCompat.getMainExecutor(this);
 ImageCapture.OutputFileOptions outputFileOptions =
 new ImageCapture.OutputFileOptions.Builder(new File(getExternalMediaDirs()[0], "photo.jpg")).build();
 imageCapture.takePicture(outputFileOptions, cameraExecutor,
 new ImageCapture.OnImageSavedCallback() {
 @Override
 public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {
 System.out.println("SAVED");
 Toast.makeText(MainActivity.this, "Photo saved", Toast.LENGTH_SHORT).show();
 }
 @Override
 public void onError(ImageCaptureException error) {
 System.out.println("not saved");
 Toast.makeText(MainActivity.this, "Error saving photo", Toast.LENGTH_SHORT).show();
 System.out.println(error);
 }
 }
 );
 }
}

I am getting the following error, and the camera image freezes and restarts again.


/ImageCapture: Send image capture request [current, pending] = [0, 1]
D/ImageCapture: issueTakePicture
D/Camera2CameraImpl: {Camera@bcf4c18[id=0]} Issue capture request
D/CaptureSession: Issuing capture request.
D/EGL_emulation: app_time_stats: avg=20.36ms min=1.77ms max=477.02ms count=39
E/CameraCaptureSession: Session 0: Exception while stopping repeating: 
 android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): cancelRequest:507: Camera 0: Error clearing streaming request: Function not implemented (-38)
 at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:1179)
...

I am getting the following error, and the camera image freezes and restarts again.

asked Feb 23, 2023 at 12:52
2
  • Could you switch to the latest version 1.3.0-alpha03 and try again? Commented Feb 26, 2023 at 2:06
  • I use API31 of android, so i cannot use latest version. I changed the code now to ImageAnalyse because i wanted to implement a BarcodeScanner Commented Feb 27, 2023 at 9:31

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.