搜索插件、代码仓与 APP 分类

插件详情v1.0.1

jpeg_encode

一个极简的JPEG编码器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
4900
喜欢数
19
Pub 分数
120

安装插件

flutter pub add jpeg_encode

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个为 Flutter 设计的极简 JPEG 编码器,允许开发者在多个平台上压缩并编码图像为 JPEG 格式。该工具以简洁为核心理念,提供轻量级解决方案,将图像数据转换为 JPEG 格式,无需复杂依赖。适用于需要高效图像压缩或以广泛支持的格式保存图像的应用程序。该包支持 Android、iOS、Web、macOS、Windows 和 Linux,确保广泛的兼容性。在文件大小缩减至关重要的场景中尤为有用。

适合场景

  • 移动应用中的图像压缩
  • 将捕获的照片保存为JPEG
  • 减小图像文件大小以用于网络上传
  • 图像资产的批处理
  • 将JPEG编码集成到跨平台应用程序中

主要特点

  • 轻量且简洁的 API
  • 跨平台支持
  • 从图像数据进行简单编码
  • 高效压缩
  • 无外部原生依赖

适合对象

  • 需要基本JPEG编码的开发人员
  • 专注于图像优化的应用程序
  • 跨平台多媒体应用
  • 需要小尺寸编码器的项目

使用建议

  • 仅限JPEG格式
  • 没有像质量调节之类的高级编码选项
  • 不适合复杂的图像处理
  • 取决于底层平台的图像处理能力

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

JPEG encoder

pub package

Skia, the engine behind dart:ui has no support for JPEG, only PNG and raw RGBA. And the source code comments make it clear that they don't intend to add any more formats, those should be left to external packages.

While there are packages that offer full support for image manipulation, many a time we don't need that, just to be able to convert a file to JPG for use outside of our app, maybe to send it to the web. This package does that and nothing else. It's a minimalistic JPEG encoder that doesn't offer any extra functionality but to create the simplest JPEG file. Not necessarily the best or most compressed one, although it's fairly good, nonetheless.

The code is just a straight Dart port of an existing implementation of Jon Olick from https://www.jonolick.com/code.html.

How to use

Let's suppose you have a dart:ui Image from somewhere, maybe you converted from an incoming image:

import "dart:ui" as ui;

Uint8List bytes;
final codec = await ui.instantiateImageCodec(bytes);
final frame = await codec.getNextFrame();
final image = frame.image;

And you want to convert it to JPG (quality is between 0 and 100, as usual):

final data = await image.toByteData(format: skia.ImageByteFormat.rawRgba);
final jpg = JpegEncoder().compress(data!.buffer.asUint8List(), image.width, image.height, 90);

In other words, the pixel array is expected to be in row major format (all columns of first row, then of the second row, etc), four bytes for each pixel: R, G, B and A. Alpha is ignored but the byte is expected to be there. Consequently, the length of the array has to be width * height * 4.

Before you ask, saving in Flutter is as simple as:

File(path).writeAsBytesSync(jpg); // or
await File(path).writeAsBytes(jpg);

Support

If you like this package, please consider supporting it.

Buy Me A Book

所属分类

源码仓库

使用此插件的项目

暂无相关数据。