Flutter: convert bitmap (e.g. screenshot) from clipboard to base64 string

316 Views Asked by At

This is my current requirement:
A) The screenshot/bitmap in my clipboard must be pasted into the app (Platform: primarily Windows, but must run on all other platforms aswell: Android/iOS/Linux/MacOS/Web)

I found out that Flutter does not support anything else than plain text from the clipboard and a request for image/binary extraction is requested by the community.

That's a bummer, but then I remembered that Flutter supports ffi:

Flutter mobile can use the dart:ffi library to call native C APIs. FFI stands for foreign function interface. Other terms for similar functionality include native interface and language bindings.

That means I can write a custom or already existing C/C++ program to handle that use case and send it to flutter. However, it seems that C/C++ programs are not guaranteed to run everywhere (Android/iOS/Linux/Windows/MacOS/Web).

So, well, my conclusion is that if I want to fulfill that requirement, I basically need to write a C/C++ program for every platform.

That sounds troublesome, isn't there anything like a standardized method on all platforms to get dynamic data out of the clipboard? So that my Flutter application handles the logic?

1

There are 1 best solutions below

0
On

Windows workaround

required plugin: process_run: ^0.12.3+2 (view on pub.dev)

final res = await Shell().run('''
powershell -c "(Get-Clipboard -Format Image).Save('C:/temp/temporary.png')"
powershell -c "[convert]::ToBase64String((get-content 'C:/temp/temporary.png' -encoding Byte))"
''');
print(res[1].stdout.trim());

res[1].stdout contains the image in base64 format.

I really do not like this solution, but it works at least for windows.