gpt4 book ai didi

android - 如何在 android studio (Camera2) 中获得相机分辨率

转载 作者:行者123 更新时间:2023-12-01 18:44:49 46 4
gpt4 key购买 nike

我只需要设备的相机分辨率(以像素为单位)。我已经尝试过这个,但它在我的应用程序上没有显示任何内容。我想我错过了一些东西

   @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
textView = (TextView) findViewById(R.id.textView);

}
@NonNull
public Size getResolution(@NonNull final CameraManager cameraManager, @NonNull final String cameraId) throws CameraAccessException
{
CameraManager cameraManager1 = (CameraManager) getSystemService(CAMERA_SERVICE);

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);
final Size size = getResolution(cameraManager, cameraId);
final float megapixels = (((size.getWidth() * size.getHeight()) / 1000.0f) / 1000.0f);
final String caption = String.format(Locale.getDefault(), "%.1f", megapixels);
textView.setText(caption);

return size;
}

}

最佳答案

尝试下一个方法。您需要传递 CameraManager 的初始化实例,然后是您想要获取其分辨率的相机镜头标识符。

@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];
}

该方法将返回分辨率大小。如果您需要显示它,您有很多选择。例如,如果您需要在 TextView 中以 1280x720 等格式显示宽度和高度,那么您可以执行以下操作:

final Size size = getResolution(cameraManager, stringId);
yourTextView.setText(size.toString());

如果您需要以百万像素显示,那么您可以执行以下操作:

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

关于android - 如何在 android studio (Camera2) 中获得相机分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59860603/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com