-2

I am suppose to get focal length of Camera. My target API version is 21+. I tried the following with help of the documentation:

import android.hardware.Camera;

private float getFocalLengthHere() {

    float focalLength = Camera.Parameters.getFocalLength ();  
    return  focalLength;
}

I encountered the following error:

Non-static method 'getFocalLength()' cannot be referenced from a static context

Here is attached image

In documentation, I didn't found anything to call Focal Length with camera2.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
YoGi
  • 3
  • 5
  • 1
    Does this answer your question? ["Non-static method cannot be referenced from a static context" error](https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – Ivo Dec 01 '21 at 09:29
  • @IvoBeckers Not really, I know why I am getting this error and here my query is also that, That method call is available below API level 21, but what is way to extract that call for the current version i.e API level 21+ – YoGi Dec 01 '21 at 10:25
  • Should be here https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#LENS_INFO_AVAILABLE_FOCAL_LENGTHS – Ivo Dec 01 '21 at 10:44

1 Answers1

0

You need to open the camera device, and get a Parameter object first, then call getFocalLength on it:

   Camera myCamera = Camera.open(<camera id>);
   float focalLength = myCamera.getParameters().getFocalLength();
   myCamera.close();

But that's the deprecated camera API. With the newer Camera2 API, you can do:

   CameraCharacteristics = CameraManager.getCameraCharacteristics(<camera id>);
   float focalLength = CameraCharacteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)[0];

which is faster since you don't have to turn on the camera to get the info. Note that a camera that supports physical zooming may have more than one focal length listed; the first one is the default.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47