英文:
Saving ARCore's Estimated Environmental HDR Cube Map to memory
问题
ARCore通过以下API调用提供场景的HDR立方体贴图:
// 获取当前帧。
Frame frame = session.update();
// 获取当前帧的光照估计。
LightEstimate lightEstimate = frame.getLightEstimate();
// 以线性色彩空间中的立方体贴图形式获取HDR环境光照。
Image[] lightmaps = lightEstimate.getEnvironmentalHdrCubeMap();
我想将这些lightmaps
保存到内部或外部存储器。我该如何实现这一目标?
英文:
ARCore provides the HDR Cube map of the scene through the following API calls:
// Get the current frame.
Frame frame = session.update();
// Get the light estimate for the current frame.
LightEstimate lightEstimate = frame.getLightEstimate();
// Get HDR environmental lighting as a cubemap in linear color space.
Image[] lightmaps = lightEstimate.getEnvironmentalHdrCubeMap();
I want to save these lightmaps
to internal or external memory. How can I achieve this?
答案1
得分: 0
我找到了解决方案!
在上面获取`lightmaps`之后,添加以下代码。这将把`android.media.Image`类型转换为位图,然后保存到内存中。
```java
for (int i = 0; i < lightmaps.length; i++) {
Image lightmapimage = lightmaps[i];
int width = lightmapimage.getWidth();
int height = lightmapimage.getHeight();
Image.Plane[] planes = lightmapimage.getPlanes();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
ByteBuffer buffer = planes[0].getBuffer();
Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
lightmapimage.close();
saveBitmap(bitmap);
}
下面是saveBitmap
函数[来源]。这将位图保存为png格式,保存路径为/storage/emulate/0/DCIM
。如果需要,请进行更改。
public static void saveBitmap(Bitmap bm) {
String path = "";
File parent = new File(TextUtils.isEmpty(path) ? Environment.getExternalStorageDirectory() + "/" + Environment
.DIRECTORY_DCIM : path);
if (!parent.exists()) {
parent.mkdirs();
}
File f = new File(parent.getAbsolutePath() + File.separator + "arcore_bitmap" + new Date().getTime() + ".jpg");
if (f.exists()) {
f.delete();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
Log.i("savingbitmap", "savingbitmap" + parent.getAbsolutePath());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
英文:
I found the solution!
Add the below code after getting the lightmaps
from above. This converts the android.media.Image
type to a bitmap and then saves it to memory.
for (int i = 0; i < lightmaps.length; i++)
{
Image lightmapimage = lightmaps[i];
int width = lightmapimage.getWidth();
int height = lightmapimage.getHeight();
Image.Plane[] planes = lightmapimage.getPlanes();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
ByteBuffer buffer = planes[0].getBuffer();
Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
lightmapimage.close();
saveBitmap(bitmap);
}
Below is the saveBitmap function [Source]. This saves the bitmap as a png to /storage/emulate/0/DCIM
. Change this if you want to.
public static void saveBitmap(Bitmap bm)
{
String path = "";
File parent = new File(TextUtils.isEmpty(path) ? Environment.getExternalStorageDirectory() + "/" + Environment
.DIRECTORY_DCIM : path);
if (!parent.exists()) {
parent.mkdirs();
}
File f = new File(parent.getAbsolutePath() + File.separator + "arcore_bitmap" + new Date().getTime() + ".jpg");
if (f.exists()) {
f.delete();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
Log.i("savingbitmap","savingbitmap" + parent.getAbsolutePath());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论