Is there any method to take screenshot for FrameLayout including cameraX Preview View and not to get a black screen?

267 views
Skip to first unread message

Leo Hsu

unread,
Aug 21, 2022, 12:00:40 PM8/21/22
to Android CameraX Discussion Group
Dear all, 

I bumped into a problem that is when I tried to get the bitmap of my FrameLayout which contains a PreviewView, I can only get the black screen for the camera region and some the overlay I put on PreviewView.

299684982_828773698485973_7166660288485665320_n.jpg

I have checked the cameraX and PreviewView can work when I only call the preview.bitmap and save the screenshot image. It is as follow:
299320813_830127754653227_1497904940251316170_n.jpg
Is there any way to take a screenshot of preview and my overlay simultaneously?
Thank you in advance!

Sincerely, Leo


Jag Saund

unread,
Aug 21, 2022, 5:08:26 PM8/21/22
to Android CameraX Discussion Group, leodrea...@gmail.com
Hi Leo,
When the view hierarchy is captured to a bitmap, it won't be able to capture the contents of the preview view because the preview view is backed by a texture view (or surface view depending on your configuration).
What you'll need to do is capture the two independently and merge them.
1. Create a composite bitmap which will be your final bitmap output
2. Capture the view hierarchy to a bitmap as you are right now
3. Capture the preview view to bitmap (`preview.bitmap`)
4. Merge the two in your composite bitmap:
```
val compositeBitmap = Bitmap.createBitmap(...)
val viewBitmap = ..
val previewBitmap = ..
val compositeCanvas = Canvas(compositeBitmap)
compositeCanvas.drawBitmap(previewBitmap, ...)
compositeCanvas.drawBitmap(viewBitmap, ...)
```
Be sure to recycle the bitmaps afterwards
Hope that helps!

Leo Hsu

unread,
Aug 22, 2022, 12:12:35 PM8/22/22
to Android CameraX Discussion Group, jags...@google.com, Leo Hsu
Hello sir, 
Thank you for your reply first! I tried the method you suggested, and it did work!!!
Really appreciate!
```
val frameBitmap = constraintFrame.drawToBitmap()
val viewFinderBitmap = viewFinder.bitmap!!
val overlayBitmap = combine2bitmap(viewFinderBitmap, frameBitmap)
saveMediaLocally(overlayBitmap)  
```
```
private fun combine2bitmap(viewFinderBit: Bitmap, frameBit: Bitmap): Bitmap{

var bitmapOverlay: Bitmap = Bitmap.createBitmap(frameBit.width, frameBit.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmapOverlay)
canvas.drawBitmap(viewFinderBit, Matrix(), null)//Draw the viewFinder
canvas.drawBitmap(frameBit, 0f, 0f, null)//Draw the frame
viewFinderBit.recycle()
frameBit.recycle()

return bitmapOverlay
}
```
I used the 
canvas.drawBitmap(bitmap1: Bitmap, Matrix(), null)
canvas.drawBitmap(bitmap2: Bitmap, 0, 0, null)
May I ask why the second line of canvas.drawBitmap(bitmap2: Bitmap, 0, 0, null) didn't overlay the whole original picture, but only overlay for the center of the preview?

Really appreciate for your helpful suggestion,
Sincerely, Leo


jags...@google.com 在 2022年8月22日 星期一清晨5:08:26 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages