【发布时间】:2022-12-29 14:29:43
【问题描述】:
我正在使用 Flutter camera 包从相机拍摄照片,现在我正在尝试显示从拍摄的照片中裁剪的图像。我使用image包裁剪图片。正如您在以下代码中看到的,我调用了返回 img.Image 数据类型的 copyCrop 函数:
import 'package:image/image.dart' as img;
final XFile imageFile = await _controller.takePicture();
final bytes = await imageFile.readAsBytes();
final uncroppedImage = img.decodeImage(bytes);
final cropPosition = overlayKey.globalPaintBounds;
final img.Image croppedImage =
img.copyCrop(
uncroppedImage,
cropPosition.left.toInt(),
cropPosition.top.toInt(),
cropPosition.width.toInt(),
cropPosition.height.toInt());
我在将裁剪后的图像显示到 Flutter 中的图像小部件时遇到问题。如您所见,img.Image 数据类型与 Flutter 中的 Image 不同。然后我尝试将其转换为 Uint8List,然后使用 Image.memory 将其加载到小部件。
final croppedImageBytes = croppedImage.getBytes(); // return Uint8List
...
// somewhere else
Image.memory(croppedImageBytes);
但是它给了我这样的错误:
E/FlutterJNI(20985): Failed to decode image
E/FlutterJNI(20985): android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error.
E/FlutterJNI(20985): at android.graphics.ImageDecoder.nCreate(Native Method)
E/FlutterJNI(20985): at android.graphics.ImageDecoder.access$200(ImageDecoder.java:173)
E/FlutterJNI(20985): at android.graphics.ImageDecoder$ByteBufferSource.createImageDecoder(ImageDecoder.java:250)
E/FlutterJNI(20985): at android.graphics.ImageDecoder.decodeBitmapImpl(ImageDecoder.java:1863)
E/FlutterJNI(20985): at android.graphics.ImageDecoder.decodeBitmap(ImageDecoder.java:1856)
E/FlutterJNI(20985): at io.flutter.embedding.engine.FlutterJNI.decodeImage(FlutterJNI.java:524)
======== Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:5886:5)
#1 ImageDescriptor.encoded (dart:ui/painting.dart:5741:12)
#2 instantiateImageCodecFromBuffer (dart:ui/painting.dart:2092:60)
#3 PaintingBinding.instantiateImageCodecFromBuffer (package:flutter/src/painting/binding.dart:153:15)
#4 MemoryImage._loadAsync (package:flutter/src/painting/image_provider.dart:1090:20)
<asynchronous suspension>
====================================================================================================
但是,每当我直接使用 Uint8List 加载图像(不裁剪)时,它仍然可以正常工作。有什么解决方案可以解决这个问题吗?
【问题讨论】:
标签: flutter base64 byte image-flutter