Sometimes we need to exchange images from Android world to Qt world and vice-versa, therefore in this article we're going to see how to convert a QImage to an Android Bitmap and how to convert an Android bitmap and an Android Drawable into a QImage.
To get access to Android Bitmap data & info we're going to use NDK's support, so we need to make sure jnigraphics library is in libraries dependency in our .pro file:
Now let's see what the convert functions looks like:
This function doesn't have any JNI magic, and it's quite simple. We try to find the best QImage::Format (To be honest I'm not very sure about ANDROID_BITMAP_FORMAT_RGBA_4444), then we copy the image data from Android bitmap to our QImage.
Being able to convert any Android Drawable to a QImage is quite useful, therefore, let's check how to "convert" (actually draw) an Android drawable into a QImage.
First function (createBitmap) is used to create an RGBA_32 bitmap, we're going to use this function in the next snippet too.
Second function (toImage) draws the drawable into a bitmap canvas, then it converts the bitmap using previous toImage function.
Finally, let's see how to convert a QImage to an Android Bitmap
This function is quite simple, we're using createBitmap from a previous snippet to create an Android Bitmap object, then we copy the QImage content to Android Bitmap.
1 Comment
8 - Aug - 2020
Aleksey
How to convert Java
Drawable
to QImage or pixmap?