4

I want to take photo with CameraX in service without any xml and PreviewView or something else for showing picture, But I have problem.

4 Answers4

0

You should use Camera2, this question is mistake

0

To access the camera you'll probably have to use a foreground service.

It isn't clear what you're trying to do with the camera from your question, but if you're trying to capture a photo, you'll need to set up the camera inside your service and use the ImageCapture use case.

Husayn Hakeem
  • 4,184
  • 1
  • 16
  • 31
0

The CameraX use cases (Preview, ImageAnalysis and ImageCapture) are all completely independent.

Bind only ImageCapture to the lifecycle without any Preview that requires PreviewView. It will work.

0

You may create a customer service to extend LifecyleService class like below: (cameraSelector and ImageCapture)

public class cameraService extends LifecycleService {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       
       RunCameraX();
       return super.onStartCommand(intent, flags, startId);
    }
    RunCameraX() {
       ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
       //select-usecase
       //Camera facing, image analysis & no preview
       Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector2,imageCapture );
     }

     imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback () {
            @Override
            public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        //todo
                    }
                });
            }
            @Override
            public void onError(@NonNull ImageCaptureException error) {
                error.printStackTrace();
            }
        });
}

Build.grade should have dependency declared,

 implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"

And declare this service in Manifest file

<service
        android:name=".cameraService"/>
Vidz
  • 406
  • 2
  • 4
  • 16