How to convert Uint8list to List<int> with dart?

abdullah &#231;elik picture abdullah çelik · Nov 20, 2019 · Viewed 11k times · Source

I developed a mobile application with flutter. I do object detection use "controller.startImageStream" this method return CameraImage and i use with object detection. I want to save this image file. I tried to convert this file to List and jpg file for save. But uint8list could not converted to List. Is this structure a true way? If you know different solutions for my problem, please share me.

This is my video streaming method ;

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;
                      });
                  }
                });
          });
        }
    }
  }

This is my image save method ;

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;
  }

Answer

Lucas Matos picture Lucas Matos · Nov 20, 2019

Do it

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

enter image description here

enter image description here