edge_detection Flutter Plugin

repository·master·Indexed 43 days ago

https://github.com/sawankumarbundelkhandi/edge_detection

A Flutter plugin for object edge detection, paper scanning, corner detection, and rectangle detection. It provides a UI for scanning and cropping images, returning the processed result's file path. The plugin integrates WeScan for iOS and provides MethodChannel APIs for Android to handle live camera scanning via EdgeDetection.detectEdge and gallery selection via EdgeDetection.detectEdgeFromGallery.

Tokens
3.8K
Snippets
13
Records
16
Agent score
54%

What's inside edge_detection

  1. Configure Android for edge_detection

    master

    Ensure your Android project meets these requirements:

    1. Kotlin Version: Set kotlin_version to 1.8.0 in your android/build.gradle file.
    2. Minimum SDK: Set minSdkVersion to 21 or higher in your android/app/build.gradle file.
    ext.kotlin_version = '1.8.0'
    
    // In android/app/build.gradle
    minSdkVersion 21
  2. Use WeScan in Swift

    master

    To implement scanning in Swift, follow these steps:

    1. Import the framework: import WeScan.
    2. Conform your view controller to the ImageScannerControllerDelegate protocol.
    3. Implement the required delegate methods to handle success, failure, and cancellation.
    4. Initialize and present the ImageScannerController.

    Note: You are responsible for dismissing the ImageScannerController within the delegate methods.

    ```swift
    import WeScan
    
    class YourViewController: UIViewController, ImageScannerControllerDelegate {
        
        // Implement delegate functions
        func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
            print(error)
        }
    
        func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
            // Handle results and dismiss
            scanner.dismiss(animated: true)
        }
    
        func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
            // Dismiss on cancel
            scanner.dismiss(animated: true)
        }
    
        // Trigger the scanner
        func startScanning() {
            let scannerViewController = ImageScannerController()
            scannerViewController.imageScannerDelegate = self
            present(scannerViewController, animated: true)
        }
    }
  3. Configure iOS for edge_detection

    master

    To use the plugin on iOS, follow these requirements:

    1. Minimum Version: Set the minimum platform version to 13.0 or higher in your ios/Podfile.
    2. Permissions: Configure permission_handler in your ios/Podfile to allow camera and photo access:
    post_install do |installer|
      installer.pods_project.targets.each do |target|
        flutter_additional_ios_build_settings(target)
        target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
            '$(inherited)',
            'PERMISSION_CAMERA=1',
            'PERMISSION_PHOTOS=1',
          ]
        end
      end
    end
    1. Info.plist Permissions: Add the following keys to ios/Runner/Info.plist with appropriate usage descriptions:
    • NSCameraUsageDescription (Privacy - Camera Usage Description)
    • NSPhotoLibraryUsageDescription (Privacy - Photo Library Usage Description)
    • NSPhotoLibraryAddUsageDescription (Privacy - Photo Library Additions Usage Description)
    1. Xcode 15 Fix: If building with Xcode 15, add the following line to your ios/Podfile inside the target 'Runner' do block:
    pod 'WeScan', :path => '.symlinks/plugins/edge_detection/ios/WeScan-3.0.0'
    1. Localization: You may need to add localizations via Xcode to localize action buttons from the underlying WeScan library.
    post_install do |installer|
      installer.pods_project.targets.each do |target|
        flutter_additional_ios_build_settings(target)
        target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
            '$(inherited)',
            'PERMISSION_CAMERA=1',
            'PERMISSION_PHOTOS=1',
          ]
        end
      end
    end
  4. Use WeScan in Objective-C

    master

    To use WeScan in an Objective-C project, you must use a bridging header because the framework is written in Swift:

    1. Create a dummy Swift class in your project to trigger the creation of a bridging header.
    2. In the bridging header, import the WeScan header.
    3. In your Objective-C class, import your project's generated Swift header: #import <yourProjectName.swift.h>.
    4. Drag and drop the WeScan folder into your project.
    5. Use @Class ImageScannerController; in your header or implementation to reference the class.
    6. Instantiate and present the controller.
    // In your class
    @Class ImageScannerController;
    
    // Implementation
    ImageScannerController *scannerViewController = [[ImageScannerController alloc] init];
    [self presentViewController:scannerViewController animated:YES completion:nil];
  5. Install edge_detection and dependencies

    master

    Add the following dependencies to your pubspec.yaml file. Ensure you check for the latest versions before installation.

    dependencies:
      flutter:
        sdk: flutter
      edge_detection: ^1.1.3
      permission_handler: ^10.0.0
      path_provider: ^2.0.11
      path: ^1.8.2
    dependencies:
      flutter:
        sdk: flutter
      edge_detection: ^1.1.3
      permission_handler: ^10.0.0
      path_provider: ^2.0.11
      path: ^1.8.2
  6. Customize the iOS launch screen assets

    master

    To change the launch screen image for the iOS version of the application, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open the iOS project workspace using the command: open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch images.
    open ios/Runner.xcworkspace
  7. Use EdgeDetection.detectEdge for live camera scanning

    master

    Use EdgeDetection.detectEdge to start a live camera session that allows edge detection and an option to select from the gallery.

    Note: You must first request camera permissions using permission_handler and generate a valid file path where the cropped image will be saved.

    import 'package:edge_detection/edge_detection.dart';
    
    // 1. Request permissions
    bool isCameraGranted = await Permission.camera.request().isGranted;
    if (!isCameraGranted) {
        isCameraGranted = await Permission.camera.request() == PermissionStatus.granted;
    }
    
    if (!isCameraGranted) return;
    
    // 2. Generate filepath
    String imagePath = join((await getApplicationSupportDirectory()).path,
        "${(DateTime.now().millisecondsSinceEpoch / 1000).round()}.jpeg");
    
    // 3. Detect edges
    try {
        bool success = await EdgeDetection.detectEdge(imagePath,
            canUseGallery: true,
            androidScanTitle: 'Scanning',
            androidCropTitle: 'Crop',
            androidCropBlackWhiteTitle: 'Black White',
            androidCropReset: 'Reset',
        );
    } catch (e) {
        print(e);
    }
  8. ImageScannerControllerDelegate

    master

    The ImageScannerControllerDelegate protocol is used to receive updates from the scanning interface. It provides three main callbacks:

    • imageScannerController(_:didFailWithError:): Called when a scanning error occurs. You must handle the error.
    • imageScannerController(_:didFinishScanningWithResults:): Called when a scan is successful. The results are provided in an ImageScannerResults object. You are responsible for dismissing the scanner.
    • imageScannerControllerDidCancel(_:): Called when the user cancels the scanning process. You are responsible for dismissing the scanner.
  9. Use EdgeDetection.detectEdgeFromGallery for gallery selection

    master

    Use EdgeDetection.detectEdgeFromGallery to allow users to select an image directly from their gallery for edge detection and cropping.

    import 'package:edge_detection/edge_detection.dart';
    
    // 1. Generate filepath
    String imagePath = join((await getApplicationSupportDirectory()).path,
        "${(DateTime.now().millisecondsSinceEpoch / 1000).round()}.jpeg");
    
    // 2. Detect edges from gallery
    try {
        bool success = await EdgeDetection.detectEdgeFromGallery(imagePath,
            androidCropTitle: 'Crop',
            androidCropBlackWhiteTitle: 'Black White',
            androidCropReset: 'Reset',
        );
    } catch (e) {
        print(e);
    }