如何在纵向模式下在 Surfaceholder 上获得相机预览?

发布于 2024-11-19 16:46:25 字数 1613 浏览 1 评论 0原文

我正在使用以下自定义 SurfaceView 来执行某些 AR 任务。我在 AndroidManifest 中为我的 Activity 设置了 android:screenOrientation="portrait",但图像以奇怪的长宽比旋转了 90 度。我正在使用三星 Galaxy S...

有人可以帮忙吗?

public class CustomCameraView extends SurfaceView
{
    Camera camera;
    SurfaceHolder previewHolder;

    SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                camera = Camera.open();
                camera.setPreviewDisplay(previewHolder);
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }

        public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h)
        {
            Parameters params = camera.getParameters();
            params.setPreviewSize( 800, 480);
            params.setPictureFormat(PixelFormat.JPEG);
            camera.setParameters(params);
            camera.startPreview();
        }

        public void surfaceDestroyed(SurfaceHolder arg0)
        {
            camera.stopPreview();
            camera.release();
        }
    };

    public CustomCameraView(Context context)
    {
        super(context);

        previewHolder = this.getHolder();
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        previewHolder.addCallback(surfaceHolderListener);
    }

    protected void onDraw (Canvas canvas)
    {
        super.onDraw(canvas);
    }

    public void closeCamera()
    {
        if(camera != null)
            camera.release();
    }
}

I'm using the following custom SurfaceView for some AR task. I set android:screenOrientation="portrait" for my activity in the AndroidManifest, but the image is rotated by 90 deg with weird aspect ratio. I'm using a Samsung Galaxy S...

Anybody can help?

public class CustomCameraView extends SurfaceView
{
    Camera camera;
    SurfaceHolder previewHolder;

    SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                camera = Camera.open();
                camera.setPreviewDisplay(previewHolder);
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }

        public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int w, int h)
        {
            Parameters params = camera.getParameters();
            params.setPreviewSize( 800, 480);
            params.setPictureFormat(PixelFormat.JPEG);
            camera.setParameters(params);
            camera.startPreview();
        }

        public void surfaceDestroyed(SurfaceHolder arg0)
        {
            camera.stopPreview();
            camera.release();
        }
    };

    public CustomCameraView(Context context)
    {
        super(context);

        previewHolder = this.getHolder();
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        previewHolder.addCallback(surfaceHolderListener);
    }

    protected void onDraw (Canvas canvas)
    {
        super.onDraw(canvas);
    }

    public void closeCamera()
    {
        if(camera != null)
            camera.release();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-11-26 16:46:25

许多设备上的相机应用程序都有一个缺陷,它只能在横向模式下正确显示,将显示锁定为横向模式可以修复此问题,但还有其他问题(例如强制您管理布局)

Stackflow 讨论 1

Stackflow 讨论 2

等等...

一些设备+sdk 版本按照纵向工作网络上的文档和示例,有些没有,我只是希望他们不会,并实现了仅景观路线,有点痛苦,但它有效。

The camera application on many devices has a defect where it will only properly display in landscape mode, locking the display to landscape mode fixes this but has other problems (like forcing you to manage the layout)

Stackflow Discussion 1

Stackflow Discussion 2

Etc ...

Some devices+sdk versions work in portrait as per the doc's and samples around the web, some don't I have just come to expect they won't and implemented the landscape only route, a bit of a pain but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文