fluttervisionsdkplugin 0.0.9 copy "fluttervisionsdkplugin: ^0.0.9" to clipboard
fluttervisionsdkplugin: ^0.0.9 copied to clipboard

A Flutter plugin package for integrating the Flutter Vision SDK into your Flutter application for scanning Barcodes and QrCodes

example/lib/main.dart

// ignore_for_file: avoid_print, unused_element, library_private_types_in_public_api

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttervisionsdkplugin/fluttervisionsdkplugin.dart';
import 'package:fluttervisionsdkplugin/settings/focus_settings.dart';
import 'package:fluttervisionsdkplugin_example/secrets.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FlutterToPluginCommunicator? communicator;

  final env = Environment.sandbox;

  String scannedCode = '';
  String scannedType = '';
  String scanErrorMessage = '';

  bool isAutoScanning = true;
  bool isOnlineOCREnabled = true;
  int scanMode = 1;

  bool shouldDisplayFocusImage = false;

  List<String> receivedCodes = [];

  void _onScanError(String errorMessage) {
    print('Scan Error: $errorMessage');

    setState(() {
      scanErrorMessage = errorMessage;
    });
  }

  void updateReceivedCodes(List<String> codes) {
    setState(() {
      receivedCodes = codes;
    });
  }

  void onDetected(String type) {
    setState(() {
      scannedType = type;
    });
  }

  void _onCodesReceived(List<String> codes) {
    updateReceivedCodes(codes);

    if (codes.isNotEmpty) {
      updateScannedCode(codes.first);
    } else {
      updateScannedCode('');
    }
  }

  void _onDetectionResult(bool textDetected, bool barCodeDetected, bool qrCodeDetected) {
    print('Text Detected: $textDetected');
    print('Barcode Detected: $barCodeDetected');
    print('QR Code Detected: $qrCodeDetected');
  }

  void _onOCRImagesReceived(Map<String, dynamic> scanResult) {
    print(scanResult);
  }

  void _changeCaptureMode(String mode) {
    print('Changing Capture Mode to: $mode');
  }

  void _changeScanMode(String mode) {
    print('Changing Scan Mode to: $mode');
  }

  void _capture() {
    print('Capturing Photo');
  }

  void updateScannedType(String type) {
    setState(() {
      scannedType = type;
    });
  }

  void updateScannedCode(String code) {
    setState(() {
      scannedCode = code;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder<PermissionStatus>(
          future: Permission.camera.status,
          builder: (context, snapshot) {
            if (Platform.isIOS || snapshot.data?.isGranted == true) {
              return Column(
                children: [
                  SizedBox(
                    width: double.infinity,
                    height: 400,
                    child: NativeViewWidget(
                      environment: env,
                      onViewCreated: (communicator) {
                        this.communicator = communicator;
                        this.communicator?.startCamera();
                      },
                      listener: MainNativeViewWidgetListener(this),
                    ),
                  ),
                  Text(
                    'Detected Type: $scannedType',
                    style: const TextStyle(color: Colors.black, fontSize: 16),
                  ),
                  Text(
                    'Scanned Code: $scannedCode',
                    style: const TextStyle(color: Colors.black, fontSize: 16),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Expanded(
                              child: RadioListTile(
                                  value: true,
                                  title: const Text('Auto'),
                                  groupValue: isAutoScanning,
                                  onChanged: (i) {
                                    setState(() {
                                      isAutoScanning = true;
                                    });
                                    communicator?.setScanMode(1);
                                  }),
                            ),
                      Expanded(
                              child: RadioListTile(
                                  value: false,
                                  title: const Text('Manual'),
                                  groupValue: isAutoScanning,
                                  onChanged: (i) {
                                    setState(() {
                                      isAutoScanning = false;
                                    });
                                    communicator?.setScanMode(2);
                                  }),
                            ),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Flexible(
                              child: RadioListTile(
                                  value: 1,
                                  title: const Text('B'),
                                  groupValue: scanMode,
                                  onChanged: (i) {
                                    setState(() {
                                      scanMode = 1;
                                    });
                                    communicator?.setCaptureModeBarcode();
                                  }),
                            ),
                      Flexible(
                              child: RadioListTile(
                                  value: 2,
                                  title: const Text('Q'),
                                  groupValue: scanMode,
                                  onChanged: (i) {
                                    setState(() {
                                      scanMode = 2;
                                    });
                                    communicator?.setCaptureModeQrCode();
                                  }),
                            ),
                      if (!isAutoScanning)
                        Flexible(
                          child: RadioListTile(
                              value: 3,
                              title: const Text('O'),
                              groupValue: scanMode,
                              onChanged: (i) {
                                setState(() {
                                  scanMode = 3;
                                });
                                communicator?.setCaptureModeOCR();
                              }),
                        ),
                    ],
                  ),
                  if (!isAutoScanning)
                    ElevatedButton(
                            onPressed: () {
                              isOnlineOCREnabled = true;
                              communicator?.capturePhoto();
                            },
                            child: const Text('Capture Photo'),
                          ),
                  ElevatedButton(
                    onPressed: () async {
                      // communicator?.stopCamera();
                      print('BEFORE CONFIGURE ON-DEVICE OCR');
                      await communicator?.configureOnDeviceOCR(
                          apiKey: switch (env) {
                            Environment.qa => throw UnimplementedError(),
                            Environment.dev => DEV.apiKey,
                            Environment.sandbox => SANDBOX.apiKey,
                            Environment.staging => throw UnimplementedError(),
                            Environment.production => throw UnimplementedError(),
                          },
                          modelClass: ModelClass.shippingLabel,
                          modelSize: ModelSize.large);
                      print('AFTER CONFIGURE ON-DEVICE OCR');
                      // communicator?.startCamera();
                    },
                    child: const Text('Configure'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      isOnlineOCREnabled = false;
                      communicator?.capturePhoto();
                    },
                    child: const Text('Get Predictions'),
                  ),
                ],
              );
            } else {
              return Center(
                  child: FutureBuilder<bool>(
                future: Permission.camera.isPermanentlyDenied,
                builder: (context, snapshot) {
                  if (snapshot.data == true) {
                    return const Text("Go to Settings and allow camera permission");
                  } else {
                    return ElevatedButton(
                        onPressed: () async {
                          await Permission.camera.request();
                          setState(() {});
                        },
                        child: const Text('Request Camera Permission'));
                  }
                },
              ));
            }
          },
        ),
      ),
    );
  }
}

