我有一部安装了Android 2.1操作系统的摩托罗拉Defy手机,并且正在开发一个带有相机预览功能的应用程序。问题是,在安装了Android 2.1操作系统的三星Galaxy S上,相机可以正常工作,但在摩托罗拉手机上,相机会旋转90度。我已经尝试过以下方法:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
但是它没有起作用。我尚未找到任何解决方案。
我有一部安装了Android 2.1操作系统的摩托罗拉Defy手机,并且正在开发一个带有相机预览功能的应用程序。问题是,在安装了Android 2.1操作系统的三星Galaxy S上,相机可以正常工作,但在摩托罗拉手机上,相机会旋转90度。我已经尝试过以下方法:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
但是它没有起作用。我尚未找到任何解决方案。
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
camera.setDisplayOrientation(90);
lp.height = previewSurfaceHeight;
lp.width = (int) (previewSurfaceHeight / aspect);
} else {
camera.setDisplayOrientation(0);
lp.width = previewSurfaceWidth;
lp.height = (int) (previewSurfaceWidth / aspect);
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera)
{
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation)
{
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
}
else
{ // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
Camera.CameraInfo.CAMERA_FACING_BACK
和 Camera.CameraInfo.CAMERA_FACING_FRONT
。 - Stevencamera.setDisplayOrientation(result);
之后尝试使用 camera.getParameters().setRotation(result);
。 - Pierre在2.1版本下,camera.setDisplayOrientation(int)不存在!
这段代码可能在某些设备上可行,但在我的Milestone/Droid上失败了 :(
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);
您可以在http://code.google.com/p/android/issues/detail?id=1193#c42中了解更多信息。
我找到了一段代码,适用于Android 1.6及以上版本(我在使用2.1版本时测试通过,可以在纵向模式下呈现预览而无需旋转)
public void surfaceCreated(SurfaceHolder holder){
try{
camera = Camera.open();
setDisplayOrientation(camera, 90);
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch(IOException e){
Log.d("CAMERA", e.getMessage());
}
}
protected void setDisplayOrientation(Camera camera, int angle){
Method downPolymorphic;
try
{
downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
}
catch (Exception e1)
{
}
}
在AndroidManifest.xml中,该活动具有android:screenOrientation="portrait"。
public static void setCameraDisplayOrientation(Activity activity,
int cameraId,android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
我认为您无法进行任何设置来支持API 2.2至2.1。该API不在您当前设备库中。您必须切换到2.2以支持API级别8。顺便说一下,我也尝试使用API级别7:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
这个函数在三星Galaxy Tab上运行良好,但在Nexus One上不行。三星Galaxy Tab使用的是2.2.0操作系统,而Nexus One使用的是2.2.1操作系统。当我尝试使用API级别8时:
camera.setDisplayOrientation(90);
它们两个都很好用。所以我认为当我们在Android OS 2.2.1上使用时,API级别7存在问题。