我正在使用libgdx制作一个应用程序,我需要使用相机,所以我按照this教程,我的所有相机进纸都旋转了90度,但它们被绘制好像不是。不幸的是,这意味着预览完全失真并且很难使用。
我不会在这里发布我的代码,除非要求片段,因为我将教程中的代码复制粘贴到我的游戏中。我记得的唯一变化如下:
我在CameraSurace.java中更改了原始的surfaceCreated()方法
public void surfaceCreated( SurfaceHolder holder ) {
// Once the surface is created, simply open a handle to the camera hardware.
camera = Camera.open();
}
打开前置摄像头(我使用只有前置摄像头的Nexus 7 ......)
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;
}
除了那个改变之外,代码的其余部分几乎完全相同(不包括小变量等等。)
答案 0 :(得分:1)
您可以使用ExifInterface
课程来确定与您的图片相关联的ORIENTATION_TAG
并相应地旋转图片。
代码如下所示:
ei = new ExifInterface(imagePath);
orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
imageView.setRotation(90);
break;
...
default:
break;
}
答案 1 :(得分:0)
在深入了解相机API后,我发现我所要做的就是使用一个名为setDisplayOrientation(90)
的漂亮小方法,它现在可以很好地工作。
@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)