如何使用CameraView(SurfaceView)在Android中截取屏幕截图

时间:2015-06-09 10:40:57

标签: android android-layout camera screenshot

我正在尝试以编程方式截取一个活动的屏幕截图,其中包含相机运行和叠加效果。

我已经尝试了以下内容:

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

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

  2. 使用Open GL

    无法获得屏幕的叠加部分

  3. 我也尝试将这两张图片合并在一起,但也不是很好。

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

    我正在活动中使用Vuforia云镜头。

    请提前帮助我。

1 个答案:

答案 0 :(得分:0)

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;
}