feat: работа с вложениями
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
|
||||
@@ -12,6 +13,14 @@ abstract class GalleryItem {
|
||||
File? get localFile;
|
||||
Future<Uint8List?> thumbnail(int size);
|
||||
Future<File?> originFile();
|
||||
Future<(int, int)?> dimensions();
|
||||
}
|
||||
|
||||
class PickedPhoto {
|
||||
final GalleryItem item;
|
||||
final File? editedFile;
|
||||
|
||||
const PickedPhoto({required this.item, this.editedFile});
|
||||
}
|
||||
|
||||
abstract class GallerySource {
|
||||
@@ -83,6 +92,14 @@ class _AssetGalleryItem implements GalleryItem {
|
||||
|
||||
@override
|
||||
Future<File?> originFile() => asset.file;
|
||||
|
||||
@override
|
||||
Future<(int, int)?> dimensions() async {
|
||||
if (asset.width > 0 && asset.height > 0) {
|
||||
return (asset.width, asset.height);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class _DesktopGallerySource implements GallerySource {
|
||||
@@ -159,4 +176,19 @@ class _FileGalleryItem implements GalleryItem {
|
||||
|
||||
@override
|
||||
Future<File?> originFile() async => file;
|
||||
|
||||
@override
|
||||
Future<(int, int)?> dimensions() async {
|
||||
try {
|
||||
final bytes = await file.readAsBytes();
|
||||
final codec = await ui.instantiateImageCodec(bytes);
|
||||
final frame = await codec.getNextFrame();
|
||||
final result = (frame.image.width, frame.image.height);
|
||||
frame.image.dispose();
|
||||
codec.dispose();
|
||||
return result;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,22 @@ const int kMaxAvatarBytes = 8 * 1024 * 1024;
|
||||
|
||||
Future<Uint8List?> compressAvatar(Uint8List input) => compute(_encodeAvatar, input);
|
||||
|
||||
Future<Uint8List?> encodeRgbaToJpeg(Uint8List rgba, int width, int height) =>
|
||||
compute(_encodeRgba, (rgba, width, height));
|
||||
|
||||
Uint8List? _encodeRgba((Uint8List, int, int) args) {
|
||||
final (rgba, width, height) = args;
|
||||
if (width <= 0 || height <= 0) return null;
|
||||
final image = img.Image.fromBytes(
|
||||
width: width,
|
||||
height: height,
|
||||
bytes: rgba.buffer,
|
||||
numChannels: 4,
|
||||
order: img.ChannelOrder.rgba,
|
||||
);
|
||||
return img.encodeJpg(image, quality: 90);
|
||||
}
|
||||
|
||||
Uint8List? _encodeAvatar(Uint8List input) {
|
||||
final decoded = img.decodeImage(input);
|
||||
if (decoded == null) return null;
|
||||
|
||||
Reference in New Issue
Block a user