简体   繁体   English

相机的预览图像被拉伸

[英]Preview image from the camera is stretched

I have an Android app that opens the camera, starts preview and streams it on screen. 我有一个Android应用程序,可以打开相机,开始预览并在屏幕上流式传输。 Important note: there is no real SurfaceView associated with the camera. 重要说明:没有与相机关联的实际SurfaceView There's only a dummy SurfaceTexture : 只有一个虚拟的SurfaceTexture

m_previewTexture = new SurfaceTexture(58346);
camera.setPreviewTexture(m_previewTexture);

Now, I'm getting the image using the Camera.PreviewCallback . 现在,我使用Camera.PreviewCallback获取图像。 It is irrelevant what I'm doing with it further. 我进一步处理它无关紧要。 So far I'm displaying it on the screen, but I might as well be saving it on the memory card. 到目前为止,我正在屏幕上显示它,但也最好将其保存在存储卡上。

Now, the problem. 现在,问题了。 I set preview size to 320x240. 我将预览尺寸设置为320x240。 I get the image of 320x240 size, all seems fine. 我得到了320x240尺寸的图像,一切似乎都很好。 But as soon as real life objects come into the frame, I can clearly see that the image is stretched. 但是,一旦现实生活中的物体进入框架,我就可以清楚地看到图像被拉伸了。
My activity's orientation is locked, it doesn't rotate. 我的活动方向已锁定,不会旋转。 As I rotate the device relative to a fixed object, I can very clearly see and confirm that the image is stretched. 当我相对于固定对象旋转设备时,我可以非常清楚地看到并确认图像已拉伸。 Why could this be and how to avoid stretching? 为什么会这样,以及如何避免拉伸?

Does your screen aspect ratio correspond to your preview's frame ratio? 屏幕长宽比是否与预览的帧长相对应? Assure correct aspect ratio in onMeasure: 确保onMeasure中正确的宽高比:

@Override
protected void onMeasure(int widthSpec, int heightSpec) {
    if (this.mAspectRatio == 0) {
        super.onMeasure(widthSpec, heightSpec);
        return;
    }
    int previewWidth = MeasureSpec.getSize(widthSpec);
    int previewHeight = MeasureSpec.getSize(heightSpec);

    int hPadding = getPaddingLeft() + getPaddingRight();
    int vPadding = getPaddingTop() + getPaddingBottom();

    previewWidth -= hPadding;
    previewHeight -= vPadding;

    boolean widthLonger = previewWidth > previewHeight;
    int longSide = (widthLonger ? previewWidth : previewHeight);
    int shortSide = (widthLonger ? previewHeight : previewWidth);
    if (longSide > shortSide * mAspectRatio) {
        longSide = (int) ((double) shortSide * mAspectRatio);
    } else {
        shortSide = (int) ((double) longSide / mAspectRatio);
    }
    if (widthLonger) {
        previewWidth = longSide;
        previewHeight = shortSide;
    } else {
        previewWidth = shortSide;
        previewHeight = longSide;
    }

    // Add the padding of the border.
    previewWidth += hPadding;
    previewHeight += vPadding;

    // Ask children to follow the new preview dimension.
    super.onMeasure(MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY));
}

from this project 来自这个项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM