Skip to content

Instantly share code, notes, and snippets.

@shingo-mori
Last active April 12, 2024 04:15
Show Gist options
  • Save shingo-mori/415028b70136819539c05ef16725bb4e to your computer and use it in GitHub Desktop.
Save shingo-mori/415028b70136819539c05ef16725bb4e to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:ui';
import 'package:scandit_flutter_datacapture_barcode/scandit_flutter_datacapture_barcode.dart';
import 'package:scandit_flutter_datacapture_barcode/scandit_flutter_datacapture_barcode_capture.dart';
import 'package:scandit_flutter_datacapture_core/scandit_flutter_datacapture_core.dart';
const String licenseKey = 'AfeCTXVfRq2YPHgW8TFeSSwGXJXGAYbWKkqcvrlKHEhSfEBsF3phC4lE+WUTWSP52isK0gQSUGQAUIYI0mt/cvNETJknZfBYWwTEBttgjoibRyGfJkLvz8BeBfoFc++QHkOZe6lFiLUoW7X1NlS/x6d+ExVAQ7Wrekhix442XQj4byuK6nqpnMhW2gIFNteLH3noYalQVl6yfAO9Ln0iKtt1xo7aUA5R1VICz+hq+kOjS0+S52HzbxlqORFQZMWPZU1LaYssrr5KbyZezjD8BqdiIOBjc5zHaG3XWqZJscAIc9IPUlg/UxJD/NEya9JyD0GtJ8tp6cAAdrxV6k2XVetdbR3XY3zUW0+DuPt9TJ8eWK43lS9rZot/2oL0Up9v9D2e4dllpXVZaxQCxUXRbq9yqFGmYs8q8m+kmaxGv3u6L09hBmcc9nRtwCm6SAcEj0AJxRR0/wV0Ay3RvFwa2BNDL74/ftLwU3THgepCAAAbIpZrjHsIxiFYMVb+XeuzShCqMHMqk9tdDUzdTjxeckvvGi+7G6LBsyBuIzvgSHQlBJw6qyTo6vCiS4WPFVtANCplfn4U5XdQt/bTr9ZcfRih6QZA/NlADDaQr1+PvJQbLnPbpFRaCXkyuAazN6gNyKZvKox3mwbC4ZkHDJSFZMZE35FYx4bP42C0CH/iRGYVDANSeIvTIu1Sksgc0D8hK+KI/QV56HWS3hISV6zeesqxMeoIBsTXyjDHd0QFuFGHy+gtxJ6rTkwRSs/1crzq9xW+vM1xnQ14QbT3vBucxUDAtxVdlC56nWOz5dZn68UDd+WQO8E3YlPuL1MDruyDTpW2pOkUQkBlOEYgZIFohHWHS5SOnG5gdsUpjokupj9ftddgn+HWCb2TSlfLot0JkQ7tOuL6cDuGl7nj9zCMr57F5RHn71996t1LCLqWEvSoqp6swndG8lGETHF+oI6V7mPaQN4XezBZcIgLPUZmc/FPDgPyzhtjU7iAMMRd4lwYp2ls+/taRulb9ibY669ZDGPdAb4dWOQ+tCksjk0sjsuw9zH5YWEvwxbW4xCswnVVzQSwWkojtJHjctnDNxlgJRn3mB2Q0iX+EA3sJeSxbPQ/2UG7hqTRZN7/FILCA7ahgM4v26xR8Ih6HJsZ2fMoUh0ZQXzm6vVtQso0ZJt8vRxuGREI518MYTtHrWW1B2opXj5lWNpJr8rrwZBj+hyH';
class ScanBloc implements BarcodeCaptureListener {
final DataCaptureContext _captureContext = DataCaptureContext.forLicenseKey(licenseKey);
late BarcodeCapture _barcodeCapture;
Camera? _camera = Camera.defaultCamera;
late DataCaptureView _captureView;
StreamController<String> _singleScanResultController = StreamController();
Stream<String> get singleScanResult => _singleScanResultController.stream;
ScanBloc() {
_init();
}
void _init() {
_camera?.applySettings(BarcodeCapture.recommendedCameraSettings);
_captureView = DataCaptureView.forContext(_captureContext);
_barcodeCapture = BarcodeCapture.forContext(_captureContext, barcodeCaptureSettings)
..addListener(this);
// 照準とバーコード読取時のオーバーレイを設定
var overlay = BarcodeCaptureOverlay.withBarcodeCaptureForViewWithStyle(
_barcodeCapture, _captureView, BarcodeCaptureOverlayStyle.frame)
..viewfinder = RectangularViewfinder.withStyle(RectangularViewfinderStyle.square);
overlay.brush = Brush(Color.fromARGB(0, 0, 0, 0), Color.fromARGB(255, 255, 255, 255), 3);
_captureView.addOverlay(overlay);
// ズーム切替ボタンを追加
_captureView.addControl(ZoomSwitchControl());
// ライト切替ボタンを追加
_captureView.addControl(TorchSwitchControl());
if (_camera != null) {
_captureContext.setFrameSource(_camera!);
}
enableBarcodeCapture();
}
BarcodeCaptureSettings get barcodeCaptureSettings {
var captureSettings = BarcodeCaptureSettings();
//必要に応じてシンボルを有効化して下さい
//記載されていないシンボルを有効化したい場合は、以下を参照してください
//https://docs.scandit.com/data-capture-sdk/flutter/barcode-capture/api/symbology.html
captureSettings.enableSymbologies({
// Symbology.ean8,
Symbology.ean13Upca,
// Symbology.upce,
// Symbology.qr,
// Symbology.dataMatrix,
// Symbology.code39,
// Symbology.code128,
// Symbology.interleavedTwoOfFive
});
//厳格(strict)モードを有効化
// https://docs.scandit.com/data-capture-sdk/flutter/barcode-capture/symbology-properties.html#symbology-extension-descriptions
var symbologySettings = captureSettings.settingsForSymbology(Symbology.ean13Upca);
symbologySettings.setExtensionEnabled("strict", enabled: true);
// 選択領域を設定
// https://docs.scandit.com/data-capture-sdk/flutter/advanced-topics.html#rectangular-location-selection
captureSettings.locationSelection = RectangularLocationSelection.withSize(SizeWithUnit(
DoubleWithUnit(0.7, MeasureUnit.fraction),
DoubleWithUnit(0.3, MeasureUnit.fraction)));
return captureSettings;
}
DataCaptureView get captureView => _captureView;
void enableBarcodeCapture() {
switchCameraOn();
_barcodeCapture.isEnabled = true;
}
void disableBarcodeCapture() {
_barcodeCapture.isEnabled = false;
switchCameraStandby();
}
void switchCameraOff() {
_camera?.switchToDesiredState(FrameSourceState.off);
}
void switchCameraOn() {
_camera?.switchToDesiredState(FrameSourceState.on);
}
void switchCameraStandby() {
_camera?.switchToDesiredState(FrameSourceState.standby);
}
void dispose() {
_barcodeCapture.removeListener(this);
disableBarcodeCapture();
switchCameraOff();
_singleScanResultController.close();
}
@override
void didScan(BarcodeCapture barcodeCapture, BarcodeCaptureSession session) {
disableBarcodeCapture();
// 読み取ったバーコードの値を通知
var code = session.newlyRecognizedBarcodes[0];
var data = (code.data == null || code.data?.isEmpty == true) ? code.rawData : code.data;
_singleScanResultController.sink.add(data!);
}
@override
void didUpdateSession(BarcodeCapture barcodeCapture, BarcodeCaptureSession session) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment