123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import 'dart:typed_data';
- import 'dart:ui' as ui;
- import 'package:flutter/painting.dart';
- import 'package:image/image.dart' as img;
- import 'package:vid/us/vid_us_image.dart';
- abstract class VidFrameProcessor {
- Uint8List process(Uint8List bytes, int position);
- static Future<VidUsImage> processFrame(
- VidUsImage frame,
- List<VidFrameProcessor>? processors,
- ) async {
- if (processors == null || processors.isEmpty) return frame;
- try {
- final start = DateTime.now();
- final bitmap = await decodeImageFromList(frame.imageData);
- final buffer = (await bitmap.toByteData())!.buffer.asUint8List();
- final width = bitmap.width, height = bitmap.height;
- if (processors.length == 1) {
- final processor = processors.first;
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int position = ((y * width) + x) * 4;
- processor.process(buffer, position);
- }
- }
- } else {
- for (final processor in processors) {
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int position = ((y * width) + x) * 4;
- processor.process(buffer, position);
- }
- }
- }
- }
- final dstImg = img.Image.fromBytes(width, height, buffer);
- final jpgData = img.encodeJpg(dstImg);
- final dstBuffer = Uint8List.fromList(jpgData);
- final end = DateTime.now();
- final snap = end.difference(start).inMilliseconds;
- // print(snap);
- return VidUsImage(frame.index, width, height, dstBuffer);
- } catch (e) {
- // ignore
- }
- return frame;
- }
- }
|