ARCore通过以下API调用提供了场景的HDR Cube映射:

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


我想将这些lightmaps保存到内部或外部存储器。我该如何实现?

最佳答案

我找到了解决方案!

从上方获取lightmaps后,添加以下代码。这会将android.media.Image类型转换为位图,然后将其保存到内存。

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函数[Source]。这会将位图另存为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();
            }
        }
    }
}

09-30 23:26