class MainNativeViewWidgetListener implements PluginToFlutterCommunicator {
  MainNativeViewWidgetListener(this._state);

  final _MyAppState _state;

  Timer? _timerTask;

  @override
  void onCameraStarted() {
    _state.communicator?.setFocusSettings(FocusSettings(shouldDisplayFocusImage: true));
  }

  @override
  void onCameraStopped() {}

  @override
  void onCodesReceived(List<String> codes) {
    _state.updateScannedCode(codes.join(','));
    _resetTextAfterDelay();
  }

  @override
  void onDetectionResult(bool isText, bool isBarcode, bool isQrCode) {
    var detectedType = '';
    if (isText) {
      detectedType = 'Text';
    } else if (isBarcode) {
      detectedType = 'Barcode';
    } else if (isQrCode) {
      detectedType = 'Qrcode';
    } else {
      detectedType = '';
    }
    _state.updateScannedType(detectedType);
  }

  @override
  void onImageCaptured(String base64Image, File? imageFile, List<String> codes) {
    if (_state.isOnlineOCREnabled) {
      _state.communicator?.callBolApi(
          apiKey: switch (_state.env) {
            Environment.qa => throw UnimplementedError(),
            Environment.dev => DEV.bolApiKey,
            Environment.sandbox => SANDBOX.bolApiKey,
            Environment.staging => throw UnimplementedError(),
            Environment.production => throw UnimplementedError(),
          },
          image: base64Image,
          barcodes: codes);

      // _state.communicator?.callOcrApi(
      //     apiKey: switch (_state.env) {
      //       Environment.qa => throw UnimplementedError(),
      //       Environment.dev => DEV.apiKey,
      //       Environment.sandbox => SANDBOX.apiKey,
      //       Environment.staging => throw UnimplementedError(),
      //       Environment.production => throw UnimplementedError(),
      //     },
      //     image: base64Image,
      //     barcodes: codes,
      //     locationId: '',
      //     sender: {
      //       'contact_id': 'ctct_hNxjevb74gV62i5XTQvqg6'
      //     },
      //     recipient: {
      //       'contact_id': 'ctct_hNxjevb74gV62i5XTQvqg6'
      //     },
      //     options: {
      //       'match': {
      //         'search': ['recipients'],
      //         'location': true
      //       },
      //       'postprocess': {'require_unique_hash': false},
      //       'transform': {'tracker': 'outbound', 'use_existing_tracking_number': false}
      //     },
      //     metadata: {
      //       'Test': 'Pass'
      //     });
    } else {
      _state.communicator?.getPredictions(base64Image, codes);
    }
  }

  @override
  void onOnDeviceConfigureProgress(double progress) {
    print('Configure progress $progress');
  }

  @override
  void onOnDeviceConfigurationComplete() {
    print('On-Device Configuration Completed');
  }

  @override
  void onOnlineSLResult(Map<String, dynamic> result) {
    _state.updateScannedCode(result.toString());
    _resetTextAfterDelay();
  }

  @override
  void onOnlineBOLResult(Map<String, dynamic> result) {
    _state.updateScannedCode(result.toString());
    _resetTextAfterDelay();
  }

  @override
  void onOnDeviceOCRResult(Map<String, dynamic> result) {
    print('ON-DEVICE OCR RESPONSE');
    print(result);
    _state.updateScannedCode(result.toString());
    _resetTextAfterDelay();
  }

  @override
  void onScanError(String error) {
    _state.updateScannedCode(error);
    _resetTextAfterDelay();
  }

  void _resetTextAfterDelay() {
    _timerTask?.cancel();
    _timerTask = Timer(const Duration(seconds: 10), () {
      _state.updateScannedCode('');
      _state.communicator?.rescan();
    });
  }
}
2
likes
0
points
524
downloads

Publisher

verified publisherpackagex.io

Weekly Downloads

A Flutter plugin package for integrating the Flutter Vision SDK into your Flutter application for scanning Barcodes and QrCodes

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface

More

Packages that depend on fluttervisionsdkplugin

Packages that implement fluttervisionsdkplugin