Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
  }
like image 613
abdullah çelik Avatar asked Nov 20 '19 12:11

abdullah çelik


People also ask

What is Uint8List in dart?

A fixed-length list of 8-bit unsigned integers. For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.


1 Answers

Flutter now have the method for converting List<int> to Uint8List. You can use the following:

Uint8List.fromList(List<int> elements);

See https://api.flutter.dev/flutter/dart-typed_data/Uint8List/Uint8List.fromList.html

like image 163
ישו אוהב אותך Avatar answered Nov 10 '22 23:11

ישו אוהב אותך