Search code examples
androidbitmapcameraoverlaytextureview

Bitmap of a Screen


I am working with Camera2 API. I have the camera preview in a texture view. And an Image that is placed over the texture view. The imageView and the textureView are placed in a RelativeLayout.

Getting the rootView and converting it into Bitmap gets me only a black-screen in the texture view as follows inside a Relative Layout:

View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();

The method getBitmap() returns the bitmap of the texture view successfully but id doesn't include the imageView which is overlaid on it.

How can I get the Bitmap of the textureView with the image overlaid on it?

Already referred Links:

Android: Want to put an silhouette overlay on a Camera preview

How can i place dynamic image view over texture view?

My Layout Structure is as follows:

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/SelectedImage"
        android:layout_width="140dp"
        android:layout_height="140dp" />

</FrameLayout>

Solution

  • Issue is solved when bitmaps are seperately generated, one for texture view using .getBitmap() and one for the imageView.

    The imageView is then overlayed by getting the parameters of its location on screen.

    public Bitmap takeScreenshot() {
        TextureView cameraTextureView=(TextureView)findViewById(R.id.CameraTextureView);
        Bitmap bitmapTextureView=cameraTextureView.getBitmap();
        ImageView selectedImageView=(ImageView)findViewById(R.id.SelectedImageView);
        Bitmap bitmapImageView;
        BitmapDrawable ImageViewDrawable=(BitmapDrawable)selectedImageView.getDrawable();
        if(ImageView.getDrawable()!=null){
            bitmapImageView=ImageViewDrawable.getBitmap();
            Log.e(TAG, "takeScreenshot: "+bitmapImageView.getHeight()+" ,"+bitmapImageView.getWidth());
            bitmapImageView=Bitmap.createScaledBitmap(bitmapImageView,selectedImageView.getWidth(),selectedImageView.getHeight(),false);
            Bitmap overlay=Bitmap.createBitmap(bitmapTextureView.getWidth(),bitmapTextureView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas=new Canvas(overlay);
            canvas.drawBitmap(bitmapTextureView,new Matrix(),null);
            canvas.drawBitmap(bitmapImageView,selectedImageView.getLeft(),selectedImageView.getTop(),null);
            screenshotBitmap=overlay;
            return overlay;
        }
        return bitmapTextureView;
    }