Flutter plugin for scanning and generating QR codes using the ZXing library, supporting Android, iOS, and desktop platforms
flutterbarcode-generatorbarcode-scannergeneratorqrqrcodeqrcode-generatorqrcode-scannerscannerzxingbarcodezxscanner
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.1 KiB
81 lines
2.1 KiB
3 years ago
|
import 'dart:typed_data';
|
||
|
|
||
3 years ago
|
import 'package:flutter/material.dart';
|
||
3 years ago
|
import 'package:flutter_zxing/flutter_zxing.dart';
|
||
3 years ago
|
import 'package:zxscanner/models/models.dart';
|
||
|
import 'package:zxscanner/utils/db_service.dart';
|
||
|
import 'package:zxscanner/utils/extensions.dart';
|
||
3 years ago
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||
|
import 'package:image_picker/image_picker.dart';
|
||
|
import 'package:image/image.dart' as imglib;
|
||
3 years ago
|
|
||
|
class ScannerPage extends StatefulWidget {
|
||
|
const ScannerPage({
|
||
|
Key? key,
|
||
|
}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
State<ScannerPage> createState() => _ScannerPageState();
|
||
|
}
|
||
|
|
||
3 years ago
|
class _ScannerPageState extends State<ScannerPage> {
|
||
|
final ImagePicker _picker = ImagePicker();
|
||
3 years ago
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: const Text('Scanner'),
|
||
|
),
|
||
3 years ago
|
body: ReaderWidget(
|
||
3 years ago
|
onScan: (result) async {
|
||
3 years ago
|
addCode(result);
|
||
3 years ago
|
},
|
||
|
),
|
||
|
floatingActionButton: FloatingActionButton(
|
||
3 years ago
|
onPressed: pickImage,
|
||
3 years ago
|
child: const Icon(FontAwesomeIcons.image),
|
||
3 years ago
|
),
|
||
|
);
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
pickImage() async {
|
||
|
try {
|
||
|
final XFile? file = await _picker.pickImage(source: ImageSource.gallery);
|
||
|
if (file != null) {
|
||
|
readCodeFromImage(file);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
debugPrint(e.toString());
|
||
|
context.showToast(e.toString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
readCodeFromImage(XFile file) async {
|
||
|
final Uint8List bytes = await file.readAsBytes();
|
||
|
imglib.Image? image = imglib.decodeImage(bytes);
|
||
|
if (image != null) {
|
||
|
final CodeResult result = FlutterZxing.readBarcode(
|
||
|
image.getBytes(format: imglib.Format.luminance),
|
||
|
Format.Any,
|
||
|
image.width,
|
||
|
image.height,
|
||
|
0,
|
||
|
0,
|
||
|
);
|
||
|
if (result.isValidBool) {
|
||
|
addCode(result);
|
||
|
} else {
|
||
3 years ago
|
if (!mounted) return;
|
||
3 years ago
|
context.showToast('No code found');
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
void addCode(CodeResult result) {
|
||
|
Code code = Code.fromCodeResult(result);
|
||
|
DbService.instance.addCode(code);
|
||
3 years ago
|
context.showToast('Code added:\n${code.text ?? ''}');
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|