Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the issue of incorrect QR code position detection when Boxfit is … #1303

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

ohnobug
Copy link

@ohnobug ohnobug commented Jan 23, 2025

my qrcode scan code:

  @override
  Widget build(BuildContext context) {
    // 二维码的位置
    final overlays = <Widget>[
      if (_barcodeCapture != null && _barcodeCapture!.barcodes.isNotEmpty)
        for (final Barcode barcode in _barcodeCapture!.barcodes)
          if (!barcode.size.isEmpty && barcode.corners.isNotEmpty)
            CustomPaint(
              painter: BarcodePainter2(
                barcodeCorners: barcode.corners,
                barcodeSize: barcode.size,
                boxFit: BoxFit.cover,
                cameraPreviewSize: _barcodeCapture!.size,
                color: const Color.fromARGB(183, 0, 255, 34),
                style: PaintingStyle.fill,
              ),
            ),
    ];

    return BlocBuilder<SystemCubit, SystemState>(
        builder: (context, systemState) {
      return Scaffold(
          primary: false,
          appBar: null,
          body: Stack(
            fit: StackFit.expand,
            children: [
              MobileScanner(
                fit: BoxFit.cover,
                controller: controller,
                // onDetect: _handleBarcode,
              ),

              if (_barcodeCapture != null && _barcodeCapture!.image != null)
                Stack(fit: StackFit.expand, children: [
                  // 扫码后暂停结果
                  Image.memory(
                    _barcodeCapture!.image!,
                    fit: BoxFit.cover,
                    filterQuality: FilterQuality.low,
                    frameBuilder: (
                      BuildContext context,
                      Widget child,
                      int? frame,
                      bool? wasSynchronouslyLoaded,
                    ) {
                      if (wasSynchronouslyLoaded == true || frame != null) {
                        return child;
                      }

                      return SizedBox();
                    },
                  ),
                  ...overlays,
                ]),

before:
3db30c82481a125934ffc29ecc2eaf2

fixed after:
5f1fa23a7a5791334c0f760070f3b48

lib/src/overlay/barcode_painter.dart Outdated Show resolved Hide resolved
lib/src/overlay/barcode_painter.dart Outdated Show resolved Hide resolved
lib/src/overlay/barcode_painter.dart Outdated Show resolved Hide resolved
lib/src/overlay/barcode_painter.dart Outdated Show resolved Hide resolved
lib/src/overlay/barcode_painter.dart Outdated Show resolved Hide resolved
@navaronbracke navaronbracke changed the title Fix the issue of incorrect QR code position detection when Boxfit is … fix: Fix the issue of incorrect QR code position detection when Boxfit is … Jan 24, 2025
@ohnobug ohnobug requested a review from navaronbracke January 26, 2025 02:23
@ohnobug
Copy link
Author

ohnobug commented Jan 26, 2025

lib/src/overlay/barcode_painter.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
@ohnobug
Copy link
Author

ohnobug commented Feb 5, 2025

/// Calculate the scaling ratios for width and height to fit the small box (cameraPreviewSize)
/// into the large box (size) based on the specified BoxFit mode.
/// Returns a record containing the width and height scaling ratios.
({double widthRatio, double heightRatio}) calculateBoxFitRatio(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add unit tests for this function? Then we can assure that the math is right.

example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
example/lib/barcode_scanner_window.dart Outdated Show resolved Hide resolved
test/scan_window_test.dart Outdated Show resolved Hide resolved
const cameraPreviewSize = Size(480.0, 640.0);
const size = Size(432.0, 256.0);

test('scp p: BoxFit.fill', () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of the scp p: in the labels?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what the naming convention for test functions is, I just saw that the previous tests used abbreviations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the reasoning behind the existing abbreviations either. I would rather avoid introducing abbreviations altogether. It makes the code harder to reason about.

test/scan_window_test.dart Outdated Show resolved Hide resolved
Copy link
Collaborator

@navaronbracke navaronbracke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment about fixing the test names & a small concern about the test results being duplicated.

size,
);

expect(ratio.widthRatio, 0.4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use several different sizes, to have the numbers be different for each test?

I'd like to avoid introducing a bug because one test group accidentally had 0.9 / 0.9 several times in tests within the group. If we use different sizes, then the outcomes will also be unique among tests in a test group.

When you define the extra sizes, you can reuse them for landscape as well (since that is just flipping the width/height)

const cameraPreviewSize = Size(480.0, 640.0);
const size = Size(432.0, 256.0);

test('scpsip: BoxFit.fill', () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can change the inner test group name to already mention portrait / landscape? Then that does not need to be repeated in the tests themselves and the abbreviations can be removed?

group('calculateBoxFitRatio', () {
  group('smaller portrait camera preview size', () {
    test('fit: BoxFit.fill', () { /* ... */ });
    
    // more tests
  });
  
  group('smaller landscape camera preview size', () {
    test('fit: BoxFit.fill', () { /* ... */ });
    
    // more tests  
  });
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants