I need just the camera resolution of the device in pixels. I have tried this, but it doesnt show nothing on my app. I think I´m missing something
@Require
Try the next method. You need to pass an initialized instance of the CameraManager, and then camera lens identifier for which you wish to get the resolution from.
@NonNull
public Size getResolution(@NonNull final CameraManager cameraManager, @NonNull final String cameraId) throws CameraAccessException
{
final CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
final StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null)
{
throw new IllegalStateException("Failed to get configuration map.");
}
final Size[] choices = map.getOutputSizes(ImageFormat.JPEG);
Arrays.sort(choices, Collections.reverseOrder(new Comparator<Size>()
{
@Override
public int compare(@NonNull final Size lhs, @NonNull final Size rhs)
{
// Cast to ensure the multiplications won't overflow
return Long.signum((lhs.getWidth() * (long)lhs.getHeight()) - (rhs.getWidth() * (long)rhs.getHeight()));
}
}));
return choices[0];
}
The method will return the resolution size. If you need to display it you have many options. For example if you need to display in a TextView the Width and Height in the format such as for example 1280x720 then you can do as next:
final Size size = getResolution(cameraManager, stringId);
yourTextView.setText(size.toString());
If instead, you need to show it in megapixels then you can do as next:
final Size size = getResolution(cameraManager, stringId);
final float megapixels = (((size.getWidth() * size.getHeight()) / 1000.0f) / 1000.0f);
final String caption = String.format(Locale.getDefault(), "%.1f", megapixels);
yourTextView.setText(caption);