0

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到内部或外部存储器中。我怎样才能做到这一点?

4

1 回答 1

0

我找到了解决方案!

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();
            }
        }
    }
}
于 2020-04-11T01:26:01.937 回答