Android Camera 2预览大小和设备长宽比 [英] Android Camera 2 preview size and devices aspect ratio

查看:354
本文介绍了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_heightint 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天全站免登陆