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
89 stars 50 forks source link

Failed to lookup symbol 'readBarcode' #77

Closed om-ha closed 10 months ago

om-ha commented 1 year ago

This issue exists in latest commit (4ebc637dfb3208f72c9c8adda813e1f58d3ed99a) and not version 0.10.0

1. Underlying Exception

Path

/Users/user/.pub-cache/git/flutter_zxing-4ebc637dfb3208f72c9c8adda813e1f58d3ed99a/lib/generated_bindings.dart

Exception

Exception has occurred. ArgumentError (Invalid argument(s): Failed to lookup symbol 'readBarcode': dlsym(RTLD_DEFAULT, readBarcode): symbol not found)

Offending code

late final _readBarcodePtr = _lookup<
      ffi.NativeFunction<
          CodeResult Function(ffi.Pointer<ffi.Char>, ffi.Int, ffi.Int, ffi.Int,
              ffi.Int, ffi.Int, ffi.Int, ffi.Int, ffi.Int)>>('readBarcode');
// ---------------------------------------------------------^

2. User-Facing Exception

Path

/Users/user/.pub-cache/git/flutter_zxing-4ebc637dfb3208f72c9c8adda813e1f58d3ed99a/lib/zxing_mobile.dart

Exception

Exception has occurred. _CastError (type 'ArgumentError' is not a subtype of type 'Code' in type cast)

Offending code

await zxingProcessCameraImage(image, params: params) as Code;
// --------------------------------------------------^

3. Usage

Init / Dispose

// StatefulWidget state property
final Zxing zxMobile = ZxingMobile();

// called from initState
await zxMobile.startCameraProcessing();

// called inside dispose
zxMobile.stopCameraProcessing();

processCameraImage

// `CameraImage` object comes from `startImageStream` from `camera` package
// https://github.com/flutter/plugins/blob/729c14a9243ebd2b16d7ec2a63b4e54661b00641/packages/camera/camera/lib/src/camera_controller.dart#L415
Code zxBarcode = await zxMobile.processCameraImage(
    cameraImage,
    params: DecodeParams(
        tryHarder: false,
        tryRotate: true,
        tryInverted: false,
    ),
);

4. pubspec.yaml

offending versions

flutter_zxing:
  git:
    url: https://github.com/khoren93/flutter_zxing.git
    ref: 4ebc637dfb3208f72c9c8adda813e1f58d3ed99a

working pubspec.yaml

flutter_zxing: 0.10.0

Platform Information

om-ha commented 1 year ago

Another question if you may, which one is more recommended when using flutter_zxing with camera package?

khoren93 commented 1 year ago

Thanks for reaching out.

Since version 1.0.0, the flutter_zxing package uses submodules, so specifying a specific version in the pubspec file is not possible. To use the main branch, you will need to manually clone it locally using the command "git clone --recursive https://github.com/khoren93/flutter_zxing.git". Once the package is cloned, navigate to the "scripts" directory using "cd scripts" and run the script "sh update_ios_macos_src.sh". After this, you can add the flutter_zxing package as a local dependency in the pubspec file by specifying the path of the cloned local repository. For example, if you cloned the package in your home directory, the path would be

flutter_zxing: 
  path: ~/flutter_zxing

For the second question, it is highly recommended to use the processCameraImage method, rather than the readBarcode method. The reason for this is that processCameraImage is specifically designed to work with the camera stream, and it processes the stream in a separate isolate. This allows for more efficient and reliable barcode scanning, as well as improved performance and reduced memory usage. However, the readBarcode method can still be used to read barcodes from local image files, such as those from the gallery or from a URL. It takes as parameter bytes (Uint8List) and it's more appropriate for use with static images rather than a camera stream.

khoren93 commented 10 months ago

Closing for now as this is already fixed.