native_camera_view 0.0.3 copy "native_camera_view: ^0.0.3" to clipboard
native_camera_view: ^0.0.3 copied to clipboard

Native Camera View - Flutter Plugin

📷 Native Camera View - Flutter Plugin #

A Flutter plugin to display a native camera preview for Android and iOS, along with basic camera controls. The plugin uses AndroidView (Android) and UiKitView (iOS) to embed the native camera view into the Flutter widget tree.


✨ Features #

  • Native Camera Preview: View live camera feed directly from the device.

  • Android & iOS Support: Works reliably across both platforms.

  • Capture Image: Saves the image to the app's temporary/cache folder and returns the path.

  • Pause/Resume Camera:

    • iOS: Stops preview at the last frame and suspends session to save battery.
    • Android: Unbinds the PreviewUseCase but still allows capturing.
  • Switch Camera: Easily toggle between front and back camera.

  • Preview Fit Modes: cover, contain, fitWidth, fitHeight.

    • Android: Supports FIT_START (align top/left).
    • iOS: contain by default centers the preview.
  • Tap-to-Focus (Android only): Tap to focus on a specific area.

  • Delete Captured Images: Clear cached images saved by the plugin.


🚀 Installation Requirements #

iOS #

Add the following to ios/Runner/Info.plist:

<key>NSCameraUsageDescription</key>
<string>This app needs camera access to preview and capture photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to the photo library to save your photos.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs permission to add photos to your library.</string>

Android #

  • Minimum API Level: 21 (Android 5.0 - due to CameraX usage)

Add permissions to AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />

🛠️ How to Use #

1. Add Dependency #

dependencies:
  flutter:
    sdk: flutter
  native_camera_view: ^0.0.2 # Replace with the latest version

Run:

flutter pub get

2. Import Plugin #

import 'package:native_camera_view/native_camera_view.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:io';

3. Use CameraPreviewView #

Basic example with StatefulWidget:

CameraController? _cameraController;
bool _isPermissionGranted = false;
bool _isCameraPaused = false;
CameraPreviewFit _currentFit = CameraPreviewFit.cover;
bool _isFrontCameraSelected = false;

@override
void initState() {
  super.initState();
  _initializeCamera();
}

void _onCameraControllerCreated(CameraController controller) {
  _cameraController = controller;
  if (_isCameraPaused) {
    _cameraController?.pauseCamera();
  }
}

4. UI Widget #

CameraPreviewView(
  key: ValueKey("camera_\${_currentFit.name}_\${_isFrontCameraSelected}"),
  onCameraControllerCreated: _onCameraControllerCreated,
  currentFitMode: _currentFit,
  isFrontCameraSelected: _isFrontCameraSelected,
)

📦 CameraController API #

final path = await _cameraController?.captureImage(); // Capture photo
await _cameraController?.pauseCamera();               // Pause preview
await _cameraController?.resumeCamera();              // Resume preview
await _cameraController?.switchCamera(true);          // Switch to front camera
await _cameraController?.deleteAllCapturedPhotos();   // Delete cached images

⚙️ CameraPreviewView Parameters #

Parameter Type Description
key Key? Use ValueKey to force view rebuild
onCameraControllerCreated Function(CameraController) Required
currentFitMode CameraPreviewFit Default: cover
isFrontCameraSelected bool Use front camera: true
isCameraPausedParent bool? External pause state

🐞 Bug Reports & Contributions #

Please report issues or contribute at: 👉 https://github.com/nat0108/Native-camera-view