gpt4 book ai didi

java - 如何使用 Arcore 截屏?

转载 作者:行者123 更新时间:2023-11-29 18:26:30 29 4
gpt4 key购买 nike

我正在尝试截取增强现实屏幕的屏幕截图并将其作为位图传递给另一个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com