Enguerrand ARMINJON
Committed by Enguerrand_ARMINJON_MAC_2

Merge branch 'juliansteenbakker:master' into feature/increase-camera-quality

... ... @@ -2,7 +2,7 @@ group 'dev.steenbakker.mobile_scanner'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.9.10'
ext.kotlin_version = '1.7.22'
repositories {
google()
mavenCentral()
... ...
... ... @@ -172,12 +172,12 @@ class MobileScanner(
// Return the best resolution for the actual device orientation.
// By default camera set its resolution to width 480 and height 640 which is too low for ML KIT.
// If we return an higher resolution than device can handle, camera package take the most relavant one available.
// If we return an higher resolution than device can handle, camera package take the most relevant one available.
// Resolution set must take care of device orientation to preserve aspect ratio.
private fun getResolution(windowManager: WindowManager): Size {
private fun getResolution(windowManager: WindowManager, androidResolution: Size): Size {
val rotation = windowManager.defaultDisplay.rotation
val widthMaxRes = 480 * 4;
val heightMaxRes = 640 * 4;
val widthMaxRes = androidResolution.width
val heightMaxRes = androidResolution.height
val targetResolution = if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
Size(widthMaxRes, heightMaxRes) // Portrait mode
... ... @@ -201,7 +201,8 @@ class MobileScanner(
torchStateCallback: TorchStateCallback,
zoomScaleStateCallback: ZoomScaleStateCallback,
mobileScannerStartedCallback: MobileScannerStartedCallback,
detectionTimeout: Long
detectionTimeout: Long,
androidResolution: Size?
) {
this.detectionSpeed = detectionSpeed
this.detectionTimeout = detectionTimeout
... ... @@ -253,16 +254,19 @@ class MobileScanner(
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
val displayManager = activity.applicationContext.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
val windowManager = activity.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
// Set initial resolution
analysisBuilder.setTargetResolution(getResolution(windowManager))
// Listen future orientation
if (androidResolution != null) {
// Override initial resolution
analysisBuilder.setTargetResolution(getResolution(windowManager, androidResolution))
// Listen future orientation change to apply the custom resolution
displayManager.registerDisplayListener(object : DisplayManager.DisplayListener {
override fun onDisplayAdded(displayId: Int) {}
override fun onDisplayRemoved(displayId: Int) {}
override fun onDisplayChanged(displayId: Int) {
analysisBuilder.setTargetResolution(getResolution(windowManager))
analysisBuilder.setTargetResolution(getResolution(windowManager, androidResolution))
}
}, null)
}
val analysis = analysisBuilder.build().apply { setAnalyzer(executor, captureOutput) }
... ...
... ... @@ -2,6 +2,7 @@ package dev.steenbakker.mobile_scanner
import android.app.Activity
import android.net.Uri
import android.util.Size
import androidx.camera.core.CameraSelector
import androidx.camera.core.ExperimentalGetImage
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
... ... @@ -133,6 +134,12 @@ class MobileScannerHandler(
val returnImage: Boolean = call.argument<Boolean>("returnImage") ?: false
val speed: Int = call.argument<Int>("speed") ?: 1
val timeout: Int = call.argument<Int>("timeout") ?: 250
val androidResolutionValueList: List<Int>? = call.argument<List<Int>>("androidResolution")
val androidResolution: Size? = if (androidResolutionValueList != null) {
Size(androidResolutionValueList[0], androidResolutionValueList[1])
} else {
null
}
var barcodeScannerOptions: BarcodeScannerOptions? = null
if (formats != null) {
... ... @@ -164,7 +171,8 @@ class MobileScannerHandler(
"torchable" to it.hasFlashUnit
))
},
timeout.toLong())
timeout.toLong(),
androidResolution)
} catch (e: AlreadyStarted) {
result.error(
... ...
... ... @@ -214,18 +214,28 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
print("Failed to reset zoom scale")
}
let dimensions = CMVideoFormatDescriptionGetDimensions(self.device.activeFormat.formatDescription)
if let device = self.device {
let dimensions = CMVideoFormatDescriptionGetDimensions(
device.activeFormat.formatDescription)
let hasTorch = device.hasTorch
DispatchQueue.main.async {
completion(
MobileScannerStartParameters(
width: Double(dimensions.height),
height: Double(dimensions.width),
hasTorch: self.device.hasTorch,
textureId: self.textureId
hasTorch: hasTorch,
textureId: self.textureId ?? 0
)
)
}
return
}
DispatchQueue.main.async {
completion(MobileScannerStartParameters())
}
}
}
... ...
... ... @@ -6,6 +6,7 @@ import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
import 'package:mobile_scanner/src/objects/barcode_capture.dart';
import 'package:mobile_scanner/src/objects/mobile_scanner_arguments.dart';
import 'package:mobile_scanner/src/scan_window_calculation.dart';
/// The function signature for the error builder.
typedef MobileScannerErrorBuilder = Widget Function(
... ... @@ -175,75 +176,6 @@ class _MobileScannerState extends State<MobileScanner>
}
}
/// the [scanWindow] rect will be relative and scaled to the [widgetSize] not the texture. so it is possible,
/// depending on the [fit], for the [scanWindow] to partially or not at all overlap the [textureSize]
///
/// since when using a [BoxFit] the content will always be centered on its parent. we can convert the rect
/// to be relative to the texture.
///
/// since the textures size and the actuall image (on the texture size) might not be the same, we also need to
/// calculate the scanWindow in terms of percentages of the texture, not pixels.
Rect calculateScanWindowRelativeToTextureInPercentage(
BoxFit fit,
Rect scanWindow,
Size textureSize,
Size widgetSize,
) {
double fittedTextureWidth;
double fittedTextureHeight;
switch (fit) {
case BoxFit.contain:
final widthRatio = widgetSize.width / textureSize.width;
final heightRatio = widgetSize.height / textureSize.height;
final scale = widthRatio < heightRatio ? widthRatio : heightRatio;
fittedTextureWidth = textureSize.width * scale;
fittedTextureHeight = textureSize.height * scale;
break;
case BoxFit.cover:
final widthRatio = widgetSize.width / textureSize.width;
final heightRatio = widgetSize.height / textureSize.height;
final scale = widthRatio > heightRatio ? widthRatio : heightRatio;
fittedTextureWidth = textureSize.width * scale;
fittedTextureHeight = textureSize.height * scale;
break;
case BoxFit.fill:
fittedTextureWidth = widgetSize.width;
fittedTextureHeight = widgetSize.height;
break;
case BoxFit.fitHeight:
final ratio = widgetSize.height / textureSize.height;
fittedTextureWidth = textureSize.width * ratio;
fittedTextureHeight = widgetSize.height;
break;
case BoxFit.fitWidth:
final ratio = widgetSize.width / textureSize.width;
fittedTextureWidth = widgetSize.width;
fittedTextureHeight = textureSize.height * ratio;
break;
case BoxFit.none:
case BoxFit.scaleDown:
fittedTextureWidth = textureSize.width;
fittedTextureHeight = textureSize.height;
break;
}
final offsetX = (widgetSize.width - fittedTextureWidth) / 2;
final offsetY = (widgetSize.height - fittedTextureHeight) / 2;
final left = (scanWindow.left - offsetX) / fittedTextureWidth;
final top = (scanWindow.top - offsetY) / fittedTextureHeight;
final right = (scanWindow.right - offsetX) / fittedTextureWidth;
final bottom = (scanWindow.bottom - offsetY) / fittedTextureHeight;
return Rect.fromLTRB(left, top, right, bottom);
}
Rect? scanWindow;
@override
... ... @@ -261,8 +193,8 @@ class _MobileScannerState extends State<MobileScanner>
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
value.size,
Size(constraints.maxWidth, constraints.maxHeight),
textureSize: value.size,
widgetSize: constraints.biggest,
);
_controller.updateScanWindow(scanWindow);
... ...
... ... @@ -23,6 +23,7 @@ class MobileScannerController {
)
this.onPermissionSet,
this.autoStart = true,
this.androidResolution,
});
/// Select which camera should be used.
... ... @@ -58,9 +59,25 @@ class MobileScannerController {
/// Automatically start the mobileScanner on initialization.
final bool autoStart;
/// Can be used to override default Android camera resolution.
/// The default camera resolution is 640x480.
/// Overriding the resolution can change the camera aspect ratio.
///
/// Example: androidResolution: Size(1920, 2560);
///
/// NOTE:
/// Values inside this Size will be converted to integer type.
///
/// The package Android implementation will manage itself the orientation.
/// You don't need to update this parameter if orientation change.
///
/// Android will take the closest resolution available if the overrided one can't be set
final Size? androidResolution;
/// Sets the barcode stream
final StreamController<BarcodeCapture> _barcodesController =
StreamController.broadcast();
Stream<BarcodeCapture> get barcodes => _barcodesController.stream;
static const MethodChannel _methodChannel =
... ... @@ -133,6 +150,12 @@ class MobileScannerController {
arguments['formats'] = formats!.map((e) => e.rawValue).toList();
} else if (Platform.isAndroid) {
arguments['formats'] = formats!.map((e) => e.index).toList();
if (androidResolution != null) {
arguments['androidResolution'] = <int>[
androidResolution!.width.toInt(),
androidResolution!.height.toInt(),
];
}
}
}
arguments['returnImage'] = returnImage;
... ... @@ -384,6 +407,7 @@ class MobileScannerController {
barcodes: [
Barcode(
rawValue: (data as Map)['payload'] as String?,
format: toFormat(data['symbology'] as int),
),
],
),
... ...
import 'dart:math';
import 'package:flutter/rendering.dart';
/// Calculate the scan window rectangle relative to the texture size.
///
/// The [scanWindow] rectangle will be relative and scaled to [widgetSize], not [textureSize].
/// Depending on the given [fit], the [scanWindow] can partially overlap the [textureSize],
/// or not at all.
///
/// Due to using [BoxFit] the content will always be centered on its parent,
/// which enables converting the rectangle to be relative to the texture.
///
/// Because the size of the actual texture and the size of the texture in widget-space
/// can be different, calculate the size of the scan window in percentages,
/// rather than pixels.
///
/// Returns a [Rect] that represents the position and size of the scan window in the texture.
Rect calculateScanWindowRelativeToTextureInPercentage(
BoxFit fit,
Rect scanWindow, {
required Size textureSize,
required Size widgetSize,
}) {
// Convert the texture size to a size in widget-space, with the box fit applied.
final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize);
// Get the correct scaling values depending on the given BoxFit mode
double sx = fittedTextureSize.destination.width / textureSize.width;
double sy = fittedTextureSize.destination.height / textureSize.height;
switch (fit) {
case BoxFit.fill:
// No-op, just use sx and sy.
break;
case BoxFit.contain:
final s = min(sx, sy);
sx = s;
sy = s;
break;
case BoxFit.cover:
final s = max(sx, sy);
sx = s;
sy = s;
break;
case BoxFit.fitWidth:
sy = sx;
break;
case BoxFit.fitHeight:
sx = sy;
break;
case BoxFit.none:
sx = 1.0;
sy = 1.0;
break;
case BoxFit.scaleDown:
final s = min(sx, sy);
sx = s;
sy = s;
break;
}
// Fit the texture size to the widget rectangle given by the scaling values above.
final textureWindow = Alignment.center.inscribe(
Size(textureSize.width * sx, textureSize.height * sy),
Rect.fromLTWH(0, 0, widgetSize.width, widgetSize.height),
);
// Transform the scan window from widget coordinates to texture coordinates.
final scanWindowInTexSpace = Rect.fromLTRB(
(1 / sx) * (scanWindow.left - textureWindow.left),
(1 / sy) * (scanWindow.top - textureWindow.top),
(1 / sx) * (scanWindow.right - textureWindow.left),
(1 / sy) * (scanWindow.bottom - textureWindow.top),
);
// Clip the scan window in texture coordinates with the texture bounds.
// This prevents percentages outside the range [0; 1].
final clippedScanWndInTexSpace = scanWindowInTexSpace.intersect(
Rect.fromLTWH(0, 0, textureSize.width, textureSize.height),
);
// Compute relative rectangle coordinates,
// with respect to the texture size, i.e. scan image.
final percentageLeft = clippedScanWndInTexSpace.left / textureSize.width;
final percentageTop = clippedScanWndInTexSpace.top / textureSize.height;
final percentageRight = clippedScanWndInTexSpace.right / textureSize.width;
final percentageBottom = clippedScanWndInTexSpace.bottom / textureSize.height;
// This rectangle can be used to cut out a rectangle of the scan image.
return Rect.fromLTRB(
percentageLeft,
percentageTop,
percentageRight,
percentageBottom,
);
}
... ...
... ... @@ -2,6 +2,7 @@ import AVFoundation
import FlutterMacOS
import Vision
import AppKit
import VideoToolbox
public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, FlutterTexture, AVCaptureVideoDataOutputSampleBufferDelegate {
... ... @@ -17,7 +18,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
var captureSession: AVCaptureSession!
// The selected camera
var device: AVCaptureDevice!
weak var device: AVCaptureDevice!
// Image to be sent to the texture
var latestBuffer: CVImageBuffer!
... ... @@ -29,6 +30,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
var timeoutSeconds: Double = 0
var symbologies:[VNBarcodeSymbology] = []
// var analyzeMode: Int = 0
var analyzing: Bool = false
... ... @@ -93,7 +96,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
var nextScanTime = 0.0
var imagesCurrentlyBeingProcessed = 0
var imagesCurrentlyBeingProcessed = false
// Gets called when a new image is added to the buffer
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
... ... @@ -109,44 +112,52 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
registry.textureFrameAvailable(textureId)
let currentTime = Date().timeIntervalSince1970
let eligibleForScan = currentTime > nextScanTime && imagesCurrentlyBeingProcessed == 0;
let eligibleForScan = currentTime > nextScanTime && !imagesCurrentlyBeingProcessed
if ((detectionSpeed == DetectionSpeed.normal || detectionSpeed == DetectionSpeed.noDuplicates) && eligibleForScan || detectionSpeed == DetectionSpeed.unrestricted) {
nextScanTime = currentTime + timeoutSeconds
imagesCurrentlyBeingProcessed += 1
let imageRequestHandler = VNImageRequestHandler(
cvPixelBuffer: latestBuffer,
orientation: .right)
imagesCurrentlyBeingProcessed = true
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
if(self!.latestBuffer == nil){
return
}
var cgImage: CGImage?
VTCreateCGImageFromCVPixelBuffer(self!.latestBuffer, options: nil, imageOut: &cgImage)
let imageRequestHandler = VNImageRequestHandler(cgImage: cgImage!)
do {
try imageRequestHandler.perform([VNDetectBarcodesRequest { [self] (request, error) in
imagesCurrentlyBeingProcessed -= 1
let barcodeRequest:VNDetectBarcodesRequest = VNDetectBarcodesRequest(completionHandler: { [weak self] (request, error) in
self?.imagesCurrentlyBeingProcessed = false
if error == nil {
if let results = request.results as? [VNBarcodeObservation] {
for barcode in results {
if self.scanWindow != nil {
let match = self.isbarCodeInScanWindow(self.scanWindow!, barcode, self.latestBuffer)
if self?.scanWindow != nil && cgImage != nil {
let match = self?.isBarCodeInScanWindow(self!.scanWindow!, barcode, cgImage!) ?? false
if (!match) {
continue
}
}
let barcodeType = String(barcode.symbology.rawValue).replacingOccurrences(of: "VNBarcodeSymbology", with: "")
let event: [String: Any?] = ["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcodeType]]
self.sink?(event)
// if barcodeType == "QR" {
// let image = CIImage(image: source)
// image?.cropping(to: barcode.boundingBox)
// self.qrCodeDescriptor(qrCode: barcode, qrCodeImage: image!)
// }
DispatchQueue.main.async {
self?.sink?(["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcode.symbology.toInt as Any?]] as [String : Any])
}
// if barcodeType == "QR" {
// let image = CIImage(image: source)
// image?.cropping(to: barcode.boundingBox)
// self.qrCodeDescriptor(qrCode: barcode, qrCodeImage: image!)
// }
}
}
} else {
print(error!.localizedDescription)
self?.sink?(FlutterError(code: "MobileScanner", message: error?.localizedDescription, details: nil))
}
})
if(self?.symbologies.isEmpty == false){
// add the symbologies the user wishes to support
barcodeRequest.symbologies = self!.symbologies
}
try imageRequestHandler.perform([barcodeRequest])
} catch let e {
self?.sink?(FlutterError(code: "MobileScanner", message: e.localizedDescription, details: nil))
}
}])
} catch {
print(error)
}
}
}
... ... @@ -192,7 +203,21 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
scanWindow = CGRect(x: minX, y: minY, width: width, height: height)
}
func isbarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: VNBarcodeObservation, _ inputImage: CVImageBuffer) -> Bool {
func isBarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: VNBarcodeObservation, _ inputImage: CGImage) -> Bool {
let imageWidth = CGFloat(inputImage.width);
let imageHeight = CGFloat(inputImage.height);
let minX = scanWindow.minX * imageWidth
let minY = scanWindow.minY * imageHeight
let width = scanWindow.width * imageWidth
let height = scanWindow.height * imageHeight
let scaledScanWindow = CGRect(x: minX, y: minY, width: width, height: height)
return scaledScanWindow.contains(barcode.boundingBox)
}
func isBarCodeInScanWindow(_ scanWindow: CGRect, _ barcode: VNBarcodeObservation, _ inputImage: CVImageBuffer) -> Bool {
let size = CVImageBufferGetEncodedSize(inputImage)
let imageWidth = size.width;
... ... @@ -220,13 +245,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
let argReader = MapArgumentReader(call.arguments as? [String: Any])
// let ratio: Int = argReader.int(key: "ratio")
let torch: Bool = argReader.bool(key: "torch") ?? false
let facing: Int = argReader.int(key: "facing") ?? 1
let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0
let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0
// let ratio: Int = argReader.int(key: "ratio")
let torch:Bool = argReader.bool(key: "torch") ?? false
let facing:Int = argReader.int(key: "facing") ?? 1
let speed:Int = argReader.int(key: "speed") ?? 0
let timeoutMs:Int = argReader.int(key: "timeout") ?? 0
symbologies = argReader.toSymbology()
timeoutSeconds = Double(timeoutMs) * 1000.0
timeoutSeconds = Double(timeoutMs) / 1000.0
detectionSpeed = DetectionSpeed(rawValue: speed)!
// Set the camera to use
... ... @@ -277,7 +303,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
captureSession.addOutput(videoOutput)
for connection in videoOutput.connections {
// connection.videoOrientation = .portrait
// connection.videoOrientation = .portrait
if position == .front && connection.isVideoMirroringSupported {
connection.isVideoMirrored = true
}
... ... @@ -373,8 +399,95 @@ class MapArgumentReader {
return args?[key] as? [String]
}
func toSymbology() -> [VNBarcodeSymbology] {
guard let syms:[Int] = args?["formats"] as? [Int] else {
return []
}
if(syms.contains(0)){
return []
}
var barcodeFormats:[VNBarcodeSymbology] = []
syms.forEach { id in
if let bc:VNBarcodeSymbology = VNBarcodeSymbology.fromInt(id) {
barcodeFormats.append(bc)
}
}
return barcodeFormats
}
func floatArray(key: String) -> [CGFloat]? {
return args?[key] as? [CGFloat]
}
}
extension VNBarcodeSymbology {
static func fromInt(_ mapValue:Int) -> VNBarcodeSymbology? {
if #available(macOS 12.0, *) {
if(mapValue == 8){
return VNBarcodeSymbology.codabar
}
}
switch(mapValue){
case 1:
return VNBarcodeSymbology.code128
case 2:
return VNBarcodeSymbology.code39
case 4:
return VNBarcodeSymbology.code93
case 16:
return VNBarcodeSymbology.dataMatrix
case 32:
return VNBarcodeSymbology.ean13
case 64:
return VNBarcodeSymbology.ean8
case 128:
return VNBarcodeSymbology.itf14
case 256:
return VNBarcodeSymbology.qr
case 1024:
return VNBarcodeSymbology.upce
case 2048:
return VNBarcodeSymbology.pdf417
case 4096:
return VNBarcodeSymbology.aztec
default:
return nil
}
}
var toInt:Int? {
if #available(macOS 12.0, *) {
if(self == VNBarcodeSymbology.codabar){
return 8
}
}
switch(self){
case VNBarcodeSymbology.code128:
return 1
case VNBarcodeSymbology.code39:
return 2
case VNBarcodeSymbology.code93:
return 4
case VNBarcodeSymbology.dataMatrix:
return 16
case VNBarcodeSymbology.ean13:
return 32
case VNBarcodeSymbology.ean8:
return 64
case VNBarcodeSymbology.itf14:
return 128
case VNBarcodeSymbology.qr:
return 256
case VNBarcodeSymbology.upce:
return 1024
case VNBarcodeSymbology.pdf417:
return 2048
case VNBarcodeSymbology.aztec:
return 4096
default:
return -1;
}
}
}
... ...
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mobile_scanner/src/scan_window_calculation.dart';
void main() {
group(
'Scan window relative to texture',
() {
group('Widget (landscape) smaller than texture (portrait)', () {
const textureSize = Size(480.0, 640.0);
const widgetSize = Size(432.0, 256.0);
final ctx = ScanWindowTestContext(
textureSize: textureSize,
widgetSize: widgetSize,
scanWindow: Rect.fromLTWH(
widgetSize.width / 4,
widgetSize.height / 4,
widgetSize.width / 2,
widgetSize.height / 2,
),
);
test('wl tp: BoxFit.none', () {
ctx.testScanWindow(
BoxFit.none,
const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6),
);
});
test('wl tp: BoxFit.fill', () {
ctx.testScanWindow(
BoxFit.fill,
const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75),
);
});
test('wl tp: BoxFit.fitHeight', () {
ctx.testScanWindow(
BoxFit.fitHeight,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tp: BoxFit.fitWidth', () {
ctx.testScanWindow(
BoxFit.fitWidth,
const Rect.fromLTRB(
0.25,
0.38888888888888895,
0.75,
0.6111111111111112,
),
);
});
test('wl tp: BoxFit.cover', () {
// equal to fitWidth
ctx.testScanWindow(
BoxFit.cover,
const Rect.fromLTRB(
0.25,
0.38888888888888895,
0.75,
0.6111111111111112,
),
);
});
test('wl tp: BoxFit.contain', () {
// equal to fitHeigth
ctx.testScanWindow(
BoxFit.contain,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tp: BoxFit.scaleDown', () {
// equal to fitHeigth, contain
ctx.testScanWindow(
BoxFit.scaleDown,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
});
group('Widget (landscape) smaller than texture and texture (landscape)',
() {
const textureSize = Size(640.0, 480.0);
const widgetSize = Size(320.0, 120.0);
final ctx = ScanWindowTestContext(
textureSize: textureSize,
widgetSize: widgetSize,
scanWindow: Rect.fromLTWH(
widgetSize.width / 4,
widgetSize.height / 4,
widgetSize.width / 2,
widgetSize.height / 2,
),
);
test('wl tl: BoxFit.none', () {
ctx.testScanWindow(
BoxFit.none,
const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625),
);
});
test('wl tl: BoxFit.fill', () {
ctx.testScanWindow(
BoxFit.fill,
const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75),
);
});
test('wl tl: BoxFit.fitHeight', () {
ctx.testScanWindow(
BoxFit.fitHeight,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tl: BoxFit.fitWidth', () {
ctx.testScanWindow(
BoxFit.fitWidth,
const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625),
);
});
test('wl tl: BoxFit.cover', () {
// equal to fitWidth
ctx.testScanWindow(
BoxFit.cover,
const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625),
);
});
test('wl tl: BoxFit.contain', () {
// equal to fitHeigth
ctx.testScanWindow(
BoxFit.contain,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tl: BoxFit.scaleDown', () {
// equal to fitHeigth, contain
ctx.testScanWindow(
BoxFit.scaleDown,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
});
},
);
}
class ScanWindowTestContext {
ScanWindowTestContext({
required this.textureSize,
required this.widgetSize,
required this.scanWindow,
});
final Size textureSize;
final Size widgetSize;
final Rect scanWindow;
void testScanWindow(BoxFit fit, Rect expected) {
final actual = calculateScanWindowRelativeToTextureInPercentage(
fit,
scanWindow,
textureSize: textureSize,
widgetSize: widgetSize,
);
// don't use expect(actual, expected) because Rect.toString() only shows one digit after the comma which can be confusing
expect(actual.left, expected.left);
expect(actual.top, expected.top);
expect(actual.right, expected.right);
expect(actual.bottom, expected.bottom);
}
}
... ...