Converting image files to VARBINARY(max) in flutter - sql

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

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)

Related

Fetch images which are stored in filestore odoo 11

How can I Fetch images which are stored in file-store odoo-11?
I am trying to fetch the product.template image, which is stored in ir_attachment in the format 39/39abfeca081b17a6b93fbeaeead3e34025a39f9c.
This is not a binary code. I tried this code in this URL. It didn't give any image. Later, I understood that this is a code in file store. When we download a Database in zip format and extract the DB we will see the file-store inside this folder "39" is a folder name and 39abfeca081b17a6b93fbeaeead3e34025a39f9c is an image name.
My Requirement is Product Image will be fetched from another Application. How can I store this in database with binary code so that other applications will fetch that binary code and get the image?
Thanks in Advance.
The stored files in Odoo filestores are regular files, which can be opened by the OS programs and can be read by any other Application as bytes of data like any other file in your computer. If you wanna get the value of the file stored in base64 format you could build the url for that file by having the id of the stored attachment and make a call to the Odoo instance and get the file content in base64.
The url format is like:
http://example.com/web/content/5
Where the id of the attachment is 5 at the end of the url

Google Drive - use WebViewLink vs thumbnailLink

I'm using the Google Drive API where I can gain access to 2 pieces of data that I need to display a jpg file oin my program. WebViewLink is the "large" size image while thumbnailLink is the "thumb" smaller size of the same image.
I'm having an issue with downloading the WebViewLink that I do not have with the thumbnailLink. Part of my code calls either exif_imagetype($filename) or getimagesize($filename) so I can retrieve the type, height & width etc for the $filename. This is successful for the thumbnailView but not the WebViewLink...
code snippet...
$WebViewLink = "https://drive.google.com/a/treering.com/file/d/blablabla";
$type = exif_imagetype($WebViewLink);
--- results in the error
"PHP Warning: exif_imagetype(): stream does not support seeking..."
where as...
$thumbnailLink = "https://lh6.googleusercontent.com/blablabla";
$type = exif_imagetype($thumbnailLink);
--- successful
where $type = 2 // a .jpg file
Not sure what I need to do to gain a usable WebViewLink... maybe use the "export" function to copy to a file on my server that is accessible, then use that exported file for the functions that fail above?
Thanks for any help.
John
I think you are using the wrong property to get the image of the file.
WebViewLink
A link for opening the file in a relevant Google editor or viewer in a browser.
thumbnailLink
A short-lived link to the file's thumbnail, if available. Typically lasts on the order of hours.
You can try using the iconLink():
A static, unauthenticated link to the file's icon.
Sample image of thumbnailLink:
Sample image of a iconLink:
It will still show relevant image about the file.
Hope it helps!

create a temp image file for upload

I have created a HTML5 image uploader using canvas.
I have the image data using
Canvas.toDataURL();
which is in the form
data:image/png;base64,<base64image string>
I sent the above data to php which will be used to upload the image to amazon server.
I normally pass the return value of
file_get_contents(path_to_file_to_upload);
to the amazon sdk and the work gets done.
Now how do i have the base64 image data converted into file_get_contents type data to upload the file.
I am not allowed to create a file in the server.Is there any way of creating a temp image and get the file_get_contents data from that temp file??
Pass the return value of base64_decode() instead of file_get_contents to the AWS SDK. file_get_contents loads a file into a string, base64_decode loads a base64 string and returns a string. Since you have a base64 string and not a file, you would call base64_decode.

How to restore an unknown type BLOB field from Firebird

I am trying to restore a BLOB field stored in a Firebird database, and the only information I have is that the content of the BLOB field is a document.
I've tried using IBManager to right-click on the cell and click "Save BLOB to file", but the saved file is unreadable (as if it was encrypted). I tried to open it with Microsoft Word, notepad, adobe etc, with no success. I also tried opening it with WinRAR (I thought that it might have been compressed before being stored to the database) but still nothing.
Is there a way to find out whether and how the BLOB file was compressed, and how to restore it?
Thanks in advance!
Update:
I have converted the firebird database to SQL and I use the following code to extract the Unencoded BLOB documents:
conn.Open();
dr = comm.ExecuteReader();
while (dr.Read())
{
byte[] document_byte = null;
if (dr[1] != System.DBNull.Value)
{
document_byte = (byte[])dr[1];
}
string subPath = "C:\\Documents\\" + dr[0] + "\\";
System.IO.Directory.CreateDirectory(subPath);
if (document_byte != null)
{
System.IO.File.WriteAllBytes(subPath + "Document", document_byte);
}
}
How can I adjust my code to decode the BLOB file from Base64 since I know is Base64 encoded?
Unless the field uses BLOB filter the data is stored into database as is, ie Firebird doesn't alter it in any way. Check the field's definition, if it does have SUB_TYPE 0 (or binary) then it is "ordinary" binary data, ie Firebird doesn't apply any filter to it. And even in case the field uses some filter, unless there is a bug in the filter code you should get the original data back when reading the content of the BLOB.
So it comes down to the program which stored the document into DB, it is quite possible that it compressed or encrypted the file, but there is no way Firebird can help you to figure out what algorithm was used... One option would be to save the content of the BLOB into file and then try the *nix file command, perhaps it is able to detect the file format used.
I would also check the DB for corruptions, just for case (Firebird's gfix command line tool).

How to return in-memory PIL image from WSGI application

I've read a lot of posts like this one that detail how to dynamically return an image using WSGI. However, all the examples I've seen are opening an image in binary format, reading it and then returning that data (this works for me fine).
I'm stuck trying to achieve the same thing using an in-memory PIL image object. I don't want to save the image to a file since I already have an image in-memory.
Given this:
fd = open( aPath2Png, 'rb')
base = Image.open(fd)
... lots more image processing on base happens ...
I've tried this:
data = base.tostring()
response_headers = [('Content-type', 'image/png'), ('Content-length', len(data))]
start_response(status, response_headers)
return [data]
WSGI will return this to the client fine. But I will receive an error for the image saying there was something wrong with the image returned.
What other ways are there?
See Image.save(). It can take a file object in which case you can write it to a StringIO instance. Thus something like:
output = StringIO.StringIO()
base.save(output, format='PNG')
return [output.getvalue()]
You will need to check what values you can use for format.