scanFromImage method

  1. @override
Future<QRScanResult?> scanFromImage(
  1. String imagePath
)
override

Scan QR code from image file

Implementation

@override
Future<QRScanResult?> scanFromImage(String imagePath) async {
  try {
    if (kDebugMode) {
      debugPrint('QuickQR Scanner: Scanning image from path - $imagePath');
    }

    // Validate file path
    if (imagePath.isEmpty) {
      throw ScannerException.invalidConfiguration('Image path cannot be empty');
    }

    // Verify file exists
    final file = File(imagePath);
    if (!await file.exists()) {
      throw ScannerException.fileNotFound(imagePath);
    }

    // Check file size (optional - could prevent memory issues)
    final fileSize = await file.length();
    if (fileSize > 50 * 1024 * 1024) { // 50MB limit
      throw ScannerException(
        ScannerErrorCode.fileReadError,
        'Image file too large (${(fileSize / 1024 / 1024).toStringAsFixed(1)}MB). Maximum size is 50MB.',
        details: {'filePath': imagePath, 'fileSize': fileSize},
      );
    }

    final result = await methodChannel.invokeMethod<Map<Object?, Object?>>(
      'scanFromImage',
      {'imagePath': imagePath}
    );

    if (result != null) {
      final scanResult = QRScanResult.fromMap(Map<String, dynamic>.from(result));

      if (kDebugMode) {
        debugPrint('QuickQR Scanner: Image scan result - ${scanResult.content}');
      }

      return scanResult;
    }

    if (kDebugMode) {
      debugPrint('QuickQR Scanner: No QR code found in image');
    }

    return null;
  } on PlatformException catch (e) {
    throw _handlePlatformException(e, 'scanFromImage');
  } on ScannerException {
    rethrow;
  } catch (e) {
    throw ScannerException(
      ScannerErrorCode.internalError,
      'Failed to scan image: $e',
      details: {'imagePath': imagePath},
    );
  }
}