如何旋转Android设备摄像头preVIEW(libgdx) [英] How to rotate android device camera preview(libgdx)

查看:137
本文介绍了如何旋转Android设备摄像头preVIEW(libgdx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libgdx提出申请,我需要用的相机,所以我跟着的本教程的和我所有的摄像头输入旋转90度,但是,如果他们没有,他们正在制定。不幸的是,这意味着在preVIEW是完全歪曲,是非常难用。

I'm using libgdx to make an application and I need to use the camera so I followed this tutorial and all my camera feed is rotated 90 degrees but they are being drawn as if they weren't. Unfortunately that means the preview is totally distorted and is very hard to use.

除非片段被询问,因为我复制粘贴在教程中的code到我的比赛。我记得曾有如下唯一的变化,我不会在这里发布我的code

I won't post my code here unless snippets are asked for because I copy-pasted the code from the tutorial into my game.. The only change I recall making was as follows.

我CameraSurace.java改变了原来的surfaceCreated()方法

I changed the original surfaceCreated() method in CameraSurace.java

 public void surfaceCreated( SurfaceHolder holder ) {
    // Once the surface is created, simply open a handle to the camera hardware.
    camera = Camera.open();
}

打开前置摄像头(IM使用的是Nexus 7只有拥有前置摄像头......)

to open the front facing camera (Im using a Nexus 7 that only has a front camera...)

    public void surfaceCreated( SurfaceHolder holder ) {
    // Once the surface is created, simply open a handle to the camera hardware.
    camera = openFrontFacingCamera();
}


@SuppressLint("NewApi")
private Camera openFrontFacingCamera() 
{
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                System.out.println("Falied to open.");
            }
        }
    }

    return cam;
}

除此之外改变code的其余部分几乎是完全相同的(不含次要变量的变化和这样。)

Other than that change the rest of the code is almost the exact same (excluding minor variable changes and such.)

推荐答案

一旦潜入照相机API,我发现所有我需要做的就是用所谓的一个不错的方法 setDisplayOrientation(90),现在完美的作品。

Upon diving into the camera API, I found that all I have to do is use a nice little method called setDisplayOrientation(90) and it works perfectly now.

修改code:

    @SuppressLint("NewApi")
public void surfaceCreated( SurfaceHolder holder ) {
    // Once the surface is created, simply open a handle to the camera hardware.
    camera = openFrontFacingCamera();
    camera.setDisplayOrientation(90);
}


@SuppressLint("NewApi")
private Camera openFrontFacingCamera() 
{
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                System.out.println("Falied to open.");
            }
        }
    }

    return cam;
}

PS只有我无视原因 NewApi 是因为我知道这个程序会在运行精确的设备,它是特定于该设备...请问不推荐,除非你知道设备的API足够高...(只需要API 8)

P.S only reason I'm ignoring the NewApi is because I know the exact device this app will be running on, and it is specific to that device... Would not recommend unless you know that the device's API is high enough... (it only requires API 8)

这篇关于如何旋转Android设备摄像头preVIEW(libgdx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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