java - 如何使用 Arcore 截屏?

标签 java android screenshot arcore sceneform

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

这是我用来截取屏幕截图的代码:

截屏功能

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

}

即使截取了屏幕截图,当它传递到另一个 Activity 时它还是黑色的。 另外,屏幕截图是在我按下后退按钮后才出现的

谁能帮我编写使用 ARCore 进行屏幕截图的代码吗?或者我做错了什么?

最佳答案

使用您的方法无法截取 SurfaceView 的屏幕截图。如果这样做,屏幕截图将是黑色的,因为它仅适用于常规 View 。

您需要使用的是 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();
}

关于java - 如何使用 Arcore 截屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58665513/

相关文章:

java - JDBC 代码是否与单一数据库类型相关?

android - 如何在 android 中使用 .pls 文件进行流式传输?

graphics - 如何在 Haskell 中将屏幕图像作为程序的输入

java - 在 Ubuntu 中使用 Java 运行二进制文件

java - java中年轻代和终身代内存的统计

Java生产者-消费者: producer does not "notify()" the consumer

java - 如何使用着色器为使用 GL_LINES 和 OpenGL ES 2.0 绘制的线条着色

android - 如何在矢量可绘制对象中设置 colors.xml 中的颜色代码?

python - 从屏幕上的矩形获取平均主导颜色(live/python)

linux - 如何以编程方式在 Linux 中截取应用程序的屏幕截图?