Get angle of view of android camera device
Asked Answered
D

4

10

I want to know the angle of view from the camera, just like in this question but using android.hardware.camera2. How can I reproduce the next code using the new camera2 library.

Camera.Parameters p = camera.getParameters();
double thetaV = Math.toRadians(p.getVerticalViewAngle());
double thetaH = Math.toRadians(p.getHorizontalViewAngle());

Is there a way to do the same thing?

Domitian answered 1/7, 2015 at 22:33 Comment(1)
How to do it using camera2 API: #67376281Jospeh
A
9

I search the google a person show possibility which he calculate FOV by Camera2 api

https://photo.stackexchange.com/questions/54054/calculating-the-field-of-view-for-a-nexus-5

and found the equation

http://www.bobatkins.com/photography/technical/field_of_view.html

FOV (rectilinear) = 2 * arctan (frame size/(focal length * 2))

thus, we need to know frame size and focal length

the frame size is size of camera you can find the code on below link

https://stackoverflow.com/a/30403558

also, focal length you can find below link

Manual focus in camera2, android

and i combine the code like this

A function calculateFOV() calculate FOV angle

https://github.com/pchan1401-ICIL/Camera2FOV

Adnah answered 20/6, 2016 at 7:33 Comment(1)
Very nice write-up! I am currently blown-away that Camera 2 API makes it harder on the developer. I thought that successive API sets are meant to increase functionality.Roquefort
H
4

You could do it mathematically.

Diagram

You have:

  • L, the width of an object
  • d, the distance to the object

You want to calculate the angle a (alpha), the field of view.

Doing some trig:

tan(a/2) = (L/2)/d
tan(a/2) = L/2d
a/2 = atan(L/2d)
a = 2*atan(L/2d)

You can do that to calculate the horizontal field of view. Good luck!

Herniotomy answered 1/7, 2015 at 23:31 Comment(2)
I knew that solution, but it's not what I want because the users would need to do calibration and there are already to many steps before reaching that point and I don't want to overwhelm the users to do a more steps to be able to know the d and LDomitian
Oh yeah... Sorry I don't know a better solution :PHerniotomy
E
4

As far as my research has gone the answer is no. With the camera2 API there is no call which can give you the vertical and horizontal viewing angle.

However, you don't need to use the camera2 API to get those values. You can just use the original camera API to get the vertical and horizontal view angle and then use the camera2 API for the rest of the app.

As far as I know the actual image capture firmware hasn't changed between the camera and camera2 APIs.

Erine answered 26/7, 2015 at 18:8 Comment(1)
very bad answer, it's possible to calculate, there is sensor size, focal length and other data available. You can just use the original camera == Just use deprecated API - so smart!...Jospeh
J
4

Using camera2 API we can do it like this:

val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val cameraCharacteristics = cameraManager.getCameraCharacteristics("0") // hardcoded first back camera id

val focalLength = cameraCharacteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS)?.firstOrNull() ?: return
val sensorSize = cameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE) ?: return
val horizontalAngle = (2f * atan((sensorSize.width / (focalLength * 2f)).toDouble())) * 180.0 / Math.PI
val verticalAngle = (2f * atan((sensorSize.height / (focalLength * 2f)).toDouble())) * 180.0 / Math.PI

// using camera2 API we got the same data as with legacy Camera API (with it was easier):
// val parameters = camera.getParameters()
// val horizontalAngle = parameters.getHorizontalViewAngle()
// val verticalAngle = parameters.getVerticalViewAngle()

But usually such values are for 4:3 aspect preview ratio, if you need it for 16:9 aspect ratio or other then check this Without additional calculation Camera or camera2 APIs return FOV angle values for 4:3 aspect ratio by default?

Jospeh answered 3/5, 2021 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.