相机的预览图像被拉伸 [英] Preview image from the camera is stretched

查看:322
本文介绍了相机的预览图像被拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,可以打开相机,开始预览并在屏幕上流式传输.重要说明:与相机没有实际的SurfaceView关联.只有一个虚拟SurfaceTexture:

I have an Android app that opens the camera, starts preview and streams it on screen. Important note: there is no real SurfaceView associated with the camera. There's only a dummy SurfaceTexture:

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

现在,我正在使用Camera.PreviewCallback获取图像.我进一步处理它无关紧要.到目前为止,我正在屏幕上显示它,但也最好将其保存在存储卡上.

Now, I'm getting the image using the 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.

现在,问题出在这里.我将预览尺寸设置为320x240.我得到了320x240尺寸的图像,一切似乎都很好.但是,一旦现实生活中的物体进入框架,我就可以清楚地看到图像被拉伸了.
我的活动的方向已锁定,不会旋转.当我相对于固定对象旋转设备时,我可以非常清楚地看到并确认图像已拉伸.为什么会这样,以及如何避免拉伸?

Now, the problem. I set preview size to 320x240. I get the image of 320x240 size, all seems fine. 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?

推荐答案

您的屏幕宽高比是否对应于预览的帧比? 确保onMeasure中正确的宽高比:

Does your screen aspect ratio correspond to your preview's frame ratio? Assure correct aspect ratio in 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));
}

来自项目

这篇关于相机的预览图像被拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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