我想在服务中使用CameraX拍照,不需要任何XML和PreviewView或其他用于显示图片的东西,但是我遇到了问题。
我想在服务中使用CameraX拍照,不需要任何XML和PreviewView或其他用于显示图片的东西,但是我遇到了问题。
您可以创建一个客户服务来扩展LifecyleService类,如下所示:(cameraSelector和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.gradle 应该声明依赖项
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
并在清单文件中声明此服务
<service
android:name=".cameraService"/>