Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更改camera2预览的纵横比?_Android_Android Camera2 - Fatal编程技术网

Android 如何更改camera2预览的纵横比?

Android 如何更改camera2预览的纵横比?,android,android-camera2,Android,Android Camera2,我尝试更改预览的纵横比,但失败:-( 对于裁剪,我必须使用这个工具,但我不能让它工作 我使用谷歌的例子进行测试 在方法中,我添加了以下行: mSensorSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); 我在信中补充说: final int centerX = mSensorSize.width() / 2; final int centerY = mSensorSize.heigh

我尝试更改预览的纵横比,但失败:-(

对于裁剪,我必须使用这个工具,但我不能让它工作

我使用谷歌的例子进行测试

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

mSensorSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
我在信中补充说:

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:-(


我做错了什么?

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

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());
        }
    }
}
我从我的相机片段中调用setAspectRation,如下所示

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

你在使用AutofittextureViews吗?当我更改纹理的大小查看视频变形:-(这可能是因为你更改了google Samples的Camera2Video代码中的其他内容它扭曲了预览没有hlp。不用担心,等一段时间我会更新我的答案@Reyans
  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);
                }