1

I'm trying to create an Android app that will allow the user to save a texture view, inside a frame layout, exactly as the user sees it, but the resultant "saved screenshot" grabs the complete camera view along with unwanted areas.

My layout xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frlayout"
    android:layout_weight="0.82"
    android:layout_width="match_parent"
        android:layout_height="0dp">
            <TextureView
            android:id="@+id/cameraView"
                android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             />
    </FrameLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.07"
    android:orientation="horizontal"
    android:gravity="center"
    android:id="@+id/seekbarView">
<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.07"
    android:progress="1000"
    android:max="2000"
    />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.11"
android:background="#004D79"
android:orientation="horizontal"
    android:id="@+id/controlView">
    <Button
        android:text="Save File"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/sfbutton" />
</LinearLayout>
</LinearLayout>

As you can see, the texture view takes up 82% of the screen (vertically), but it looks like everything that is obscured by other views/elements is also included.

The part of the code that grabs the contents of the textureview is as follows:

   Bitmap bitmap;
    ViewGroup v1 = findViewById(R.id.frlayout);

    bitmap = textureView.getBitmap();
    bitmap = Bitmap.createBitmap( bitmap, 0, v1.getTop(), bitmap.getWidth(), v1.getHeight(), textureView.getTransform( null ), true );

I've tried to get it to take only a picture starting at the top of the framelayout and the only for the height of the layout, but with no luck so far. My android app is a landscape only programme, so the getTransform(...) call simply applies a 90 degree transformation to go from portrait to landscape.

I've included some pictures; the first is an actual screengrab of the whole screen. The second is the output from the code above. They were taken seconds apart, so there is some slight camera jiggle to within a few pixels, but as far as I can see the sizes and aspect ratios of the things in the picture are fine - its just that I'm getting more than I want comparison of real screengrab with programmatic version

EDIT: I've done some more analysis and its more complicated that I thought.

On the screenshot, the FrameLayout area goes from (0, 48) to (1196, 600). The programmatically saved area goes from (0,0) to (1196, 897). The width is therefore correct. The problem is that in the programmed screenshot, the vertical "excess" above the framelayout area is 178, while below, it is about 190, so pretty close. It looks like the programmatical screenshot is showing a lot more than is underneath the other views in the "normal" screenshot. Its hard to explain, but the maximum "under" the programmatic screenshot be 48 pixels high at most (the bar containing the phone charging status etc.), while at the bottom, the obscured height should be 120 pixels, the height of the panels with the button and so on.

  • Unfortunately, there is no easy way to reproduce a complex layout which includes camera live preview, as a screenshot bitmap. Try [this](https://stackoverflow.com/a/33242595/192373) code sample. – Alex Cohn Jul 20 '19 at 12:14

0 Answers0