我如何才能仅在android studio(camera2)中获得相机分辨率 [英] How can I get just the camera resolution in android studio (Camera2)

查看:282
本文介绍了我如何才能仅在android studio(camera2)中获得相机分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

   @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 的初始化实例,然后传递照相机您希望从中获得分辨率的镜头标识符.

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

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

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

这篇关于我如何才能仅在android studio(camera2)中获得相机分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