androidx.camera.lifecycle.ProcessCameraProvider Java Examples

The following examples show how to use androidx.camera.lifecycle.ProcessCameraProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScannerView.java    From LPR with Apache License 2.0 6 votes vote down vote up
public void initCamera() {
    // 获取 ProcessCameraProvider
    ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(getContext());
    cameraProviderFuture.addListener(() -> {
        // 初始化 UseCase
        initUseCase();
        try {
            ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
            cameraProvider.unbindAll();

            // 绑定 UseCase 到相机
            camera = cameraProvider.bindToLifecycle((LifecycleOwner) getContext(), CameraSelector.DEFAULT_BACK_CAMERA, preview, imageAnalyzer);

            // 开始预览
            preview.setSurfaceProvider(mPreviewView.createSurfaceProvider());
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }, ContextCompat.getMainExecutor(getContext()));
}
 
Example #2
Source File: PublishFragment.java    From lbry-android with MIT License 6 votes vote down vote up
private void displayPreviewWithCameraX() {
    Context context = getContext();
    if (context != null && MainActivity.hasPermission(Manifest.permission.CAMERA, context)) {
        cameraProviderFuture = ProcessCameraProvider.getInstance(context);
        cameraProviderFuture.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                    if (cameraProvider != null) {
                        Preview preview = new Preview.Builder().build();
                        CameraSelector cameraSelector = new CameraSelector.Builder()
                                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                                .build();

                        Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, preview);
                        preview.setSurfaceProvider(cameraPreview.createSurfaceProvider(camera.getCameraInfo()));
                        cameraPreviewInitialized = true;
                    }
                } catch (ExecutionException | IllegalArgumentException | InterruptedException ex) {
                    // pass
                }
            }
        }, ContextCompat.getMainExecutor(context));
    }
}
 
Example #3
Source File: CameraXModule.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
CameraXModule(CameraXView view) {
  mCameraXView = view;

  Futures.addCallback(ProcessCameraProvider.getInstance(view.getContext()),
      new FutureCallback<ProcessCameraProvider>() {
        // TODO(b/124269166): Rethink how we can handle permissions here.
        @SuppressLint("MissingPermission")
        @Override
        public void onSuccess(@Nullable ProcessCameraProvider provider) {
          Preconditions.checkNotNull(provider);
          mCameraProvider = provider;
          if (mCurrentLifecycle != null) {
            bindToLifecycle(mCurrentLifecycle);
          }
        }

        @Override
        public void onFailure(Throwable t) {
          throw new RuntimeException("CameraX failed to initialize.", t);
        }
      }, CameraXExecutors.mainThreadExecutor());

  mPreviewBuilder = new Preview.Builder().setTargetName("Preview");

  mImageCaptureBuilder = new ImageCapture.Builder().setTargetName("ImageCapture");

  // Begin Signal Custom Code Block
  mVideoCaptureConfigBuilder =
      new VideoCaptureConfig.Builder().setTargetName("VideoCapture")
                                      .setAudioBitRate(VideoUtil.AUDIO_BIT_RATE)
                                      .setVideoFrameRate(VideoUtil.VIDEO_FRAME_RATE)
                                      .setBitRate(VideoUtil.VIDEO_BIT_RATE);
  // End Signal Custom Code Block
}