题
我有一个摩托罗拉无视系统安卓2.1和我让一个应用程序有摄像机的预览。问题是,该摄像机运作现在Galaxy S与安卓2.1,但是对摩托罗拉的照相机转动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)
{
}
}
该活动具有android:屏幕方向= androidmanifest.xml上的“肖像”
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);
}
我认为你可以不作任何设置,以支持API2.2至2.1.API没有在你目前的设备lib。你必须改变2.2支持API level8.顺便说一句,我也尽量使用API7级:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
这个函数的运作良好的照片但关系的一个。本星使用OS的2.2.0和一使用OS2.2.1.当我试图使用API level8:
camera.setDisplayOrientation(90);
他们两个很好的工作。因此,我认为API7级有问题,当我们使用上的安卓OS2.2.1.
不隶属于 StackOverflow