Flutter转换png图片为jpg图片

1.需求

在xxx产品需求中,需要将png图片转为jpg图片。

2.引用库

Dart 复制代码
 image: ^4.1.3

Dart图像库提供了以各种图像文件格式加载、保存和操作图像的功能。

该库可以与dart:io和dart:html一起用于命令行、Flutter和web应用程序。

注:4.0是该库先前版本的主要修订版。

支持的图像格式

Read/Write

  • JPG
  • PNG / Animated APNG
  • GIF / Animated GIF
  • BMP
  • TIFF
  • TGA
  • PVR
  • ICO

Read Only

  • WebP / Animated WebP
  • PSD
  • EXR

Write Only

  • CUR

https://pub-web.flutter-io.cn/packages/image

3.代码实现

Dart 复制代码
import 'package:image/image.dart' as imglib;

//原图片本地路径 
final cmd = imglib.Command()..decodeImageFile('/xxx/xxx/xx.png');

//image对象
var image = await cmd.getImage();

//转为jpg
var newImg = imglib.encodeJpg(image);
if (newPath != null) {
   File('/xxx/xxx/xx/new.jpg').writeAsBytesSync(newImg);
}
相关推荐
HH思️️无邪2 小时前
Flutter 开发技巧 AI 快速构建 json_annotation model 的提示词
flutter·json
笔沫拾光7 小时前
hooks_riverpod框架解析
flutter·hooks·hooks_riverpod
problc10 小时前
Flutter桌面应用实战:Windows系统代理切换工具开发
windows·flutter
程序员老刘12 小时前
Cursor vs Claude Code vs AS+AI助手:谁才是客户端的编程神器?
flutter·ai编程·客户端
耳東陈15 小时前
【重磅发布】flutter_chen_updater - 版本升级更新
flutter
wordbaby18 小时前
Flutter列表渲染的"诡异"问题:为什么我的数据总是第一个?
前端·flutter
恋猫de小郭19 小时前
谷歌开启 Android 开发者身份验证,明年可能开始禁止“未经验证”应用的侧载,要求所有开发者向谷歌表明身份
android·前端·flutter
苏元1 天前
Flutter + GetX:Dio 多接口 401 拦截后跳登录,避免重复跳转和 Controller 找不到问题
flutter
Mhua_Z2 天前
使用 flutter_tts 的配置项
flutter
你听得到112 天前
弹窗库1.1.0版本发布!不止于统一,更是全面的体验升级!
android·前端·flutter