# How to Build an Offline Flutter Document Scanner App with Auto Edge Detection for Android and iOS

Canonical: https://www.dynamsoft.com/codepool/flutter-document-scanner-app-guide.html
Markdown: https://www.dynamsoft.com/codepool/flutter-document-scanner-app-guide.md
Description: Step-by-step guide to building an offline Flutter document scanner for Android and iOS using Dynamsoft Capture Vision Flutter 3.6.2000. Covers auto edge detection, perspective correction, manual crop editing, color-mode switching, and PNG export.
Author: Xiao Ling
Published: 2025-05-13
Updated: 2026-11-09
Topics: Flutter, document-scanner, mobile, android, ios, image-processing, DCV

Use Dynamsoft Capture Vision Flutter **3.6.2000** to build **DocScan**, an offline Flutter document scanner for Android and iOS with on-device auto edge detection, perspective correction, manual quad-corner editing, color-mode switching, and PNG export. The [Dynamsoft Capture Vision SDK](https://www.dynamsoft.com/capture-vision/docs/introduction/) provides real-time document boundary detection, deskewing, and image-processing capabilities without a server.

**What you'll build:** A cross-platform Flutter document scanner for Android and iOS that performs offline, on-device auto edge detection, perspective correction, manual quad-corner editing, color-mode switching, and PNG export — using Dynamsoft Capture Vision Flutter 3.6.2000.

## Key Takeaways

- This tutorial uses Dynamsoft Capture Vision Flutter 3.6.2000 to build an offline document scanner for Android and iOS with no server-side processing.
- The `CaptureVisionRouter`, `CameraEnhancer`, and `CapturedResultReceiver` are the three core SDK objects that wire together camera input, document detection, and result delivery.
- Enabling `MultiFrameResultCrossFilter` with cross-verification eliminates unstable single-frame detections and ensures only confirmed document boundaries trigger capture.
- Manual corner adjustment is handled by `ImageEditorView`, letting users refine the auto-detected quadrilateral before exporting the final PNG — critical for low-contrast or imperfect lighting conditions.

## Common Developer Questions

### How do I integrate an offline document scanner SDK into a Flutter app for Android and iOS?

Add `dynamsoft_capture_vision_flutter: ^3.6.2000`, configure Android and iOS camera permissions, initialize the license once, and then connect `CameraEnhancer`, `CaptureVisionRouter`, and a result receiver inside your scan page. That gives you an on-device document scanner without any server round-trip.

### How do I prevent false detections when the Flutter document scanner picks up table edges or background patterns?

Enable `MultiFrameResultCrossFilter` and turn on cross-verification so the SDK only accepts boundaries that remain consistent across consecutive frames. The tutorial calls this out as the main defense against single-frame false positives from structured backgrounds.

### Why does `getOriginalImage()` return null after calling `stopCapturing()` in Flutter document scanning?

Because the router clears its intermediate result buffer when capture stops, you must fetch the original image before calling `stopCapturing()`. The article explicitly reorders those calls so `getOriginalImage(result.originalImageHashId)` runs while the result is still available.



## Demo Video: Flutter Document Scanner

<video src="https://github.com/user-attachments/assets/3d730b36-ca18-48b5-bb02-ea58dd2c5115" controls="controls" muted="muted" style="max-height:640px; min-height: 200px; max-width: 100%;"></video>

![document scanner result page](./img/2026/03/flutter-document-scanner-app-android-ios.jpg)


## Tested Environment

The sample was checked with static analysis only: `flutter analyze --no-pub` against [sample commit `713fcc4`](https://github.com/yushulx/flutter-barcode-mrz-document-scanner/tree/713fcc462c680878c532248c30505e6e9d1297eb) using Flutter 3.47.2 / Dart 3.13.2. No Android or iOS build and no device run were performed, so live document detection, normalization and platform permissions remain unverified.

## Prerequisites

- Flutter SDK ≥ 3.8
- Dart SDK ≥ 3.8
- Android Studio or VS Code Latest
- Xcode (for iOS builds) Latest
- [Get a 30-day free trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) for the Dynamsoft Capture Vision SDK to test the scanner on a real device.


## Understand the Project Structure Before You Start

Before diving into code, it is helpful to understand the final file layout:

```
lib/
├── main.dart          # App entry point, Material 3 theme, home screen
├── scan_page.dart     # Live camera + real-time document detection
├── result_page.dart   # Preview, colour-mode switching, PNG export
├── edit_page.dart     # Manual quad-corner drag adjustment
├── constants.dart     # App-wide constants (license key, padding, etc.)
└── app_theme.dart     # Centralised Material 3 light/dark theme
```

Each Dart file has a single, clearly scoped responsibility. This separation makes unit testing and future feature additions straightforward.


## Step 1: Create the Flutter Project and Add the Capture Vision Package

Start by creating a new Flutter project:

```bash
flutter create --org com.dynamsoft.flutter scan_document
cd scan_document
```

Open `pubspec.yaml` and add the two required packages:

```yaml
dependencies:
  flutter:
    sdk: flutter
  dynamsoft_capture_vision_flutter: ^3.6.2000
  path_provider: ^2.1.5
```

Run `flutter pub get` to fetch them.


## Step 2: Configure Camera Permissions for Android and iOS

### Configure Android Camera Permissions

Open `android/app/src/main/AndroidManifest.xml` and add permissions **before** the `<application>` tag:

```xml
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />
```

Because the Dynamsoft Capture Vision SDK requires API 21 or higher, update `android/app/build.gradle.kts`:

```kotlin
defaultConfig {
    applicationId = "com.dynamsoft.flutter.scan_document"
    minSdk = 21
    targetSdk = flutter.targetSdkVersion
    versionCode = flutter.versionCode
    versionName = flutter.versionName
}
```

### Configure iOS Camera Permissions

Add camera and photo-library usage descriptions to `ios/Runner/Info.plist`:

```xml
<key>NSCameraUsageDescription</key>
<string>DocScan needs camera access to scan documents.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>DocScan saves scanned documents to your photo library.</string>
```


## Step 3: Centralize App Configuration with constants.dart

Hardcoding the license key directly into the scanner widget makes it difficult to rotate keys or run CI builds with a different key. Create `lib/constants.dart`:

```dart
class AppConstants {
  AppConstants._();

  /// Replace with your own license from https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
  static const String licenseKey = 'YOUR_LICENSE_KEY_HERE';

  static const String appName = 'DocScan';
  static const double defaultPadding = 16.0;
  static const double fabSize = 64.0;
  static const Duration snackBarDuration = Duration(seconds: 4);
  static const String exportFilePrefix = 'docscan_';
  static const String exportFileExtension = '.png';
}
```


## Step 4: Define the Material 3 App Theme in app_theme.dart

Material 3 provides a robust theming system. Create `lib/app_theme.dart` that returns both a light and a dark `ThemeData`:

```dart
import 'package:flutter/material.dart';
import 'constants.dart';

class AppTheme {
  AppTheme._();

  static const Color primaryColor = Color(0xFFFF6D00);

  static ThemeData get light {
    final base = ColorScheme.fromSeed(
      seedColor: primaryColor,
      brightness: Brightness.light,
    );
    return ThemeData(
      useMaterial3: true,
      colorScheme: base,
      appBarTheme: AppBarTheme(
        backgroundColor: base.primary,
        foregroundColor: base.onPrimary,
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: primaryColor,
          foregroundColor: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 14),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8)),
        ),
      ),
    );
  }

  static ThemeData get dark {
    final base = ColorScheme.fromSeed(
      seedColor: primaryColor,
      brightness: Brightness.dark,
    );
    return ThemeData(useMaterial3: true, colorScheme: base);
  }
}
```


## Step 5: Build the Home Screen in main.dart

The entry point locks the app to portrait mode and renders a feature-list home screen:

```dart
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  runApp(const DocScanApp());
}

class DocScanApp extends StatelessWidget {
  const DocScanApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: AppConstants.appName,
      debugShowCheckedModeBanner: false,
      theme: AppTheme.light,
      darkTheme: AppTheme.dark,
      themeMode: ThemeMode.system,
      navigatorObservers: [routeObserver],
      home: const HomeScreen(),
    );
  }
}
```

The `RouteObserver` is a global singleton that the `ScannerPage` subscribes to. This enables the camera to pause automatically when another screen is pushed on top of it and resume when that screen is popped — a common pitfall that causes battery drain and camera resource conflicts if not handled.


## Step 6: Implement the Live Camera Scanner Page

The scanner page is the heart of the application. It wires together three SDK objects:

- **`CameraEnhancer`** — manages the device camera stream
- **`CaptureVisionRouter`** — runs the document-detection algorithm
- **`CapturedResultReceiver`** — receives detection results asynchronously

### Initialize the SDK and Camera

```dart
Future<void> _initSdk() async {
  _cvr = CaptureVisionRouter()
    ..addResultFilter(
      MultiFrameResultCrossFilter()
        ..enableResultCrossVerification(
          EnumCapturedResultItemType.deskewedImage.value,
          true,
        ),
    );

  _receiver = CapturedResultReceiver()
    ..onProcessedDocumentResultReceived = _onResult;

  final (isSuccess, message) =
      await LicenseManager.initLicense(AppConstants.licenseKey);
  if (!isSuccess) { /* show error */ return; }

  await _cvr.setInput(_camera);
  _cvr.addResultReceiver(_receiver);
  await _camera.open();
  await _cvr.startCapturing(EnumPresetTemplate.detectAndNormalizeDocument);
}
```

The `MultiFrameResultCrossFilter` is critical for a good user experience. Without it, the receiver fires on every frame, including unstable or blurry frames. Enabling `enableResultCrossVerification` causes the SDK to wait until the same document boundary is confirmed across multiple consecutive frames before firing the callback.

### Handle Captured Document Results

```dart
Future<void> _onResult(ProcessedDocumentResult result) async {
  if (_isProcessing) return;
  final item = result.deskewedImageResultItems?.firstOrNull;
  if (item == null) return;

  final bool autoVerified =
      item.crossVerificationStatus == EnumCrossVerificationStatus.passed;
  if (!autoVerified && !_isCapturing) return;

  _isCapturing = false;
  _isProcessing = true;

  // Fetch intermediate image BEFORE stopping capture
  final originalImage = await _cvr
      .getIntermediateResultManager()
      .getOriginalImage(result.originalImageHashId);

  await _cvr.stopCapturing();
  _camera.close();

  if (mounted && item.imageData != null && originalImage != null) {
    await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) => ResultPage(
          deskewedImage: item.imageData!,
          originalImage: originalImage,
          sourceDeskewQuad: item.sourceDeskewQuad,
        ),
      ),
    );
  }
  _isProcessing = false;
}
```

> **Important:** `getOriginalImage()` must be called **before** `stopCapturing()`. Once capturing stops the intermediate result buffer is cleared.

### Manage Camera Lifecycle with RouteAware

Subscribe to `RouteAware` callbacks to pause and resume the camera automatically:

```dart
@override
void didPushNext() {
  _cvr.stopCapturing();
  _camera.close();
}

@override
void didPopNext() {
  _isProcessing = false;
  _camera.open();
  _cvr.startCapturing(EnumPresetTemplate.detectAndNormalizeDocument);
}

@override
void dispose() {
  routeObserver.unsubscribe(this);
  _cvr.stopCapturing();
  _camera.close();
  _cvr.removeResultReceiver(_receiver);
  _cvr.removeAllResultFilters();
  super.dispose();
}
```

### Build the Scanner UI

The camera view fills the screen. A gradient overlay at the bottom contains the capture button:

```dart
Stack(
  fit: StackFit.expand,
  children: [
    CameraView(cameraEnhancer: _camera),
    Positioned(
      left: 0, right: 0, bottom: 0,
      child: /* gradient + capture button */,
    ),
  ],
)
```


## Step 7: Build the Document Result Preview Page

Once a document is captured, `ResultPage` shows a preview and lets the user:

1. Switch between **color**, **grayscale**, and **binary (B&W)** modes
2. Go back to **EditPage** to re-adjust the crop
3. **Export** the final image as a PNG

### Apply Color Mode Conversion

```dart
Future<void> _changeColourMode(_ColourMode mode) async {
  setState(() => _isConverting = true);
  try {
    ImageData? converted;
    switch (mode) {
      case _ColourMode.colour:
        converted = _deskewedColorfulImage;
      case _ColourMode.grayscale:
        converted = await ImageProcessor().convertToGray(_deskewedColorfulImage);
      case _ColourMode.binary:
        converted = await ImageProcessor()
            .convertToBinaryLocal(_deskewedColorfulImage, compensation: 15);
    }
    if (mounted && converted != null) {
      setState(() { _showingImage = converted!; _currentMode = mode; });
    }
  } finally {
    if (mounted) setState(() => _isConverting = false);
  }
}
```

The `compensation` parameter in `convertToBinaryLocal` controls the threshold offset. A value of `15` works well for most printed documents scanned under ambient lighting. In Capture Vision Flutter 3.4, the default `compensation` is `10`, and `convertToBinaryLocal` accepts color or binary images as well as grayscale.

### Export the Scanned Document as PNG

```dart
Future<void> _exportImage() async {
  final directory = Platform.isAndroid
      ? (await getExternalStorageDirectory())!
      : await getApplicationDocumentsDirectory();

  final filePath =
      '${directory.path}/${AppConstants.exportFilePrefix}'
      '${DateTime.now().millisecondsSinceEpoch}'
      '${AppConstants.exportFileExtension}';

  await ImageIO().saveToFile(_showingImage, filePath, true);
}
```


## Step 8: Build the Manual Crop Edit Page

`ImageEditorView` from the SDK renders the original camera frame and overlays a draggable quadrilateral that the user can reposition:

```dart
ImageEditorView(
  imageData: widget.originalImageData,
  drawingQuadsByLayer: {
    EnumDrawingLayerId.ddn: [widget.quad],
  },
  onPlatformViewCreated: (controller) {
    _controller = controller;
  },
),
```

Version 3.6.2000 also improved `ImageEditorView`: the corner adjustment points are now visible as soon as the view renders. Earlier releases required a tap on the image before the handles appeared, which users often read as a broken editor.

When the user confirms, `getSelectedQuad()` returns the adjusted corners, and `cropAndDeskewImage()` re-processes the original image with the new quadrilateral:

```dart
final selectedQuad = await _controller!.getSelectedQuad();
final croppedImageData = await ImageProcessor()
    .cropAndDeskewImage(widget.originalImageData, selectedQuad);
Navigator.pop(context, {
  'croppedImageData': croppedImageData,
  'updatedQuad': selectedQuad,
});
```


## Step 9: Build and Run on Device

### Run a Debug Build on a Connected Android Device

```bash
flutter devices          # find your device ID
flutter run -d <id>
```

### Build a Release APK

```bash
flutter build apk --release
```

The compiled APK is at `build/app/outputs/flutter-apk/app-release.apk`.

## Common Issues & Edge Cases

- **Camera freezes when returning from the result screen:** Ensure `didPopNext()` calls both `_camera.open()` and `_cvr.startCapturing()`. The most common root cause is forgetting to subscribe to `RouteObserver` or calling only one of the two methods.
- **`getOriginalImage()` returns `null`:** This method must be called *before* `stopCapturing()`. Once capturing stops, the intermediate result buffer is cleared. Reorder calls so `getOriginalImage()` is invoked with the valid `originalImageHashId` before you stop the router.
- **Document boundary detected on table edges or background patterns:** Enable `MultiFrameResultCrossFilter` with `enableResultCrossVerification` set to `true`. Only boundaries confirmed across multiple consecutive frames will fire the result callback, filtering out single-frame false positives caused by structured backgrounds.

## Source Code

[Get the complete sample project source code on GitHub](https://github.com/yushulx/flutter-barcode-mrz-document-scanner/tree/main/examples/dynamsoft_document)
