CameraView Android Library

repository·main·Indexed 26 days ago

https://github.com/natario1/cameraview

A high-level Android library for simplifying picture and video capture. It supports Camera1 and Camera2 engines, real-time filters, watermarks, and advanced controls via a feature-rich API and XML attributes. The library includes snapshot APIs for flipped front camera results, cropping, and overlays, as well as a CameraListener for handling asynchronous camera events and lifecycle management.

Tokens
17.9K
Snippets
57
Records
102
Agent score
88%

What's inside CameraView

  1. Declare required permissions for CameraView

    main

    CameraView requires android.permission.CAMERA for pictures and videos. While the library manifest declares the camera permission, you must manually declare android.permission.RECORD_AUDIO in your AndroidManifest.xml if you intend to record videos with Audio.ON (the default setting).

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  2. Use JitPack to pull latest snapshots or specific commits

    main

    If you want to use the latest changes before an official release, you can pull snapshots or specific commit hashes via JitPack in your build.gradle file.

    implementation 'com.github.natario1:CameraView:main-SNAPSHOT'
    implementation 'com.github.natario1:CameraView:<commit hash>'
  3. Add CameraView to your layout

    main

    To use the CameraView engine, add the CameraView component to your XML layout. It is designed to be hosted inside a UI component like a Fragment or Activity.

    <com.otaliastudios.cameraview.CameraView
        android:id="@+id/camera"
        android:keepScreenOn="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  4. Register the LifecycleOwner for CameraView

    main

    The camera component is bound to the host lifecycle. You should register the lifecycle owner as soon as possible to ensure proper management of camera resources.

    // For activities
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CameraView camera = findViewById(R.id.camera);
        camera.setLifecycleOwner(this);
    }
    
    // For fragments
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        CameraView camera = findViewById(R.id.camera);
        camera.setLifecycleOwner(getViewLifecycleOwner());
    }
  5. Tune Frame Processing threads and pool size

    main

    For slow processing tasks, you can increase the number of background threads allocated for frame processing using setFrameProcessingExecutors(int).

    Important: When increasing executors, you should also tune the setFrameProcessingPoolSize(int). The pool size represents the number of Frame instances that can exist simultaneously. A recommended pattern is to set the pool size to executors + 1.

    Example for two threads:

    cameraView.setFrameProcessingExecutors(2);
    cameraView.setFrameProcessingPoolSize(3);
    // Single threaded (default)
    cameraView.setFrameProcessingExecutors(1);
    cameraView.setFrameProcessingPoolSize(2);
    
    // Two threads
    cameraView.setFrameProcessingExecutors(2);
    cameraView.setFrameProcessingPoolSize(3);
  6. Control CameraView preview behavior using layout attributes

    main

    You can control how CameraView fills its container and handles aspect ratios using standard Android layout_width and layout_height attributes. The view is designed to never distort the preview; it will only crop if necessary.

    ValueMeaning
    WRAP_CONTENTThe view respects the aspect ratio and chooses dimensions to show the entire preview without cropping.
    MATCH_PARENTThe view fills the dimension. Parts of the content may be cropped to fit.
    Fixed values (e.g. 500dp)Behaves the same as MATCH_PARENT.
  7. Manage overlays at runtime

    main

    You can dynamically add, remove, or modify overlays using standard View methods and OverlayLayout.LayoutParams.

    • To add an overlay: Use addView(View, OverlayLayout.LayoutParams).
    • To remove an overlay: Use removeView(View).
    • To modify drawing flags: Cast the view's layout parameters to OverlayLayout.LayoutParams, update the boolean flags, and call setLayoutParams().
    // Add an overlay
    OverlayLayout.LayoutParams params = new OverlayLayout.LayoutParams();
    cameraView.addView(overlay, params);
    
    // Remove an overlay
    cameraView.removeView(overlay);
    
    // Modify drawing flags at runtime
    View overlay = findViewById(R.id.watermark);
    OverlayLayout.LayoutParams params = (OverlayLayout.LayoutParams) overlay.getLayoutParams();
    params.drawOnPreview = true;
    params.drawOnPictureSnapshot = true;
    params.drawOnVideoSnapshot = true;
    overlay.setLayoutParams(params);
  8. Handle runtime permissions in CameraView

    main

    For Android Marshmallow (API 23) and above, permissions must be granted at runtime. You have two options:

    1. Manual Handling: Manage permissions yourself and call open() or setLifecycleOwner() only after permissions are acquired.
    2. Automatic Handling: Let CameraView request permissions automatically. The library will present a request based on the current configuration.

    Note: Automatic requests are performed at the Activity level. Therefore, the onRequestPermissionResults() callback will be invoked on the parent Activity, not the Fragment.

  9. Manage CameraView lifecycle without support libraries

    main

    If you are not using support libraries and cannot resolve the LifecycleOwner interface, you must manually manage the camera lifecycle by overriding the host's lifecycle methods and calling open(), close(), and destroy().

    @Override
    protected void onResume() {
        super.onResume();
        cameraView.open();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        cameraView.close();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraView.destroy();
    }
  10. Use snapshot APIs to flip front camera results

    main
    When using the front camera, takePicture() and takeVideo() produce results that are not horizontally flipped (representing reality). If you need the captured media to match the horizontal orientation of the preview, use the snapshot APIs instead of the standard capture methods.