androidx.camera.core.CameraSelector Java Examples
The following examples show how to use
androidx.camera.core.CameraSelector.
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: CameraXModule.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
public void toggleCamera() { // TODO(b/124269166): Rethink how we can handle permissions here. @SuppressLint("MissingPermission") Set<Integer> availableCameraLensFacing = getAvailableCameraLensFacing(); if (availableCameraLensFacing.isEmpty()) { return; } if (mCameraLensFacing == null) { setCameraLensFacing(availableCameraLensFacing.iterator().next()); return; } if (mCameraLensFacing == CameraSelector.LENS_FACING_BACK && availableCameraLensFacing.contains(CameraSelector.LENS_FACING_FRONT)) { setCameraLensFacing(CameraSelector.LENS_FACING_FRONT); return; } if (mCameraLensFacing == CameraSelector.LENS_FACING_FRONT && availableCameraLensFacing.contains(CameraSelector.LENS_FACING_BACK)) { setCameraLensFacing(CameraSelector.LENS_FACING_BACK); return; } }
Example #2
Source File: CameraXModule.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
@RequiresPermission(permission.CAMERA) private Set<Integer> getAvailableCameraLensFacing() { // Start with all camera directions Set<Integer> available = new LinkedHashSet<>(Arrays.asList(LensFacingConverter.values())); // If we're bound to a lifecycle, remove unavailable cameras if (mCurrentLifecycle != null) { if (!hasCameraWithLensFacing(CameraSelector.LENS_FACING_BACK)) { available.remove(CameraSelector.LENS_FACING_BACK); } if (!hasCameraWithLensFacing(CameraSelector.LENS_FACING_FRONT)) { available.remove(CameraSelector.LENS_FACING_FRONT); } } return available; }
Example #3
Source File: ScannerView.java From LPR with Apache License 2.0 | 6 votes |
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 #4
Source File: PublishFragment.java From lbry-android with MIT License | 6 votes |
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: CameraXSelfieFlashHelper.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
private boolean shouldUseViewBasedFlash() { Integer cameraLensFacing = camera.getCameraLensFacing(); return camera.getFlash() == ImageCapture.FLASH_MODE_ON && !camera.hasFlash() && cameraLensFacing != null && cameraLensFacing == CameraSelector.LENS_FACING_BACK; }
Example #6
Source File: CameraXUtil.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
public static int toCameraDirectionInt(int facing) { if (facing == CameraSelector.LENS_FACING_FRONT) { return Camera.CameraInfo.CAMERA_FACING_FRONT; } else { return Camera.CameraInfo.CAMERA_FACING_BACK; } }
Example #7
Source File: CameraXUtil.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
public static int toLensFacing(@CameraSelector.LensFacing int cameraDirectionInt) { if (cameraDirectionInt == Camera.CameraInfo.CAMERA_FACING_FRONT) { return CameraSelector.LENS_FACING_FRONT; } else { return CameraSelector.LENS_FACING_BACK; } }
Example #8
Source File: CameraXModule.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
@RequiresPermission(permission.CAMERA) public boolean hasCameraWithLensFacing(@CameraSelector.LensFacing int lensFacing) { String cameraId; try { cameraId = CameraX.getCameraWithLensFacing(lensFacing); } catch (Exception e) { throw new IllegalStateException("Unable to query lens facing.", e); } return cameraId != null; }
Example #9
Source File: CameraXFragment.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
private void onCaptureClicked() { Stopwatch stopwatch = new Stopwatch("Capture"); CameraXSelfieFlashHelper flashHelper = new CameraXSelfieFlashHelper( requireActivity().getWindow(), camera, selfieFlash ); camera.takePicture(Executors.mainThreadExecutor(), new ImageCapture.OnImageCapturedCallback() { @Override public void onCaptureSuccess(@NonNull ImageProxy image) { flashHelper.endFlash(); SimpleTask.run(CameraXFragment.this.getViewLifecycleOwner().getLifecycle(), () -> { stopwatch.split("captured"); try { return CameraXUtil.toJpeg(image, camera.getCameraLensFacing() == CameraSelector.LENS_FACING_FRONT); } catch (IOException e) { return null; } finally { image.close(); } }, result -> { stopwatch.split("transformed"); stopwatch.stop(TAG); if (result != null) { controller.onImageCaptured(result.getData(), result.getWidth(), result.getHeight()); } else { controller.onCameraError(); } }); } @Override public void onError(ImageCaptureException exception) { flashHelper.endFlash(); controller.onCameraError(); } }); flashHelper.startFlash(); }
Example #10
Source File: CameraXView.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
private void init(Context context, @Nullable AttributeSet attrs) { addView(mPreviewView = new PreviewView(getContext()), 0 /* view position */); mCameraModule = new CameraXModule(this); if (attrs != null) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraXView); setScaleType( ScaleType.fromId( a.getInteger(R.styleable.CameraXView_scaleType, getScaleType().getId()))); setPinchToZoomEnabled( a.getBoolean( R.styleable.CameraXView_pinchToZoomEnabled, isPinchToZoomEnabled())); setCaptureMode( CaptureMode.fromId( a.getInteger(R.styleable.CameraXView_captureMode, getCaptureMode().getId()))); int lensFacing = a.getInt(R.styleable.CameraXView_lensFacing, LENS_FACING_BACK); switch (lensFacing) { case LENS_FACING_NONE: setCameraLensFacing(null); break; case LENS_FACING_FRONT: setCameraLensFacing(CameraSelector.LENS_FACING_FRONT); break; case LENS_FACING_BACK: setCameraLensFacing(CameraSelector.LENS_FACING_BACK); break; default: // Unhandled event. } int flashMode = a.getInt(R.styleable.CameraXView_flash, 0); switch (flashMode) { case FLASH_MODE_AUTO: setFlash(ImageCapture.FLASH_MODE_AUTO); break; case FLASH_MODE_ON: setFlash(ImageCapture.FLASH_MODE_ON); break; case FLASH_MODE_OFF: setFlash(ImageCapture.FLASH_MODE_OFF); break; default: // Unhandled event. } a.recycle(); } if (getBackground() == null) { setBackgroundColor(0xFF111111); } mPinchToZoomGestureDetector = new PinchToZoomGestureDetector(context); }
Example #11
Source File: CameraXView.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
/** * Focus the position of the touch event, or focus the center of the preview for * accessibility events */ @Override public boolean performClick() { super.performClick(); final float x = (mUpEvent != null) ? mUpEvent.getX() : getX() + getWidth() / 2f; final float y = (mUpEvent != null) ? mUpEvent.getY() : getY() + getHeight() / 2f; mUpEvent = null; CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing( mCameraModule.getLensFacing()).build(); DisplayOrientedMeteringPointFactory pointFactory = new DisplayOrientedMeteringPointFactory( getDisplay(), cameraSelector, mPreviewView.getWidth(), mPreviewView.getHeight()); float afPointWidth = 1.0f / 6.0f; // 1/6 total area float aePointWidth = afPointWidth * 1.5f; MeteringPoint afPoint = pointFactory.createPoint(x, y, afPointWidth); MeteringPoint aePoint = pointFactory.createPoint(x, y, aePointWidth); Camera camera = mCameraModule.getCamera(); if (camera != null) { ListenableFuture<FocusMeteringResult> future = camera.getCameraControl().startFocusAndMetering( new FocusMeteringAction.Builder(afPoint, FocusMeteringAction.FLAG_AF).addPoint(aePoint, FocusMeteringAction.FLAG_AE).build()); Futures.addCallback(future, new FutureCallback<FocusMeteringResult>() { @Override public void onSuccess(@Nullable FocusMeteringResult result) { } @Override public void onFailure(Throwable t) { // Throw the unexpected error. throw new RuntimeException(t); } }, CameraXExecutors.directExecutor()); } else { Log.d(TAG, "cannot access camera"); } return true; }
Example #12
Source File: ScannerOptions.java From LPR with Apache License 2.0 | 4 votes |
public CameraSelector getCameraFacing() { return cameraFacing; }
Example #13
Source File: CameraXView.java From mollyim-android with GNU General Public License v3.0 | 2 votes |
/** * Queries whether the current device has a camera with the specified direction. * * @return True if the device supports the direction. * @throws IllegalStateException if the CAMERA permission is not currently granted. */ @RequiresPermission(permission.CAMERA) public boolean hasCameraWithLensFacing(@CameraSelector.LensFacing int lensFacing) { return mCameraModule.hasCameraWithLensFacing(lensFacing); }
Example #14
Source File: ScannerOptions.java From LPR with Apache License 2.0 | 2 votes |
/** * 设置扫描摄像头,默认后置 * * @param cameraFacing * @return */ public Builder setCameraFacing(CameraSelector cameraFacing) { options.cameraFacing = cameraFacing; return this; }