2

I have to print this floatbuffer as array and there is a function for that in the documentation but the function is not working. I cant understand what am I doing wrong?

I have tried using the floatBuffer.toString() but it does print the array that the documentation(ARCore) has described.Thus not proper results.

 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

As per the documentation(ARCore) every point in the floatBuffer has 4 values:x,y,z coordinates and a confidence value.

Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
PARUL JINDAL
  • 49
  • 1
  • 6
  • May you provide exception stack trace (from console/logcat)? – Boken Mar 28 '19 at 11:05
  • There is no exception trace. – PARUL JINDAL Mar 28 '19 at 11:53
  • 2019-03-28 17:22:25.605 24341-24360/com.google.ar.core.examples.java.augmentedimage E/AugmentedImageActivity: Exception on the OpenGL thread java.lang.UnsupportedOperationException at java.nio.FloatBuffer.array(FloatBuffer.java:601) – PARUL JINDAL Mar 28 '19 at 11:55

1 Answers1

3

Depending on the implementation of FloatBuffer, the array() method may not be available if the buffer is not backed by an array. You may not need the array if all you are going to do is iterate through the values.

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....
}

But if you do need to use an array, you'll need to call hasArray() and if it does not, allocate an array and copy the data.

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);
}
Clayton Wilkinson
  • 4,524
  • 1
  • 16
  • 25