khoren93 / flutter_zxing

Flutter plugin for scanning and generating QR codes using the ZXing library, supporting Android, iOS, and desktop platforms
https://pub.dev/packages/flutter_zxing
MIT License
88 stars 50 forks source link

Cannot scan barcode in image #109

Open kaboc opened 9 months ago

kaboc commented 9 months ago

Scanning a barcode in an image returns Code with empty text and error at 1.4.0 and 1.4.1. There was no issue at the point of v1.3.2.

Code sample

  1. Create a new Flutter project.
  2. Create an "assets" folder and place an image in there.
  3. Run the code below.
  4. Press the button and see the text shown in the screen.

pubspec.yaml

...

dependencies:
  ...
  flutter_zxing: 1.4.1
  path_provider: ^2.1.1

...

flutter:
  assets:
    - assets/

main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:flutter_zxing/flutter_zxing.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(const App());

class App extends StatefulWidget {
  const App();

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  Code? _code;

  Future<void> _read() async {
    final image = await rootBundle.load('assets/image.jpg'); // Put the actual file name

    final dir = await getTemporaryDirectory();
    final file = File('${dir.path}/${DateTime.now().microsecondsSinceEpoch}');
    file.writeAsBytesSync(image.buffer.asUint8List());

    final code = await zx.readBarcodeImagePathString(file.path);
    setState(() => _code = code);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: _read,
                  child: const Text('Scan'),
                ),
                const SizedBox(height: 24.0),
                Text('Text: ${_code?.text}'),
                Text('Error: ${_code?.error}'),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
kaboc commented 9 months ago

I think this is a regression. Besides the issue above, it seems to me that it is also problematic that there is currently no way to automatically detect such a regression. Some tests may be necessary. It wouldn't be difficult to write tests for scanning an image file, compared to scanning with a camera.

cmy665 commented 1 month ago

It is also not recognized in version 1.5.2