将图像编码为base64字符串

时间:2018-10-17 08:51:32

标签: dart flutter

am尝试使用image_picker ..库从dart的照片库中上传图片,效果很好..

然后尝试将所选图像转换为bas64字符串,如下所示:

imageSelectorGallery() async {
    _image = await ImagePicker.pickImage(
      source: ImageSource.gallery,
      // maxHeight: 50.0,
      // maxWidth: 50.0,
    );
    List<int> imageBytes = _image.readAsBytesSync();
    print(imageBytes);
    String base64Image = base64Encode(imageBytes);
    print('string is');
    print(base64Image);
    print("You selected gallery image : " + _image.path);
    setState(() {});
   }

但是这总是返回不正确的字符串..例如:

  

_9j_4QG1RXhpZgAATU0AKgAAAAgABwEQAAIAAAAaAAAAYgEAAAQAAAABAAADwAEBAAQAAAABAAAFAAEyAAIAAAAUAAAAfAESAAMAAAABAAEAAIdpAAQAAAABAAAAlwEPAAIAAAAHAAAAkAAAAABBbmRyb2lkIFNESyBidWlsdCBmb3IgeDg2ADIwMTg6MTA6MTcgMTE6Mjc6NTMAR29vZ2xlAAAQgp0ABQAAAAEAAAFdgpoABQAAAAEAAAFlkpIAAgAAAAQ0NzQAkpEAAgAAAAQ0NzQAkpAAAgAAAAQ0NzQAkgoABQAAAAEAAAFtkgkAAwAAAAEAAAAAiCcAAwAAAAEAZAAAkAQAAgAAABQAAAF1kAMAAgAAABQAAAGJoAMABAAAAAEAAAUApAMAAwAAAAEAAAAAoAIABAAAAAEAAAPAkgIABQAAAAEAAAGdkgEACgAAAAEAAAGlkAAABwAAAAQwMjIwAAAAAAAAARgAAABkAJiWgDuaygAAABOIAAAD6DIwMTg6MTA6MTcgMTE6Mjc6NTMAMjAxODoxMDoxNyAxMToyNzo1MwAAAAEpAAAAZAAAGfMAAAPo_-AAEEpGSUYAAQEAAAEAAQAA_9sAQwACAQEBAQECAQEBAgICAgIEAwICAgIFBAQDBAYFBgYGBQYGBgcJCAYHCQcGBggLCAkKCgoKCgYICwwLCgwJCgoK_9sAQwECAgICAgIFAwMFCgcGBwoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoK_8AAEQgFAAPAAwEiAAIRAQMRAf_EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC__EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ 2hpanN0dXZ3eHl6g4SFhoeIiYq

并且不能将其转换为图像。

问题在哪里以及如何解决?

1 个答案:

答案 0 :(得分:0)

您仅将编码字符串的前1024个字符复制到问题中。也许const PushNotification = require('push-notification'); const DeviceType = PushNotification.DeviceType; const pn = PushNotification({ apn: { cert: path.resolve('./cert/cert.pem'), key: path.resolve('./cert/key.pem'), production: true, }, gcm: { apiKey: '<API-KEY>' } }); 命令将其截断了。另外,在线工具似乎更喜欢(要求?)URL安全版本,因此正如Günter所说,请使用print

要确认您使用的字符串的长度大致正确,请添加33%。也就是说,一个2k图像将转换为大约2667个字符长的字符串。