Skip to main content

yuv_ffi 0.4.0 copy "yuv_ffi: ^0.4.0" to clipboard
yuv_ffi: ^0.4.0 copied to clipboard

retracted

High-performance YUV/BGRA image processing for Flutter via native C/FFI.

example/lib/main.dart

import 'dart:async';
import 'dart:ui' as ui;

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_mlkit_face_detection/google_mlkit_face_detection.dart';
import 'package:image_picker/image_picker.dart';
import 'package:yuv_ffi/yuv_ffi.dart';
import 'package:yuv_ffi_example/camera_screen.dart';
import 'package:yuv_ffi_example/ext.dart';
import 'package:yuv_ffi_example/widgets/crop_targets.dart';
import 'package:yuv_ffi_example/widgets/face_rect_paint.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await YuvFfi.initialize();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  YuvImage? original;

  bool get originalExists => original != null;

  YuvImage? image;

  YuvImage get requireImage => image!;

  bool isLoading = false;

  int? lastOpTiming;
  Rect? faceBox;

  @override
  Widget build(BuildContext context) {
    var baseStyle = (Theme.of(context).textTheme.labelSmall ?? const TextStyle(fontSize: 11)).copyWith(
      // color: Colors.cyanAccent,
      foreground: ui.Paint()
        ..blendMode = ui.BlendMode.difference
        ..color = Colors.white,
    );
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Builder(
        builder: (context) {
          return Scaffold(
            appBar: AppBar(
              forceMaterialTransparency: true,
              title: const Text('YUV FFI'),
              actions: [
                IconButton(onPressed: () => takePhoto(context), icon: Icon(Icons.camera), tooltip: 'Take photo'),
                IconButton(onPressed: () => loadExisting(), icon: Icon(Icons.undo), tooltip: 'Load existing'),
                IconButton(onPressed: () => loadImage(), icon: Icon(Icons.drive_folder_upload), tooltip: 'Load image'),
              ],
            ),
            body: Stack(
              fit: StackFit.expand,
              children: [
                Positioned.fill(child: ColoredBox(color: Colors.black)),
                Positioned.fill(
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Expanded(
                        child: _ImageWidget(image: image, faceBox: faceBox),
                      ),
                      Container(
                        color: Colors.white,
                        padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12),
                        child: Wrap(
                          spacing: 12,
                          children: [
                            IconButton(onPressed: () => rotateClockWise(), icon: Icon(Icons.rotate_right, size: 32), tooltip: 'Rotate Clockwise'),
                            IconButton(
                              onPressed: () => rotateCouterClockwise(),
                              icon: Icon(Icons.rotate_left, size: 32),
                              tooltip: 'Rotate Couterclockwise',
                            ),
                            IconButton(
                              onPressed: () => flipImageVertically(),
                              icon: Icon(CupertinoIcons.arrow_up_arrow_down, size: 32),
                              tooltip: 'Flip vertically',
                            ),
                            IconButton(
                              onPressed: () => flitImageHorizontally(),
                              icon: Icon(CupertinoIcons.arrow_left_right, size: 32),
                              tooltip: 'Flip horizontally',
                            ),
                            IconButton(onPressed: () => cropImage(), icon: Icon(Icons.crop, size: 32), tooltip: 'crop image'),
                            IconButton(onPressed: () => grayscaleImage(), icon: Icon(CupertinoIcons.color_filter, size: 32), tooltip: 'Grayscale'),
                            IconButton(onPressed: () => blackwhiteImage(), icon: Icon(Icons.filter_b_and_w, size: 32), tooltip: 'Black&White'),
                            IconButton(onPressed: () => invertImage(), icon: Icon(Icons.invert_colors, size: 32), tooltip: 'Negate'),
                            IconButton(onPressed: () => gaussianBlurImage(), icon: Icon(Icons.blur_on, size: 32), tooltip: 'Gaussian blur'),
                            IconButton(onPressed: () => meanBlurImage(), icon: Icon(Icons.blur_linear, size: 32), tooltip: 'Mean blur'),
                            IconButton(onPressed: () => boxBlurImage(), icon: Icon(Icons.crop_square, size: 32), tooltip: 'Box blur'),
                            IconButton(onPressed: () => doFaceDetection(), icon: Icon(Icons.face_outlined, size: 32), tooltip: 'Face detection'),
                            IconButton(
                              onPressed: () => toI420(),
                              icon: Text('To i420', style: TextStyle(fontSize: 12)),
                              tooltip: 'To i420',
                            ),
                            IconButton(
                              onPressed: () => toNV21(),
                              icon: Text('To Nv21', style: TextStyle(fontSize: 12)),
                              tooltip: 'To NV21',
                            ),
                            IconButton(
                              onPressed: () => toBGRA(),
                              icon: Text('To BGRA', style: TextStyle(fontSize: 12)),
                              tooltip: 'To BGRA8888',
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                if (lastOpTiming != null) Positioned(right: 8, top: 8, child: Text('$lastOpTiming msec', style: baseStyle)),
                if (image != null) Positioned(left: 8, top: 8, child: Text('$image', style: baseStyle)),
                if (isLoading) Center(child: CircularProgressIndicator()),
              ],
            ),
          );
        },
      ),
    );
  }

  Future takePhoto(BuildContext context) async {
    final result = await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) {
          return CameraScreen();
        },
        settings: RouteSettings(name: 'camera'),
      ),
    );

    if (result is! YuvImage) {
      return;
    }

    await Future.delayed(Duration(seconds: 1));

    setState(() {
      image = result;
      original = result.copy();
      faceBox = null;
      isLoading = false;
    });
  }

  Future loadExisting() async {
    if (!originalExists) {
      return;
    }

    setState(() {
      isLoading = false;
      image = original!.copy();

      // image = image!.applyFormat(YuvPixelFormat.i420);
      // image = image!.applyFormat(YuvPixelFormat.nv12);

      faceBox = null;
    });
  }

  void rotateClockWise() {
    logTimed(() => requireImage.applyRotation(YuvImageRotation.rotation90), name: '$image rotateClockWise');
  }

  void rotateCouterClockwise() {
    logTimed(() => requireImage.applyRotation(YuvImageRotation.rotation270), name: '$image rotateCouterClockwise');
  }

  Future flipImageVertically() async {
    logTimed(() async => requireImage.applyFlipVertical(), name: '$image flipVertically');
  }

  void flitImageHorizontally() {
    logTimed(() => requireImage.applyFlipHorizontal(), name: '$image flitHorizontally');
  }

  void cropImage() {
    var cropTarget = CropTarget.percented(top: .15, bottom: .75, left: .15, right: .85);
    var r = cropTarget.place(requireImage.size);

    logTimed(() => requireImage.applyCrop(r), name: '$image cropImage');
  }

  void grayscaleImage() {
    logTimed(() => requireImage.applyGrayscale(), name: '$image grayscaleImage');
  }

  void blackwhiteImage() {
    logTimed(() => requireImage.applyBlackWhite(), name: '$image blackwhiteImage');
  }

  void invertImage() {
    logTimed(() => requireImage.applyNegate(), name: '$image invertImage');
  }

  void gaussianBlurImage() {
    logTimed(() => requireImage.applyGaussianBlur(radius: 10, sigma: 10), name: '$image gaussianBlurImage');
  }

  void meanBlurImage() {
    logTimed(() => requireImage.applyMeanBlur(radius: 10), name: '$image meanBlurImage');
  }

  Future logTimed(FutureOr Function() execution, {String? name}) async {
    var t = DateTime.now();
    await execution();
    var d = t.difference(DateTime.now()).abs().inMilliseconds;
    lastOpTiming = d;
    debugPrint('${name ?? ''} > $d msec');
    setState(() {});
  }

  void boxBlurImage() {
    logTimed(() => requireImage.applyBoxBlur(radius: 10), name: '$image boxBlurImage');
  }

  Future loadImage() async {
    final xfile = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (xfile == null) {
      return;
    }
    setState(() {
      isLoading = true;
    });

    Uint8List? bytes = await xfile.readAsBytes();
    final ui.Codec codec = await ui.instantiateImageCodec(bytes);
    final ui.FrameInfo frame = await codec.getNextFrame();
    final ui.Image img = frame.image;

    ByteData? rgbaBytes = await img.toByteData(format: ui.ImageByteFormat.rawRgba);
    bytes = null;
    img.dispose();

    if (rgbaBytes == null) throw Exception('Could not decode image');
    setState(() {
      image = YuvImage.bgra(img.width, img.height)..applyRgbaBytes(rgbaBytes.buffer.asUint8List());
      original = image!.copy();
      faceBox = null;
      isLoading = false;
    });
  }

  Future doFaceDetection() async {
    final FaceDetector detector = FaceDetector(
      options: FaceDetectorOptions(enableClassification: true, performanceMode: FaceDetectorMode.accurate, enableTracking: true),
    );

    final yuvForMlInput = (!kIsWeb && defaultTargetPlatform == TargetPlatform.iOS)
        ? requireImage.copy().applyFormat(YuvPixelFormat.bgra8888)
        : requireImage.copy().applyFormat(YuvPixelFormat.nv12);
    final inputImage = yuvForMlInput.toInputImage();

    final faces = await detector.processImage(inputImage);
    if (faces.isEmpty) {
      faceBox = null;
    } else {
      faces.sort((a, b) => b.boundingBox.area.compareTo(a.boundingBox.area));
      final face = faces.first;
      faceBox = face.boundingBox;
    }

    setState(() {});
  }

  Future toI420() async {
    logTimed(() => requireImage.applyFormat(YuvPixelFormat.i420), name: '$image toI420');
  }

  Future toNV21() async {
    logTimed(() => requireImage.applyFormat(YuvPixelFormat.nv12), name: '$image toNV21');
  }

  Future toBGRA() async {
    logTimed(() => requireImage.applyFormat(YuvPixelFormat.bgra8888), name: '$image toBGRA');
  }
}

class _ImageWidget extends StatelessWidget {
  final YuvImage? image;
  final ui.Rect? faceBox;

  const _ImageWidget({required this.image, required this.faceBox});

  @override
  Widget build(BuildContext context) {
    if (image == null) {
      return SizedBox();
    }

    final i = image!;

    return FittedBox(
      fit: BoxFit.contain,
      alignment: Alignment.center,
      clipBehavior: Clip.antiAlias,
      child: SizedBox(
        width: i.width.toDouble(),
        height: i.height.toDouble(),
        child: Stack(
          clipBehavior: Clip.antiAlias,
          fit: StackFit.expand,
          children: [
            YuvImageWidget(image: i, boxFit: BoxFit.none),
            if (faceBox != null)
              CustomPaint(
                painter: FaceRectPainter(rect: faceBox!, image: i, strokeWidth: 10),
              ),
          ],
        ),
      ),
    );
  }
}

extension on Rect {
  double get area => width * height;
}
2
likes
0
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

High-performance YUV/BGRA image processing for Flutter via native C/FFI.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

ffi, flutter, flutter_web_plugins, plugin_platform_interface

More

Packages that depend on yuv_ffi

Packages that implement yuv_ffi