为了获得相机参考,我这样做:
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "Surface created!");
mCamera = Camera.open();
if (mCamera == null) {
// try to open front facing camera
try {
mCamera = Camera.open(0);
} catch (RuntimeException e) {
Toast.makeText(getApplicationContext(),
"No camera available!", Toast.LENGTH_LONG).show();
}
}
try {
mCamera.setPreviewDisplay(mCameraHolder);
setCameraDisplayOrientation(CameraActivity.this, 0, mCamera);
} catch (Exception e) {
e.printStackTrace();
if (mCamera != null)
mCamera.release();
mCamera = null;
}
}
setCameraDisplayOrientation 在哪里(我在某个地方的stackoverflow上找到):
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);
}
当我用我的 Nexus 4 拍照时,它的方向是正确的。当我使用只有前置摄像头的 Nexus 7 进行尝试时,它被镜像(倒置)(即使实际上没有这段代码,所以我猜它什么也没做。)。
有没有可靠的方法来获取相机方向并使其工作?我在 Google 和 Stackoverflow 上进行了搜索,但到目前为止没有找到任何有效的方法。