Search code examples
androidarraysarcorefloatbuffer

I am unable to print this array.The application stops working post this line


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.


Solution

  • 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);
    }