import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui' as ui;

class ImageHepler {
  static Future<String> imageToBase64(ui.Image? image) async {
    if (image == null) {
      return '';
    }
    // 创建一个字节数据列表来保存图像数据
    final ByteData? byteData =
        await image.toByteData(format: ui.ImageByteFormat.png);
    if (byteData == null) {
      throw Exception('Failed to convert image to byte data');
    }

    // 将字节数据转换为 Base64 字符串
    final String base64String = base64Encode(byteData.buffer.asUint8List());
    return base64String;
  }
}
