Reputation: 41
I need to convert my SurfaceView's content into a Bitmap. I tried to achieve this using the following code snippet inside the SurfaceView:
public final Bitmap getScreenCopy() {
Bitmap bitmap = Bitmap.createBitmap(
getWidth(),
getHeight(),
Bitmap.Config.ARGB_8888
);
Canvas temporaryCanvas = new Canvas(bitmap);
draw(temporaryCanvas); // Voodoo.
return bitmap;
}
The bitmap I receive from that drone seems to be transparent, does anyone know how to fix that?
Using a TextureView is not possible, since I use the Parrot SDK and the drone needs a SurfaceView to display the Frames.
Upvotes: 4
Views: 4719
Reputation: 14506
I've been trying to find a way to achieve this too and so far I haven't found a way to do it for devices with API level below 24. I tried messing with the drawing cache, but I get nothing but transparency. I didn't expect the drawingCache approach to work anyway due to the fact that the view itself draws nothing and everything goes onto the Surface
.
For API levels 24 and newer, you can use the PixelCopy APIs to copy all or a portion of the SurfaceView
into a bitmap.
Here's a snippet from my (Kotlin) code:
override fun getPlotBitmap(view: PlotSurfaceView) = Single.create<Bitmap> {
val plotBitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val listener = PixelCopy.OnPixelCopyFinishedListener { copyResult ->
when (copyResult) {
PixelCopy.SUCCESS -> {
it.onSuccess(plotBitmap)
}
else -> {
it.onError(RuntimeException("Pixel copy failed with result $copyResult"))
}
}
}
PixelCopy.request(view, plotBitmap, listener, view.handler)
}
Upvotes: 2