简体   繁体   English

将 base64 字符串转换为图像 class 并返回

[英]Convert base64 string to Image class and back

I have a simple table, which I would like to create and maintain via MOOR .我有一个简单的表,我想通过MOOR创建和维护它。 Below is the table definition:下面是表定义:

class Plants extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get name => text()();
  TextColumn get image => text().map(const ImageConverter()).nullable()();
}

As you can see, I have an image field, and I would like to use Image to base64 conversion and back.如您所见,我有一个image字段,我想使用Imagebase64转换并返回。 Please, note I refer to Flutter Image class , not the widget.请注意,我指的是 Flutter Image class ,而不是小部件。 I already found the topic , where conversion between Image widget and base64 took place, but it does not seem to me right from an architectural point of view to use widget on the data layer.我已经找到了主题,其中Image 小部件base64之间发生了转换,但从架构的角度来看,在数据层上使用小部件似乎并不正确。 This is what I have done so far:这是我到目前为止所做的:

import 'package:image/image.dart';
import 'dart:convert';
import 'dart:typed_data';
import 'package:moor/moor.dart';

class ImageConverter extends TypeConverter<Image, String> {
  Image _imageFromBase64String(String base64String) {
    return null; // <--- missing implementation
  }

  Uint8List _dataFromBase64String(String base64String) {
    return base64Decode(base64String);
  }

  String _base64String(Uint8List data) {
    return base64Encode(data);
  }

  const ImageConverter();
  @override
  Image mapToDart(String fromDb) {
    if (fromDb == null) {
      return null;
    }
    return _imageFromBase64String(fromDb);
  }

  @override
  String mapToSql(Image value) {
    if (value == null) {
      return null;
    }

    return _base64String(value.getBytes());
  }
}

As you can see, I have no problem (I guess) to implement Image to base64 conversion, I struggle however to implement base64 to Image .正如你所看到的,我没有问题(我猜)实现Imagebase64转换,但是我很难实现base64Image

Note that the link in the question is incorrect.请注意,问题中的链接不正确。 The question refers to the Image class from the image package - see https://pub.dev/documentation/image/latest/image/Image-class.html The question refers to the Image class from the image package - see https://pub.dev/documentation/image/latest/image/Image-class.html

You need a pair of functions like this:你需要一对这样的函数:

Image b64ToImage(int width, int height, String b64) {
  return Image.fromBytes(width, height, base64.decode(b64));
}

String imageToB64(Image image) => base64.encode(image.getBytes());

getBytes returns you an array of NxM 32 bit pixels (by default in RGBA order) but gives you no idea of the geometry of the image. getBytes为您返回一个 NxM 32 位像素数组(默认情况下按 RGBA 顺序),但您不知道图像的几何形状。 A 100x200 image will return an 80,000 byte array - but so will a 200x100 image. 100x200 图像将返回 80,000 字节数组 - 但 200x100 图像也将返回。 When you re-construct the image you need to tell it the original width and height.当您重新构建图像时,您需要告诉它原始的宽度和高度。

This is why file formats like BMP exist.这就是存在像 BMP 这样的文件格式的原因。 They prefix the pixel array with a header that contain information like width, height, pixel depth, pixel order, etc. The package has a BMP decoder, but no encoder.他们在像素阵列前面加上 header,其中包含宽度、高度、像素深度、像素顺序等信息。package 有一个 BMP 解码器,但没有编码器。 You could implement a simple BMP encoder if you wanted to encode the width and height into the byte array and thus into the base64 string.如果您想将宽度和高度编码到字节数组中,从而编码到 base64 字符串中,您可以实现一个简单的 BMP 编码器。 (See this partial answer .) (请参阅此部分答案。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM