1

I have an app that displays live video streaming from a camera device on the Surface view. As I am not an expert on Android, it took me weeks to display live streaming content on the Surface view. The format is raw H.264. I used Mediacodec to decode. What I need is to record that live streaming video and capture a screenshot of the same on a button click.

Code for taking a screenshot:

    private void takeScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {

        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or DOM
        e.printStackTrace();
    }
}

private void openScreenshot(File imageFile) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(imageFile);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);
}

Using the above code I am able to take a screenshot but the image color is black and also contains other buttons that I am using. All the other buttons are placed on top of the Surface view.

Is it possible to record and capture only the contents(excluding the buttons on top of the Surface view) that are displayed on the Surface view?

I am just thinking, please correct me if I am wrong. Instead of displaying it on SurfaceView, Is there any way to record and capture the decoded frames directly? Please find the below code:

This is how I display streaming content on SurfaceView;

    LinearLayout surface2 = (LinearLayout)findViewById(R.id.surface);
    SurfaceView sv = new SurfaceView(this);
    sv.getHolder().addCallback(this);
    surface2.addView(sv);
Sarath
  • 89
  • 2
  • 9
  • Does this answer your question? [how to create and save a screenshot from a surfaceview?](https://stackoverflow.com/questions/25091703/how-to-create-and-save-a-screenshot-from-a-surfaceview) – E.Abdel Jan 27 '20 at 15:45

0 Answers0