采取截图截图

2023-09-07 01:50:17 作者:执笔写思念。

我工作的一个应用程序,它使用frontcamera来模拟使用的应用程序的人的一面镜子。我实现了一个头向上,显示这是在cameraview前面,显示一些东西。这在目前工作正常。现在,我想实现的方法采取截图每5秒。

I am working on an app, which uses the frontcamera to simulate a mirror for the person using the app. I have implemented a "Head-up-Display" which is in front of the cameraview and displays some stuff. This works fine at the moment. Now I'd like to implement a method to take a screenshot every 5s.

但如何采取截图?我想一个可能的解决方法,但这只是需要HUD的截图没有镜子,因为我需要传递一个视图(V1)这个方法:

But how to take a screenshot? I tried one possible solution, but this just takes a screenshot of the HuD without the mirror, because I have to pass a view (v1) to this method:

    // create bitmap screen capture
    Bitmap b1;

    View v1 = (RelativeLayout) findViewById(R.id.RelLayout);
    v1.setDrawingCacheEnabled(true);
    b1 = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    //mPreviewLayout.setDrawingCacheEnabled(true);
    //mPreviewLayout.buildDrawingCache();
    //Bitmap b1 = mPreviewLayout.getDrawingCache();

    String path = Environment.getExternalStorageDirectory().toString();
    FileOutputStream out = null;
    try {
        File file = new File(path, "brushGuide" + instructionCounter + ".jpg");
        out = new FileOutputStream(file);
        b1.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Throwable ignore) {
        }
    }

我的布局和重要code对于这个问题是相当少:

My layout and the important code for this question is quite less:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:id="@+id/RelLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal" >

<FrameLayout
    android:id="@+id/camPreview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</FrameLayout>

 <ImageView android:id="@+id/imageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="80dp"
          android:layout_marginLeft="65dp"
          ></ImageView>

</RelativeLayout>

的onCreate()

onCreate()

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 // do we have a camera?
 if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    Toast.makeText(this,
            "No camera feature on this device. App closing",
            Toast.LENGTH_LONG).show();
 }else{

    mCameraId = findFirstFrontFacingCamera();

    if (mCameraId >= 0) {
        // Kamera wurde gefunden
        mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
        mPreviewLayout.removeAllViews();
        startCameraInLayout(mPreviewLayout, mCameraId);
    }
  }
}

甚至可以只取设备显示的实际内容的正常截图没有生根的设备,如果是:如何

Is even possible to just take a normal screenshot of the actual content of the devices display without rooting the device, and if yes: How?

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

其实我只是做这个的一天,这是一个痛苦,以获得整个活动的截图,同时还确保从图像中删除的状态栏(因为它将出现在绘图缓存一个黑色矩形)。这种方法也可以让你用叠加一些颜色(即褪色。))返回的图像:

I actually just did this the other day and it was a pain to get a screenshot of the entire Activity, while also making sure to remove the StatusBar from the image (as it will appear as a black rectangle in the drawing cache). This method will also allow you to overlay the returned image with some color (ie. fading)):

public static Bitmap getActivitySnapshot(Activity activity, boolean fade){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    if(fade && snapshot != null){
        Canvas canvas = new Canvas(snapshot);
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#88121212"));
        canvas.drawRect(0, 0, snapshot.getWidth(), snapshot.getHeight(), paint);
    }

    view.setDrawingCacheEnabled(false);
    return snapshot;
}

如果你不想衰落的一部分,重要部位也只是:

If you don't want the fading part, the important parts would just be:

public static Bitmap getActivitySnapshot(Activity activity){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    view.setDrawingCacheEnabled(false);
    return snapshot;
}

您可能还希望赶上可能与大位图时抛出的OutOfMemoryError异常。

You may also want to catch the OutOfMemoryError that may be thrown when working with large Bitmaps.

 
精彩推荐
图片推荐