- Upgrade v1.2, support custom view
中文文档 | English
A scan code Flutter plugin, which is a Flutter package for HUAWEI ScanKit SDK.The HUAWEI ScanKit is a powerful library that is easy to use and fast to read.
Scan Kit automatically detects, magnifies, and recognizes barcodes from a distance, and is also able to scan a very small barcode in the same way. It works even in suboptimal situations, such as under dim lighting or when the barcode is reflective, dirty, blurry, or printed on a cylindrical surface. This leads to a high scanning success rate, and an improved user experience.
- Android
- iOS
ScanKit supports 13 major barcode formats (listed as follows). If your app requires only some of the 13 formats, specify the desired formats to speed up barcode scanning.
- 1D barcode formats: EAN-8, EAN-13, UPC-A, UPC-E, Codabar, Code 39, Code 93, Code 128, and ITF-14
- 2D barcode formats: QR Code, Data Matrix, PDF417, and Aztec
Support camera scan code and local picture recognition.
- Configure Permissions
- Handling permission requests
- Calling APIs
Add the following to ios/Runner/Info.plist
<key>NSCameraUsageDescription</key>
<string>Explain to the user here why you need the permission</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Explain to the user here why you need the permission</string>
Note that replacing the content of the tag gives the user a reason for needing the permission.
No configuration required for Android platform!
In Flutter, you need a plugin library for permission handling, here I recommend using another plugin library of mine: flutter_easy_permission, go here for detailed configuration.
Open the ios/Podfile file and add the following code:
target 'Runner' do
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
# Add the library of permissions you need here
pod 'LBXPermission/Camera'
pod 'LBXPermission/Photo'
end
Then execute the command to install.
void initState() {
super.initState();
scanKit = FlutterScankit()
..addResultListen((val) {
// Back to the results
debugPrint("scanning result:$val");
});
FlutterEasyPermission().addPermissionCallback(
onGranted: (requestCode, perms,perm) {
startScan();
},
onDenied: (requestCode, perms,perm, isPermanent) {});
}
Scan the code:
// Request if no permission
if (!await FlutterEasyPermission.has(perms: _permissions,permsGroup: _permissionGroup)) {
FlutterEasyPermission.request(perms: _permissions,permsGroup: _permissionGroup);
} else {
// Call if you have permission
startScan();
}
Future<void> startScan() async {
try {
await scanKit.startScan(scanTypes: [ScanTypes.ALL]);
} on PlatformException {}
}
For the usage of FlutterEasyPermission
please check here .
Use ScanKitWidget
as a scan widget, ScanKitController
for flash switching, picture code recognition and other functions
class _CustomizedViewState extends State<CustomizedView> {
late ScanKitController _controller;
final screenWidth = window.physicalSize.width;
final screenHeight = window.physicalSize.height;
@override
void dispose(){
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
var pixelSize = boxSize * window.devicePixelRatio;
var left = screenWidth/2 - pixelSize/2;
var top = screenHeight/2 - pixelSize/2;
var right = screenWidth/2 + pixelSize/2;
var bottom = screenHeight/2 + pixelSize/2;
var rect = Rect.fromLTRB(left, top, right, bottom);
return Scaffold(
body: SafeArea(
child: Stack(
children: [
ScanKitWidget(
callback: (controller) {
_controller = controller;
controller.onResult.listen((result) {
debugPrint("scanning result:$result");
Navigator.of(context).pop(result);
});
},
continuouslyScan: false,
boundingBox: rect),
Align(
alignment: Alignment.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: Icon(
Icons.arrow_back,
color: Colors.white,
size: 28,
)),
IconButton(
onPressed: () {
_controller.switchLight();
},
icon: Icon(
Icons.lightbulb_outline_rounded,
color: Colors.white,
size: 28,
)),
IconButton(
onPressed: () {
_controller.pickPhoto();
},
icon: Icon(
Icons.picture_in_picture_rounded,
color: Colors.white,
size: 28,
))
],
),
),
Align(
alignment: Alignment.center,
child: Container(
width: boxSize,
height: boxSize,
decoration: BoxDecoration(
border: Border(
left: BorderSide(color: Colors.orangeAccent, width: 2),
right: BorderSide(color: Colors.orangeAccent, width: 2),
top: BorderSide(color: Colors.orangeAccent, width: 2),
bottom: BorderSide(color: Colors.orangeAccent, width: 2)),
),
),
)
],
),
),
);
}
}
For a complete example, please see here.