我正在使用这段代码:
在 Manifest
中,Activity Orientation
设置为 Landscape。
所以,这就像只允许用户在横向模式下拍照,如果是在纵向模式下手持设备拍照,则保存的图像是这样的:
90 度旋转的图像。
在搜索解决方案后,我发现了这个:
Android - Camera preview is sideways
解决方案在哪里:
在 surfaceChanged()
中检查
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
display.getRotation();
并相应地更改相机的显示方向。
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());
}
}
关于android - 图像以错误的方向保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098281/