camera_worker 1.0.0 copy "camera_worker: ^1.0.0" to clipboard
camera_worker: ^1.0.0 copied to clipboard

PlatformAndroidiOS
outdated

A library that converts streaming images from a camera to luminance information for easy processing, and supports parallel processing by Isolate.

example/lib/main.dart

import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:camera_worker/camera_worker.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Scaffold(body: MyHomePage()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final controller = WorkerController<Luminances>(_workerMethod);
  final ValueNotifier<ui.Image?> image = ValueNotifier(null);

  @override
  void initState() {
    controller.init(resolution: ResolutionPreset.high).then((_) {
      setState(() {});
    });
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void _buttonPressed() async {
    if (controller.started) {
      return controller.stop();
    } else {
      final stream = await controller.start(maxWidth: 800);
      bool skip = false;
      stream.listen((e) {
        if (skip) return;
        skip = true;
        final luminances = e;
        if (luminances != null) {
          controller.makeImage(luminances).then((value) {
            image.value = value;
            skip = false;
          });
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final camera = controller.camera;
    return Scaffold(
      body: Stack(
        children: [
          camera == null ? const SizedBox.shrink() : CameraPreview(camera),
          ValueListenableBuilder<ui.Image?>(
            valueListenable: image,
            builder: (context, image, _) {
              if (image == null) {
                return const SizedBox.shrink();
              }
              return Transform.scale(
                scale: 0.5,
                alignment: Alignment.bottomLeft,
                child: RawImage(
                  image: image,
                  width: image.width.toDouble(),
                  height: image.height.toDouble(),
                  colorBlendMode: BlendMode.src,
                  alignment: Alignment.bottomLeft,
                ),
              );
            },
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _buttonPressed(),
        child: const Icon(Icons.camera),
      ),
    );
  }
}

Luminances _workerMethod(Luminances lumi) {
  // Perform heavy processing such as image recognition
  return lumi;
}
0
likes
150
points
46
downloads

Publisher

verified publisherzuvola.com

Weekly Downloads

A library that converts streaming images from a camera to luminance information for easy processing, and supports parallel processing by Isolate.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

camera, flutter, isoworker

More

Packages that depend on camera_worker