【问题标题】:I am unable to print this array.The application stops working post this line我无法打印此数组。应用程序在此行后停止工作
【发布时间】:2023-03-14 09:10:01
【问题描述】:

我必须将此floatbuffer 打印为array,并且文档中有一个功能,但该功能不起作用。我不明白我做错了什么?

我尝试使用floatBuffer.toString(),但它确实打印了文档(ARCore)所描述的array。因此结果不正确。

 Camera camera = frame.getCamera();
 CameraIntrinsics cameraIntrinsics=camera.getImageIntrinsics();
 float[] focal=cameraIntrinsics.getFocalLength();
 Log.e("Focals",Arrays.toString(focal));
 int [] getDiminsions=cameraIntrinsics.getImageDimensions();
 Log.e("Dimensions ", Arrays.toString(getDiminsions));
 backgroundRenderer.draw(frame);
 PointCloud pointCloud=frame.acquirePointCloud();
 FloatBuffer floatBuffer=pointCloud.getPoints();
 FloatBuffer readonly=floatBuffer.asReadOnlyBuffer();
 //final boolean res=readonly.hasArray();
 final float[] points=floatBuffer.array();
        //what should I do

根据文档 (ARCore),floatBuffer 中的每个点都有 4 个值:x、y、z 坐标和置信度值。

【问题讨论】:

  • 能否提供异常堆栈跟踪(来自控制台/logcat)?
  • 没有异常跟踪。
  • 2019-03-28 17:22:25.605 24341-24360/com.google.ar.core.examples.java.augmentedimage E/AugmentedImageActivity:OpenGL 线程 java.lang.UnsupportedOperationException 上的异常java.nio.FloatBuffer.array(FloatBuffer.java:601)

标签: android arrays arcore floatbuffer


【解决方案1】:

根据 FloatBuffer 的实现,如果缓冲区没有数组支持,array() 方法可能不可用。如果您要做的只是遍历值,则可能不需要数组。

FloatBuffer floatBuffer = pointCloud.getPoints();
// Point cloud data is 4 floats per feature, {x,y,z,confidence}
for (int i = 0; i < floatBuffer.limit() / 4; i++) {
    // feature point
    float x = floatBuffer.get(i * 4);
    float y = floatBuffer.get(i * 4 + 1);
    float z = floatBuffer.get(i * 4 + 2);
    float confidence = floatBuffer.get(i * 4 + 3);

    // Do something with the the point cloud feature....
}

但如果确实需要使用数组,则需要调用hasArray(),如果不需要,分配数组并复制数据。

FloatBuffer floatBuffer = pointCloud.getPoints().asReadOnlyBuffer();
float[] points;
if (floatBuffer.hasArray()) {
  // Access the array backing the FloatBuffer
  points = floatBuffer.array();
} else {
 // allocate array and copy.
 points = new float[floatBuffer.limit()];
 floatBuffer.get(points);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多