【问题标题】:How can I take Screenshots using Arcore?如何使用 Arcore 截屏?
【发布时间】:2019-11-01 20:28:14
【问题描述】:

我正在尝试截取我的增强现实屏幕截图并将其作为位图传递给另一个活动。

这是我用来截屏的代码:

截屏功能

public static void tmpScreenshot(Bitmap bmp, Context context){
        try {
            //Write file
            String filename = "bitmap.png";
            FileOutputStream stream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

            //Cleanup
            stream.close();
            bmp.recycle();

            //Pop intent
            Intent in1 = new Intent(context, CostActivity.class);
            in1.putExtra("image", filename);
            context.startActivity(in1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

接收截图功能

private void loadTmpBitmap() {
        Bitmap bmp = null;
        String filename = getIntent().getStringExtra("image");
        try {
            FileInputStream is = this.openFileInput(filename);
            bmp = BitmapFactory.decodeStream(is);
            ImageView imageView = findViewById(R.id.test);
            imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, 120, 120, false));
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

即使截取了屏幕截图,当它被传递给另一个活动时它还是黑色的。 另外,屏幕截图只有在我按下后退按钮后才会出现

任何人都可以帮助我使用 ARCore 截取屏幕截图的代码吗?还是我做错了什么?

【问题讨论】:

标签: java android screenshot arcore sceneform


【解决方案1】:

无法使用您的方法截取 SurfaceView 的屏幕截图。如果你这样做了,那么屏幕截图将是黑色的,因为它只适用于常规视图。

你需要使用的是pixelcopy

    private void takePhoto() {
    ArSceneView view = arFragment.getArSceneView();

    // Create a bitmap the size of the scene view.
    final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
            Bitmap.Config.ARGB_8888);

    // Create a handler thread to offload the processing of the image.
    final HandlerThread handlerThread = new HandlerThread("PixelCopier");
    handlerThread.start();
    // Make the request to copy.
    PixelCopy.request(view, bitmap, (copyResult) -> {
        if (copyResult == PixelCopy.SUCCESS) {
            try {
                saveBitmapToDisk(bitmap);
            } catch (IOException e) {
                Toast toast = Toast.makeText(VisualizerActivity.this, e.toString(),
                        Toast.LENGTH_LONG);
                toast.show();
                return;
            }
            SnackbarUtility.showSnackbarTypeLong(settingsButton, "Screenshot saved in /Pictures/Screenshots");




        } else {

            SnackbarUtility.showSnackbarTypeLong(settingsButton, "Failed to take screenshot");

        }
        handlerThread.quitSafely();
    }, new Handler(handlerThread.getLooper()));
}


public void saveBitmapToDisk(Bitmap bitmap) throws IOException {

  //  String path = Environment.getExternalStorageDirectory().toString() +  "/Pictures/Screenshots/";

    if (videoDirectory == null) {
        videoDirectory =
                new File(
                        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                + "/Screenshots");
    }

    Calendar c = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
    String formattedDate = df.format(c.getTime());

    File mediaFile = new File(videoDirectory, "FieldVisualizer"+formattedDate+".jpeg");

    FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();
}

【讨论】:

  • 当我使用 PixelCopy 时,我有一个没有场景的屏幕截图,因为屏幕截图上的场景看起来是透明的
猜你喜欢
  • 2018-08-07
  • 2013-08-11
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多