Dart:如何编码解码动画 webp 文件?

发布于 2025-01-09 22:39:52 字数 1763 浏览 0 评论 0原文

我正在尝试解码动画 webp 图像文件并将其帧编码为 Dart (cli) 中的动画 webp 图像文件

Dart 代码:

import 'dart:io';
import 'package:image/image.dart';

void main() {
    final image = 'asset/test.webp';

    File bytes = File(image);
    List<int> list = bytes.readAsBytesSync();

    var anim = decodeWebPAnimation(list)!;

    var obj = WebPEncoder();

    for (var frame in anim) {
      obj.addFrame(frame, duration: 1);
    }

    var finished = obj.finish();

    print(finished); // [] empty, no frames get added
    // final encodedAnim = obj.encodeAnimation(); 

    File('asset/output.webp').writeAsBytesSync(finished!,
      flush: true); // pass something like encodeWebp(input)
}

pubspec.yaml

dependencies:
    image: ^3.1.3

输入文件: https://raw.githubusercontent.com/WhatsApp/stickers/main/Android/app/src/main/assets/2/07_OK.webp

输出: 调用finish()后出现零帧,因此输出图像文件无效。即文件大小:0 字节

Dart API 参考:

WebpEncoderhttps://pub.dev/documentation/image/latest/image/ WebPEncoder-class.html

DecodeWebpAnimationhttps://pub.dev/documentation/image/latest/image/decodeWebPAnimation.html

出了什么问题?如何解决这个问题?或者会建议任何图书馆? 谢谢你!

I am trying to decode animated webp image file and encode it's frames to animated webp image file in Dart (cli)

Dart Code:

import 'dart:io';
import 'package:image/image.dart';

void main() {
    final image = 'asset/test.webp';

    File bytes = File(image);
    List<int> list = bytes.readAsBytesSync();

    var anim = decodeWebPAnimation(list)!;

    var obj = WebPEncoder();

    for (var frame in anim) {
      obj.addFrame(frame, duration: 1);
    }

    var finished = obj.finish();

    print(finished); // [] empty, no frames get added
    // final encodedAnim = obj.encodeAnimation(); 

    File('asset/output.webp').writeAsBytesSync(finished!,
      flush: true); // pass something like encodeWebp(input)
}

pubspec.yaml

dependencies:
    image: ^3.1.3

input file: https://raw.githubusercontent.com/WhatsApp/stickers/main/Android/app/src/main/assets/2/07_OK.webp

Output:
There is zero frame after calling finish(), so the output image file is invalid. i.e. file size: 0 byte

Ref of Dart APIs:

WebpEncoder:
https://pub.dev/documentation/image/latest/image/WebPEncoder-class.html

DecodeWebpAnimation: https://pub.dev/documentation/image/latest/image/decodeWebPAnimation.html

What went wrong? How to fix this? or would suggest any library?
Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文