保存ARCore估计的环境HDR立方体贴图至内存

huangapple 未分类评论48阅读模式
英文:

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 &lt; 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 = &quot;&quot;;
    File parent = new File(TextUtils.isEmpty(path) ? Environment.getExternalStorageDirectory() + &quot;/&quot; + Environment
            .DIRECTORY_DCIM : path);
    if (!parent.exists()) {
        parent.mkdirs();
    }
    File f = new File(parent.getAbsolutePath() + File.separator + &quot;arcore_bitmap&quot; + new Date().getTime() + &quot;.jpg&quot;);
    if (f.exists()) {
        f.delete();
    }
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.PNG, 90, out);
        Log.i(&quot;savingbitmap&quot;,&quot;savingbitmap&quot; + parent.getAbsolutePath());
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

huangapple
  • 本文由 发表于 2020年4月9日 00:31:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61105497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定