首页 > 解决方案 > 如何将 Uint8list 转换为 List飞镖?

问题描述

我用颤振开发了一个移动应用程序。我做对象检测使用“controller.startImageStream”这个方法返回CameraImage,我使用对象检测。我想保存这个图像文件。我试图将此文件转换为 List 和 jpg 文件进行保存。但 uint8list 无法转换为 List。这种结构是真的吗?如果您知道我的问题的不同解决方案,请分享我。

这是我的视频流方法;

startVideoStreaming() {
    if (cameras == null || cameras.length < 1) {
      print('No camera is found');
    } else {
      controller = new CameraController(
        cameras[0],
          ResolutionPreset.medium,
        );

        if(!_busy){
          controller.initialize().then((_) {

          print("model yükleme bitmiş stream dinleme başlıyor ");

          controller.startImageStream((CameraImage img){
                  print("img format: ${img.format} planes: ${img.planes}");
                  List<int> imageBytes = [];
                  img.planes.map((plane) {
                    imageBytes.addAll(plane.bytes.toList());
                  });
                  
                  // call save image file method
                    saveImageFile(imageBytes).then((res) => {
                      print("save image file successfull filepath: $res")
                    }).catchError((err) => {
                      print("error on save image file error: $err")
                    });
                  
                  if(!isDetecting){
                    isDetecting = true;
                    print("Tflite'a stream gönderildi");
                    Tflite.detectObjectOnFrame(
                        bytesList: img.planes.map((plane) {
                          return plane.bytes;
                        }).toList(),
                        model: "SSDMobileNet",
                        imageHeight: img.height,
                        imageWidth: img.width,
                        imageMean: 127.5,
                        imageStd: 127.5,
                        numResultsPerClass: 1,
                        threshold: 0.4,
                      ).then((recognitions) {
                        int endTime = new DateTime.now().millisecondsSinceEpoch;
                        setState(() {
                          _recognitions=recognitions;
                        });
                        print("Recognitions: $recognitions");
                        isDetecting = false;
                      });
                  }
                });
          });
        }
    }
  }

这是我的图像保存方法;

Future<String> saveImageFile(imageBytes) async {
    final Directory extDir = await getApplicationDocumentsDirectory();
    final String dirPath = '${extDir.path}/Pictures/flutter_test';
    await Directory(dirPath).create(recursive: true);
    final String filePath = '$dirPath/${timestamp()}.jpg';

    if (controller.value.isTakingPicture) {
      // A capture is already pending, do nothing.
      return null;
    }

    try {
      File file = new File(filePath);
      file.writeAsBytes(imageBytes);
      print("finish image saved $imageBytes");
    } on CameraException catch (e) {
      _showCameraException(e);
      return null;
    }
    return filePath;
  }

标签: flutterdarttypeconverteruint8list

解决方案


做吧

var temp = new Uint8List(500);
var list  = new List.from(temp);

在此处输入图像描述

在此处输入图像描述


推荐阅读