Skip to content

Commit

Permalink
improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed Nov 6, 2022
1 parent 20dcd85 commit b3b934f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
22 changes: 17 additions & 5 deletions example/lib/models/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,25 @@ int getColorFromByte(List<int> byte, int index, {bool isLog = false}) {
);
}

int getLuminanceSourcePixel(List<int> byte, int index) {
if (byte.length <= index + 3) {
return 0xff;
}
final r = byte[index] & 0xff; // red
final g2 = (byte[index + 1] << 1) & 0x1fe; // 2 * green
final b = byte[index + 2]; // blue
// Calculate green-favouring average cheaply
return ((r + g2 + b) ~/ 4);
}

List<Result>? decodeImage(IsoMessage message) {
final pixels = List<int>.generate(
message.width * message.height,
(index) => getColorFromByte(message.byteData, index * 4),
);
final pixels = Uint8List(message.width * message.height);
for (int i = 0; i < pixels.length; i++) {
pixels[i] = getLuminanceSourcePixel(message.byteData, i * 4);
}
print(pixels);

final imageSource = RGBLuminanceSource(
final imageSource = RGBLuminanceSource.orig(
message.width,
message.height,
pixels,
Expand Down
41 changes: 34 additions & 7 deletions example/lib/scanner/camera.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:ui' as ui;

import 'package:buffer_image/buffer_image.dart';
import 'package:camera/camera.dart';
Expand Down Expand Up @@ -47,6 +48,8 @@ class _CameraPageState extends State<CameraPage> {
return;
}

_controller!.setFlashMode(_flashMode);

setState(() {
detectedCamera = true;
});
Expand All @@ -58,25 +61,29 @@ class _CameraPageState extends State<CameraPage> {
}
}

BufferImage? image;
Future<void> onCameraView() async {
if (isDetecting) return;
if (isDetecting || !mounted) return;
setState(() {
isDetecting = true;
});
XFile pic = await _controller!.takePicture();

Uint8List data = await pic.readAsBytes();
BufferImage? image = await BufferImage.fromFile(data);
image = await BufferImage.fromFile(data);
if (!mounted) return;
if (image != null) {
if (image.width > 1000) {
image.scaleDown(image.width / 800);
}
setState(() {});

var results =
await decodeImageInIsolate(image.buffer, image.width, image.height);
var results = await decodeImageInIsolate(
image!.buffer,
image!.width,
image!.height,
);
if (!mounted) return;
setState(() {
isDetecting = false;
image = null;
});
if (results != null) {
Navigator.of(context).pushNamed('/result', arguments: results);
Expand All @@ -88,6 +95,7 @@ class _CameraPageState extends State<CameraPage> {
}
} else {
setState(() {
image = null;
isDetecting = false;
});
print('can\'t take picture from camera');
Expand Down Expand Up @@ -142,6 +150,25 @@ class _CameraPageState extends State<CameraPage> {
_controller!,
child: Stack(
children: [
if (image != null)
Positioned(
left: 0,
top: 0,
right: 0,
bottom: 0,
child: FutureBuilder<ui.Image>(
future: image!.getImage(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox.shrink();
}
return RawImage(
image: snapshot.data!,
fit: BoxFit.fill,
);
},
),
),
Align(
alignment: const Alignment(0, 0.7),
child: CupertinoIconButton(
Expand Down
6 changes: 4 additions & 2 deletions example/lib/scanner/camera_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class _CameraStreamPageState extends State<CameraStreamPage> {

bool _isStart = false;
Future<void> start() async {
if (_isStart) return;
if (_isStart || !mounted) return;
await _controller!.startImageStream(tryDecodeImage);
_isStart = true;
}
Expand All @@ -87,19 +87,21 @@ class _CameraStreamPageState extends State<CameraStreamPage> {
}

Future<void> tryDecodeImage(CameraImage image) async {
if (isDetecting) return;
if (isDetecting || !mounted) return;
await stop();
setState(() {
isDetecting = true;
});

try {
final results = await _isoController.setPlanes(image.planes);
if (!mounted) return;
setState(() {
isDetecting = false;
});
Navigator.of(context).pushNamed('/result', arguments: results);
} catch (_) {
if (!mounted) return;
setState(() {
isDetecting = false;
});
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.7.3"
version: "0.8.1"
sdks:
dart: ">=2.17.0 <3.0.0"
flutter: ">=3.0.5"

0 comments on commit b3b934f

Please sign in to comment.