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
相关产品推荐
相关产品推荐

