Android Camera 2 预览尺寸和设备纵横比 [英] Android Camera 2 preview size and devices aspect ratio

查看:16
本文介绍了Android Camera 2 预览尺寸和设备纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的项目中实现 Camera 2 API.我正在使用 TextureView 和这些代码行来设置相机全屏预览大小:

I'm implementing Camera 2 API in my project. I'm using TextureView and these line of codes to set the camera fullscreen preview size:

StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];

这似乎是设备支持的最大预览尺寸.我不确定这个尺寸是否适用于所有设备并适合其设备的纵横比而不会被拉伸.有人知道吗?

This seems to be the largest preview size that device support. I'm not sure if this size works with all devices and fit its device's aspect ratio without being stretched. Does anyone know?

推荐答案

如果您的相机分辨率、纹理视图和设备的显示尺寸不同,那么您必须调整尺寸.为此,您必须将 TextureView 放入 FrameLayout.下面的代码适用于具有各种显示分辨率的所有设备.

If your Camera resolutions , texture view and your device's display dimensions are not same then you have to adjust the dimensions. For that you have to put your TextureView inside of FrameLayout. Below Code is applicable to all the devices with various Display resolutions.

如果您在全屏预览,请使用您的显示尺寸.使用 int DSI_height, int DSI_width 全局变量.

Take your Display Dimetions if you are previewing in full screen.Take int DSI_height, int DSI_width global variable.

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    DSI_height = displayMetrics.heightPixels;
    DSI_width = displayMetrics.widthPixels;

从Camera2 API中选择您需要的分辨率并分配给Size imageDimension,全局取private Size imageDimension并使用

select your required resolutions from Camera2 API and assign to Size imageDimension, Take private Size imageDimension globally and use

setAspectRatioTextureView(imageDimension.getHeight(),imageDimension.getWidth());

并使用以下逻辑

private void setAspectRatioTextureView(int ResolutionWidth , int ResolutionHeight )
    {
        if(ResolutionWidth > ResolutionHeight){
            int newWidth = DSI_width;
            int newHeight = ((DSI_width * ResolutionWidth)/ResolutionHeight);
            updateTextureViewSize(newWidth,newHeight);

        }else {
            int newWidth = DSI_width;
            int newHeight = ((DSI_width * ResolutionHeight)/ResolutionWidth);
            updateTextureViewSize(newWidth,newHeight);
        }

    }

    private void updateTextureViewSize(int viewWidth, int viewHeight) {
        Log.d(TAG, "TextureView Width : " + viewWidth + " TextureView Height : " + viewHeight);
        textureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
    }

这篇关于Android Camera 2 预览尺寸和设备纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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