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);
}
相关推荐
早起的年轻人9 小时前
Flutter String 按 ,。分割
flutter
helloxmg19 小时前
鸿蒙harmonyos next flutter通信之MethodChannel获取设备信息
flutter
helloxmg19 小时前
鸿蒙harmonyos next flutter混合开发之开发package
flutter·华为·harmonyos
lqj_本人2 天前
flutter_鸿蒙next_Dart基础②List
flutter
lqj_本人2 天前
flutter_鸿蒙next_Dart基础①字符串
flutter
The_tuber_sadness2 天前
【Flutter】- 基础语法
flutter
helloxmg2 天前
鸿蒙harmonyos next flutter通信之BasicMessageChannel获取app版本号
flutter
linpengteng3 天前
使用 Flutter 开发数字钱包应用(Dompet App)
前端·flutter·firebase