You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.3 KiB

2 years ago
  1. import 'dart:developer';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:image/image.dart' as image_lib;
  5. import 'package:image_picker/image_picker.dart';
  6. import 'package:palette_generator/palette_generator.dart';
  7. import 'package:tuple/tuple.dart';
  8. class ImageSplitter {
  9. List<Image> splitImage(Map<String, dynamic> mapData) {
  10. List<int> input = mapData['input'];
  11. int size = mapData['size'];
  12. // convert image to image from image package
  13. image_lib.Image image = image_lib.decodeImage(input)!;
  14. int x = 0, y = 0;
  15. int width = (image.width / size).round();
  16. int height = (image.height / size).round();
  17. // split image to parts
  18. List<image_lib.Image> parts = [];
  19. for (int i = 0; i < size; i++) {
  20. for (int j = 0; j < size; j++) {
  21. parts.add(image_lib.copyCrop(image, x, y, width, height));
  22. x += width;
  23. }
  24. x = 0;
  25. y += height;
  26. }
  27. // convert image from image package to Image Widget to display
  28. List<Image> output = [];
  29. for (var img in parts) {
  30. output.add(Image.memory(Uint8List.fromList(image_lib.encodeJpg(img))));
  31. }
  32. return output;
  33. }
  34. Future<PaletteGenerator> getImagePalette(ImageProvider imageProvider) async {
  35. final paletteGenerator = await PaletteGenerator.fromImageProvider(imageProvider);
  36. return paletteGenerator;
  37. }
  38. Future<Tuple3<Image, Uint8List, PaletteGenerator>?> getImage({required ImagePicker picker}) async {
  39. final pickedFile = await picker.pickImage(source: ImageSource.gallery);
  40. if (pickedFile != null) {
  41. // final imageFile = File(pickedFile.path);
  42. // final image = Image.file(File(pickedFile.path));
  43. final image = Image.memory(await pickedFile.readAsBytes());
  44. final palette = await getImagePalette(image.image);
  45. // final PaletteGenerator palette = await getImagePalette(
  46. // FileImage(imageFile),
  47. // );
  48. final bytes = await pickedFile.readAsBytes();
  49. log('Image properties retrieved');
  50. return Tuple3(image, bytes, palette);
  51. }
  52. return null;
  53. }
  54. Future<List<Image>> runSplitterIsolate(List<int> input, int size) async {
  55. final Map<String, dynamic> data = {
  56. 'input': input,
  57. 'size': size,
  58. };
  59. List<Image> splitImages = await compute(splitImage, data);
  60. return splitImages;
  61. }
  62. }