我有一个 Motorola Defy OS Android 2.1,我用相机预览制作了一个应用程序。问题是摄像头在装有 Android 2.1 的三星 Galaxy S 上运行良好,但在摩托罗拉上,摄像头旋转了 90 度。我试图这样做:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
但它不起作用。我还没有找到任何解决方案。
我有一个 Motorola Defy OS Android 2.1,我用相机预览制作了一个应用程序。问题是摄像头在装有 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);
}
现在在 Android 文档中有官方示例代码(在 setDisplayOrientation() 下):
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.setDisplayOrientation(int) 在 2.1 下不存在!
这段代码可能有效,但在我的里程碑/机器人中失败:(
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 上运行良好。三星 Galaxy Tab 使用 OS 2.2.0,而 Nexus 使用 OS 2.2.1。当我尝试使用 API 级别 8 时:
camera.setDisplayOrientation(90);
他们都运作良好。所以我认为当我们在 Android OS 2.2.1 上使用 API 级别 7 时会出现问题。