133 lines
3.2 KiB
Dart
133 lines
3.2 KiB
Dart
import 'dart:async';
|
|
import 'package:camera/camera.dart';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
List<CameraDescription> cameras = [];
|
|
|
|
Future<void> main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
cameras = await availableCameras();
|
|
runApp(CameraApp());
|
|
}
|
|
|
|
class CameraApp extends StatefulWidget {
|
|
@override
|
|
_CameraAppState createState() => _CameraAppState();
|
|
}
|
|
|
|
class _CameraAppState extends State<CameraApp> {
|
|
CameraController? controller;
|
|
var streaming = false;
|
|
var image_width = 0;
|
|
var image_height = 0;
|
|
var posy = 0.0;
|
|
var posx = 0.0;
|
|
Uint8List? previousImage;
|
|
Uint8List? diff;
|
|
Uint8List? dst;
|
|
Float32List? background;
|
|
Float64List? sums;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
controller = CameraController(cameras[1], ResolutionPreset.low);
|
|
controller!.initialize().then((_) {
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
setState(() {});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller?.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final CameraController cameraController = controller!;
|
|
if (!cameraController.value.isInitialized) {
|
|
return Container();
|
|
}
|
|
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Home'),
|
|
),
|
|
body: Column(children: <Widget>[
|
|
CameraPreview(cameraController),
|
|
]),
|
|
floatingActionButton: FloatingActionButton(
|
|
child: const Icon(Icons.plus_one),
|
|
onPressed: () {
|
|
startStream();
|
|
},
|
|
),
|
|
),
|
|
debugShowCheckedModeBanner: false,
|
|
);
|
|
}
|
|
|
|
void startStream() {
|
|
final CameraController? cameraController = controller;
|
|
|
|
if (cameraController == null || !cameraController.value.isInitialized) {
|
|
return;
|
|
}
|
|
|
|
if (streaming == false) {
|
|
cameraController.startImageStream(handleImage);
|
|
} else {
|
|
cameraController.stopImageStream();
|
|
}
|
|
|
|
setState(() {
|
|
streaming = !streaming;
|
|
});
|
|
}
|
|
|
|
void handleImage(CameraImage src) {
|
|
sums = Float64List(src.height);
|
|
|
|
if (previousImage == null || background == null) {
|
|
previousImage = Uint8List(src.planes[0].bytes.length);
|
|
dst = Uint8List(src.planes[0].bytes.length);
|
|
diff = Uint8List(src.planes[0].bytes.length);
|
|
background = Float32List(src.planes[0].bytes.length);
|
|
image_width = src.width;
|
|
image_height = src.height;
|
|
} else {
|
|
for (var i = 0; i < src.width; i++) {
|
|
for (var j = 0; j < src.height; j++) {
|
|
//Background calculation
|
|
background![i + j] = (background![i + j] * 0.99 +
|
|
src.planes[0].bytes[i + j].toDouble() * (1.0 - 0.99));
|
|
|
|
//Difference calculation
|
|
var diff_val =
|
|
(background![i + j].toInt() - src.planes[0].bytes[i + j]).abs();
|
|
if (diff_val > 50) {
|
|
diff![i + j] = diff_val;
|
|
} else {
|
|
diff![i + j] = 0;
|
|
}
|
|
|
|
//Center of motion calc
|
|
sums[i] +=
|
|
}
|
|
}
|
|
// Processing
|
|
print(diff![0]);
|
|
// End Processing
|
|
previousImage = src.planes[0].bytes;
|
|
}
|
|
}
|
|
}
|