【问题标题】:How to take a screenshot in Android with CameraView (SurfaceView)如何在 Android 中使用 CameraView (SurfaceView) 截取屏幕截图
【发布时间】:2015-06-09 10:40:57
【问题描述】:

我正在尝试以编程方式截取摄像头正在运行并在其上覆盖的活动的屏幕截图。

我已经尝试了以下方法:

  1. 捕获位图并将其绘制在画布上

    屏幕的相机部分显示为黑色

  2. 使用 Open GL

    无法获取屏幕的覆盖部分

我也尝试将两张图片合并,结果也不好。

注意:我已经遇到过很多类似的问题,但找不到相同的解决方案。

我在活动中使用 Vuforia 云记录相机。

请帮我解决这个问题,在此先感谢。

【问题讨论】:

标签: android android-layout camera screenshot


【解决方案1】:

mSnapshotLayout 正在获取您想要的快照的主布局 id,像这样

mSnapshotLayout = (LinearLayout) findViewById(R.id.snapshotLayout);

在你的按钮点击中使用这个

View v1 = mSnapshotLayout.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();

storeImage(bm);

这是存储该图像的方法

@SuppressLint("SimpleDateFormat")
private boolean storeImage(Bitmap imageData) {
    // get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory()
            + "/Snapshot/";
    File sdIconStorageDir = new File(iconsStoragePath);

    // create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String currentDateandTime = sdf.format(new Date());

    String filename = "Deal" + currentDateandTime + ".png";

    try {

        File file = new File(sdIconStorageDir.toString() + File.separator
                + filename);

        FileOutputStream fileOutputStream = new FileOutputStream(file);

        BufferedOutputStream bos = new BufferedOutputStream(
                fileOutputStream);

        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();

        MediaScannerConnection.scanFile(this,
                new String[] { file.getPath() },
                new String[] { "image/jpeg" }, null);



    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }
    return true;
}

【讨论】:

  • 谢谢,但它不适用于 WebView。保存的图像是空白的。
  • 在 WbView 上??你想要 WebView 图像吗?
  • 是的,我在 cameraview 上的叠加层使用的是 webview。如果我能得到 webview 的图像,那么我可以将它与我保存的相机图像结合起来。
  • 你能告诉我你想要谁的快照吗?
  • 我正在使用java代码动态添加包括webview在内的大部分视图,因此活动的Xml文件中没有太多内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2017-10-09
  • 2014-06-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
相关资源
最近更新 更多