android camera rotate,Android camera rotate

I have a Motorola Defy OS Android 2.1 and I make an application with camera Preview. The problem is that the camera works fine on Samsung Galaxy S with Android 2.1, but on Motorola the camera is rotated with 90 degrees. I have tried to do this:

Parameters parameters = camera.getParameters();

parameters.setRotation(90);

but it's not working. I didn't find any solution yet.

Peter

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);

}

There is official example code for this in the Android docs now (under 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) is not exist under 2.1!

And this code may work, but fail in my milestone/droid :(

Camera.Parameters parameters = camera.getParameters();

parameters.set("orientation", "portrait");

camera.setParameters(parameters);

I found this code that works in Android 1.6 and over (works for me using 2.1 and present preview in portrait mode without rotating)

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)

{

}

}

The activity has android:screenOrientation="portrait" on 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);

}

None

I think that you can not make any setting to support the API 2.2 to 2.1. The API doesn't have in your current device lib. You must change to 2.2 to support API level 8. By the way, I also try to use the API level 7:

Parameters parameters = camera.getParameters();

parameters.setRotation(90);

This function works well on the Samsung Galaxy Tab but Nexus one. The Samsung Galaxy Tab uses OS 2.2.0 and the Nexus one uses OS 2.2.1. When I try to use API level 8:

camera.setDisplayOrientation(90);

both of them work well. So I think that the API level 7 has problem when we use on Android OS 2.2.1.

来源:https://stackoverflow.com/questions/5309029/android-camera-rotate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值