如何在Arcore(java)中将16位深度图像保存到文件

如何在Arcore(java)中将16位深度图像保存到文件,java,android,arcore,bytebuffer,Java,Android,Arcore,Bytebuffer,我想将深度信息从arcore保存到存储器中。 下面是《开发人员指南》中的示例 public int getMillimetersDepth(Image depthImage, int x, int y) { // The depth image has a single plane, which stores depth for each // pixel as 16-bit unsigned integers. Image.Plane plane = depthImage.ge

我想将深度信息从arcore保存到存储器中。 下面是《开发人员指南》中的示例

  public int getMillimetersDepth(Image depthImage, int x, int y) {
  // The depth image has a single plane, which stores depth for each
  // pixel as 16-bit unsigned integers.
  Image.Plane plane = depthImage.getPlanes()[0];
  int byteIndex = x * plane.getPixelStride() + y * plane.getRowStride();
  ByteBuffer buffer = plane.getBuffer().order(ByteOrder.nativeOrder());
  short depthSample = buffer.getShort(byteIndex);
  return depthSample;
}
所以我想将这个bytebuffer保存到一个本地文件中,但我的输出txt文件不可读。我怎样才能解决这个问题? 这是我的

Image depthImage = frame.acquireDepthImage();
Image.Plane plane = depthImage.getPlanes()[0];
int format = depthImage.getFormat();
ByteBuffer buffer = plane.getBuffer().order(ByteOrder.nativeOrder());
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
File mypath=new File(super.getExternalFilesDir("depDir"),Long.toString(lastPointCloudTimestamp)+".txt");
FileChannel fc = new FileOutputStream(mypath).getChannel();
fc.write(buffer);
fc.close();
depthImage.close();
我试着用计算机解码它们

String s = new String(data, "UTF-8");
System.out.println(StandardCharsets.UTF-8.decode(buffer).toString());
但是输出仍然像这样奇怪

    .03579:<=>@ABCDFGHJKMNOPRQSUWY]_b
.03579:@ABCDFGHJKMNOPRQSUWY]\u b

为了获得ARCore会话提供的深度数据,您需要将字节写入本地文件。缓冲区对象是一个容器,它对特定基元类型的有限元素序列进行计数(这里字节代表字节缓冲区)。因此,需要在文件中写入的是与先前存储在缓冲区中的信息相对应的
data
变量(根据
buffer.get(data)

它对我来说工作得很好,我设法在python代码中绘制了提供的深度图(但以下代码背后的思想可以很容易地适应java代码):

有关更多详细信息,请在此处阅读有关depthmap(DEPTH16)格式的信息: 您还必须注意,深度贴图分辨率设置为
160x120
像素,并根据横向格式定向

如果出现
IOException
错误,请确保用
try/catch
代码块包围代码

depthData = np.fromfile('depthdata.txt', dtype = np.uint16) 
H = 120 
W = 160
def extractDepth(x):
    depthConfidence = (x >> 13) & 0x7 
    if (depthConfidence > 6): return 0 
    return x & 0x1FFF 
depthMap = np.array([extractDepth(x) for x in depthData]).reshape(H,W)
depthMap = cv.rotate(depthMap, cv.ROTATE_90_CLOCKWISE)