Converting image files to VARBINARY(max) in flutter

315

The problem was that I'm not decoding it in the backend before inserting it into the database.

I'm Using Python "FAST API" and I needed to write the following line to attachmentdata before inserting it into the database.

attachment.attachmentData = base64.decodebytes(attachment.attachmentData)
Share:
315
Ramy Gomaa
Author by

Ramy Gomaa

Updated on January 01, 2023

Comments

  • Ramy Gomaa
    Ramy Gomaa over 1 year

    I need to store images that are captured using "image picker" in a SQL server database

    final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 25);
    

    now, I know that the images should be stored in a "VARBINARY" datatype. I tried to convert the XFile "image" that I got from the "ImagePicker()" to VARBINARY and these are the steps I made.

    final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 25);
    
    var imageTemp = File(image.path);
    

    and then I store the image in the database using the following.

    Attachment(
                attachmentData: base64Encode((imageTemp .readAsBytesSync())),
                attachmentName: imageTemp .path.split('/').last,
                attachmentSize: imageTemp .readAsBytesSync().lengthInBytes,
                attachmentType: imageTemp .path.split(".").last,
              )
    

    "attachmentData: base64Encode((imageTemp .readAsBytesSync()))" "attachmentData is a String" So from XFile to VARBINARY, I had done the following

     final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 25);
     var imageTemp = File(image.path);
    
      base64Encode((imageTemp .readAsBytesSync())) //the sent data 
    

    when I print the data after the readAsByteSynce() it starts with [255,216,.255,..] which is FF D8 FF it looks like this screen shot of print(imageTemp.readAsBytesSync())

    and when I print the image after using base64Encode it starts with /9j/4Q it looks like this screen shot of print(base64Encode((imageTemp.readAsBytesSync())))

    now when it's stored in the SQL server database it starts with 0x2F39 it looks like this screen shot of the attachmebtData field in the SQL server database

    The image extension is .jpg. Shouldn't jpg images start with 0xFF? I need to take these images and view them in ArcGIS and when I open the image it says that the images are not in a supported format.

    can someone please tell me what am I doing wrong and why are my jpg images starts with 0x2F in the database and not 0xFFD8FF and how to fix this? I want the image to be stored in the database

    I also Tried converting Uint8List to hex using dart package hex 0.2.0 which converted it to this the output of print(Hex.encode(await image.readAsBytes))

    as you can see in the above picture it starts with ffd8ff which is the correct format for jpg pictures, but when I send this to the database it looks like it converts these letters to VARBINARY and the output starts with 0x666666 and looks like this image from SQL server AttachmentData field when Hex.encode data was sent to it

    • AlwaysLearning
      AlwaysLearning over 2 years
      0x2F39 are the bytes for characters /9. You're base64Encode-ing the image data bytes to send it - but where are you base64Decode-ing it before inserting into the database?
    • Ramy Gomaa
      Ramy Gomaa over 2 years
      Dude, the problem was me not decoding it. after your comment and I to the API and decoded it before inserting it into the database. thanks a lot. I'm using python, and this is what I did. "attachment.attachmentData = base64.decodebytes(attachment.attachmentData)" Can you please add your comment as an answer so I can approve it?