androidx.camera.core.Preview Java Examples

The following examples show how to use androidx.camera.core.Preview. 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: MainActivity.java    From journaldev with MIT License 7 votes vote down vote up
private Preview setPreview() {

        Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight());
        Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen


        PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
        Preview preview = new Preview(pConfig);

        preview.setOnPreviewOutputUpdateListener(
                new Preview.OnPreviewOutputUpdateListener() {
                    @Override
                    public void onUpdated(Preview.PreviewOutput output) {
                        ViewGroup parent = (ViewGroup) textureView.getParent();
                        parent.removeView(textureView);
                        parent.addView(textureView, 0);

                        textureView.setSurfaceTexture(output.getSurfaceTexture());
                        updateTransform();
                    }
                });

        return preview;
    }
 
Example #2
Source File: TextureViewImplementation.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
@Override
public Preview.SurfaceProvider getSurfaceProvider() {
    return (surfaceRequest) -> {
        mResolution = surfaceRequest.getResolution();
        initInternal();
        if (mSurfaceRequest != null) {
            mSurfaceRequest.willNotProvideSurface();
        }

        mSurfaceRequest = surfaceRequest;
        surfaceRequest.addRequestCancellationListener(
                ContextCompat.getMainExecutor(mTextureView.getContext()), () -> {
                    if (mSurfaceRequest != null && mSurfaceRequest == surfaceRequest) {
                        mSurfaceRequest = null;
                        mSurfaceReleaseFuture = null;
                    }
                });

        tryToProvidePreviewSurface();
    };
}
 
Example #3
Source File: ScannerView.java    From LPR with Apache License 2.0 6 votes vote down vote up
@SuppressLint("RestrictedApi")
private void initUseCase() {
    // 1. preview
    int screenAspectRatio = getPreviewRatio();
    int rotation = mPreviewView.getDisplay().getRotation();
    preview = new Preview.Builder()
            .setTargetAspectRatio(screenAspectRatio)
            .setTargetRotation(rotation)
            .build();

    imageAnalyzer = new ImageAnalysis.Builder()
            .setTargetAspectRatio(screenAspectRatio)
            .setTargetRotation(rotation)
            .build();

    imageAnalyzer.setAnalyzer(cameraExecutor, mCameraAnalyzer);
}
 
Example #4
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 #5
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
}
 
Example #6
Source File: SurfaceViewImplementation.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@NonNull
@Override
public Preview.SurfaceProvider getSurfaceProvider() {
    return mSurfaceProvider;
}
 
Example #7
Source File: PreviewView.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets the {@link Preview.SurfaceProvider} to be used with
 * {@link Preview#setSurfaceProvider(Executor, Preview.SurfaceProvider)}.
 */
@NonNull
public Preview.SurfaceProvider getPreviewSurfaceProvider() {
    return mImplementation.getSurfaceProvider();
}
 
Example #8
Source File: PreviewView.java    From mollyim-android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Gets the {@link Preview.SurfaceProvider} to be used with {@link Preview}.
 */
@NonNull
Preview.SurfaceProvider getSurfaceProvider();