base.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'dart:typed_data';
  2. import 'dart:ui' as ui;
  3. import 'package:flutter/painting.dart';
  4. import 'package:image/image.dart' as img;
  5. import 'package:vid/us/vid_us_image.dart';
  6. abstract class VidFrameProcessor {
  7. Uint8List process(Uint8List bytes, int position);
  8. static Future<VidUsImage> processFrame(
  9. VidUsImage frame,
  10. List<VidFrameProcessor>? processors,
  11. ) async {
  12. if (processors == null || processors.isEmpty) return frame;
  13. try {
  14. final start = DateTime.now();
  15. final bitmap = await decodeImageFromList(frame.imageData);
  16. final buffer = (await bitmap.toByteData())!.buffer.asUint8List();
  17. final width = bitmap.width, height = bitmap.height;
  18. if (processors.length == 1) {
  19. final processor = processors.first;
  20. for (int x = 0; x < width; x++) {
  21. for (int y = 0; y < height; y++) {
  22. int position = ((y * width) + x) * 4;
  23. processor.process(buffer, position);
  24. }
  25. }
  26. } else {
  27. for (final processor in processors) {
  28. for (int x = 0; x < width; x++) {
  29. for (int y = 0; y < height; y++) {
  30. int position = ((y * width) + x) * 4;
  31. processor.process(buffer, position);
  32. }
  33. }
  34. }
  35. }
  36. final dstImg = img.Image.fromBytes(width, height, buffer);
  37. final jpgData = img.encodeJpg(dstImg);
  38. final dstBuffer = Uint8List.fromList(jpgData);
  39. final end = DateTime.now();
  40. final snap = end.difference(start).inMilliseconds;
  41. // print(snap);
  42. return VidUsImage(frame.index, width, height, dstBuffer);
  43. } catch (e) {
  44. // ignore
  45. }
  46. return frame;
  47. }
  48. }