我正在使用此代码:
在清单中,活动方向设置为横向.
因此,就像允许用户仅在横向模式下拍照,如果通过将设备保持在纵向模式下拍照,则保存的图像如下所示:
90度旋转的图像.
在寻找解决方案之后,我发现了这一点:
解决方案在哪里:
在surfaceChanged()中检查
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
display.getRotation();
并相应地更改相机的displayOrientation.
camera.setDisplayOrientation(90);
但是,无论我旋转设备多少次,都不会调用surfaceChanged().
我什至尝试删除Manifest.xml中的orientation =“ Landscape”,但是预览本身会横向显示(可能是因为默认的android.view.SurfaceView应该处于横向模式?).
最佳答案
尝试这个.
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
camParam = camera.getParameters();
Camera.Parameters params = camera.getParameters();
String currentversion = android.os.Build.VERSION.SDK;
Log.d("System out", "currentVersion " + currentversion);
int currentInt = android.os.Build.VERSION.SDK_INT;
Log.d("System out", "currentVersion " + currentInt);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
if (currentInt != 7) {
camera.setDisplayOrientation(90);
} else {
Log.d("System out", "Portrait " + currentInt);
params.setRotation(90);
/*
* params.set("orientation", "portrait");
* params.set("rotation",90);
*/
camera.setParameters(params);
}
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// camera.setDisplayOrientation(0);
if (currentInt != 7) {
camera.setDisplayOrientation(0);
} else {
Log.d("System out", "Landscape " + currentInt);
params.set("orientation", "landscape");
params.set("rotation", 90);
camera.setParameters(params);
}
}
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
Log.d("CAMERA", e.getMessage());
}
}