You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter图片旋转后背景变黑问题求助:需改为白色背景

解决Flutter图片旋转后背景变黑的问题

你只需要在copyRotate方法中指定白色背景即可,默认背景色为黑色,这就是问题的根源。

修改后的完整代码如下:

Future rotateImage(String imagePath, double rotationValue) async {
  File imageFile = File(imagePath);

  List<int> imageBytes = await imageFile.readAsBytes();
  var originalImage = img.decodeImage(Uint8List.fromList(imageBytes));
  
  // 指定白色背景
  img.Image fixedImage = img.copyRotate(originalImage!,
          angle: rotationValue, 
          interpolation: img.Interpolation.average,
          background: img.Color.fromRgb(255, 255, 255)); // 新增背景色配置
  
  var fixedFile = await imageFile.writeAsBytes(img.encodePng(fixedImage));

  return fixedFile;
}

补充说明:

  • img.copyRotate的background参数负责设置旋转后空白区域的颜色,默认值是黑色(img.Color.fromRgb(0,0,0))
  • 替换为img.Color.fromRgb(255,255,255)即可得到白色背景
  • 原代码中decodedImage变量未被使用,可直接移除以精简代码

内容的提问来源于stack exchange,提问作者Yusuf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 05:45:37