How to convert a matrix of 64 bit values response to image in Dart

211
  1. Are you getting the image file with extension (.jpg / .png) in url.

  2. Did you try saving your 64bit data as follows (use .png or .jpg as case may be).


Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = new File('$tempPath/image.png');
await file.writeAsBytes(image64BitData);
// Display your image => file
Share:
211
Hưng Nguyễn Trần Gia
Author by

Hưng Nguyễn Trần Gia

Updated on December 25, 2022

Comments

  • Hưng Nguyễn Trần Gia
    Hưng Nguyễn Trần Gia over 1 year

    I have a 'get' rest response as the picture, it returns in 64-bits value array. How can I convert these data to an image in Flutter?

    The content-type is ‘application/octet-stream’ and 64 bits has to be converted to 8 bits to display the grayscale image.

    I have tried base64.decode(response) but it throws me FormatException (FormatException: Invalid base64 data (at character 2)

    [Postman response for 'get' request]: [1]: https://i.stack.imgur.com/FoxKR.png

    Thank you

    [Edit]: This is the request I make:

          final response = await dio.get(
            Consts.baseUrl + endPointUrl,
            queryParameters: {
              '_id': '5f6c5a7e8934b52296b957c5',
              'type': 'contamination'
            },
            options: Options(
                headers: {'Authorization': Consts.apiKey},
                contentType: 'application/octet-stream'),
          );
    
    • Mohamed Al Ameen
      Mohamed Al Ameen over 3 years
      Please provide the screenshot of raw data, not pretty, as it does not have any data, Or else you can provide url & input. Need to know about input data to parse.
    • Hưng Nguyễn Trần Gia
      Hưng Nguyễn Trần Gia over 3 years
      @MohamedAlAmeen I edit the picture to have raw data. The input is a get HTTP request and it returns a matrix of 64 bit values
    • Hưng Nguyễn Trần Gia
      Hưng Nguyễn Trần Gia over 3 years
      Don't know why you said "it does not have any data". Because I can see the response returns a data string with very weird characters. When I printed it out it has a length of 34455 characters
  • Hưng Nguyễn Trần Gia
    Hưng Nguyễn Trần Gia over 3 years
    1. The url is like this "xxx.azurewebsites.net/api/room/…", and it doesn't have any extension 2. The response returns a string of data as I show above, so I don't know how to convert it to List<Int> to input in file.writeAsBytes() function.
  • bluenile
    bluenile over 3 years
    Possibly since the url has no .jpg or .png extension the response is being received as application/octet-stream. Try setting the header Content-type : image/jpeg
  • Hưng Nguyễn Trần Gia
    Hưng Nguyễn Trần Gia over 3 years
    Oh, I have mentioned above about the content-type is ‘application/octet-stream’. Because in the API doc I am working on it says it has to use that specific type to fetch the data. Let me try to change the type
  • Hưng Nguyễn Trần Gia
    Hưng Nguyễn Trần Gia over 3 years
    No, it does not work for me. I suppose we have to convert these data into Array<UInt64> then scale it back to Array<UInt8> then apply color look up table to grayscale image
  • Hưng Nguyễn Trần Gia
    Hưng Nguyễn Trần Gia over 3 years
    Oh, I have figured out what is the problem. In web rest API, I can fetch the data by using response.arrayBuffer() - see this link: stackoverflow.com/questions/53857416/…. So my question now is do we have any solution to use ArrayBuffer for API response in Flutter like web ?