如何更改camera2预览的宽高比? [英] How to change aspect ratio of camera2 preview?

查看:1483
本文介绍了如何更改camera2预览的宽高比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更改 Camera2 预览,但我失败了:-(

I try to change the aspect ratio of the Camera2 preview but I fail :-(

要进行裁剪,我必须使用 SCALER_CROP_REGION 但我不起作用.

For cropping I have to use the SCALER_CROP_REGION but I don't get it working.

我使用Google的 android-Camera2Video 示例进行测试.

I used the android-Camera2Video example from Google for my tests.

openCamera 方法中,我添加了以下行:

In the openCamera method I added the following line:

mSensorSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

并在 startPreview 我添加了:

final int centerX = mSensorSize.width() / 2;
final int centerY = mSensorSize.height() / 2;
final int cropSize = Math.min(mSensorSize.width(), mSensorSize.height());
final Rect crop = new Rect(centerY - cropSize / 2,
                           centerX - cropSize / 2,
                           cropSize,
                           cropSize);
mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, crop);

我应该以1:1的比例获得预览,但预览比例应为3:4:-(

I should get a preview with a 1:1 ratio but it is 3:4 :-(

我怎么了?

推荐答案

您可以尝试在AutofitTextureView类中更改onMeasure方法

you may try to change onMeasure method inside your AutofitTextureView class

public class AutoFitTextureView extends TextureView {

    int maxwidth = 0;
    int maxheight = 0;
    private int mRatioWidth = 0;
    private int mRatioHeight = 0;
    private Size previewSize;

    public AutoFitTextureView(Context context) {
        this(context, null);
    }

    public AutoFitTextureView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setAspectRatio(int width, int height, int maxwidth, int maxheight, Size preview) {
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }
        mRatioWidth = width;
        mRatioHeight = height;
        this.maxwidth = maxwidth;
        this.maxheight = maxheight;
        this.previewSize = preview;
        enterTheMatrix();
        requestLayout();
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        boolean isFullBleed = true;
        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight,height);
         }

    }


    private void adjustAspectRatio(int previewWidth,
                                   int previewHeight,
                                   int rotation) {
        Matrix txform = new Matrix();
        int viewWidth = getWidth();
        int viewHeight = getHeight();
        RectF rectView = new RectF(0, 0, viewWidth, viewHeight);
        float viewCenterX = rectView.centerX();
        float viewCenterY = rectView.centerY();
        RectF rectPreview = new RectF(0, 0, previewHeight, previewWidth);
        float previewCenterX = rectPreview.centerX();
        float previewCenterY = rectPreview.centerY();

        if (Surface.ROTATION_90 == rotation ||
                Surface.ROTATION_270 == rotation) {
            rectPreview.offset(viewCenterX - previewCenterX,
                    viewCenterY - previewCenterY);

            txform.setRectToRect(rectView, rectPreview,
                    Matrix.ScaleToFit.FILL);

            float scale = Math.max((float) viewHeight / previewHeight,
                    (float) viewWidth / previewWidth);

            txform.postScale(scale, scale, viewCenterX, viewCenterY);
            txform.postRotate(90 * (rotation - 2), viewCenterX,
                    viewCenterY);
        } else {
            if (Surface.ROTATION_180 == rotation) {
                txform.postRotate(180, viewCenterX, viewCenterY);
            }
        }

        if (LollipopCamera.type == 1) {
            txform.postScale(-1, 1, viewCenterX, viewCenterY);
        }

        setTransform(txform);
    }

    private void enterTheMatrix() {
        if (previewSize != null) {
            adjustAspectRatio(mRatioWidth,
                    mRatioHeight,
                    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRotation());
        }
    }
}

这是在应用上述方法之前

这是应用上述方法之后的
Camera2Video谷歌示例

this is before applying above method

and this is after applying above method
Camera2Video google sample

我也删除了fragment_camera2_video.xml

 android:layout_below="@id/texture"
    android:background="#4285f4"

我正在从我的相机Fragment调用setAspectRation,如下所示

I am calling setAspectRation from my camera Fragment as follows

  mPreviewSize = chooseOptimalSize(map.getOutputSizes(ImageFormat.JPEG),
                        rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                        maxPreviewHeight, largest);

                // We fit the aspect ratio of TextureView to the size of preview we picked.
                int orientation = getResources().getConfiguration().orientation;
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    mTextureView.setAspectRatio(
                            mPreviewSize.getWidth(), mPreviewSize.getHeight(), universal_width, universal_height, largest);
                } else {
                    mTextureView.setAspectRatio(
                            mPreviewSize.getHeight(), mPreviewSize.getWidth(), universal_width, universal_height, largest);
                }

这篇关于如何更改camera2预览的宽高比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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