【发布时间】:2013-04-15 12:24:02
【问题描述】:
为了获得相机参考,我这样做:
@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 上进行了搜索,但到目前为止没有找到任何可行的方法。
【问题讨论】:
-
我很确定这是正常的(和预期的行为),所有用于视频聊天的“网络摄像头”都会这样做。如果它们没有被镜像,那么移动到屏幕左侧会使您的图像移动到屏幕右侧,这会使用户有些迷失方向。图像是否在没有镜像的情况下存储?
-
好的,现在我不确定垂直方向是否正确。所以当我给我的脸拍照时,它是颠倒的......
-
嗯,好吧,那是不对的。我有 nexi 4、7、10 和一些三星标签。如果我下午晚些时候有时间,我会去看看。
-
你有时间去看看吗?因为我没有在这方面取得任何进展
-
没关系,我的“临时”解决方案是翻转从前置摄像头接收到的每张照片。不过还是谢谢。如果有人有“更好”的解决方案,... :)
标签: android camera android-camera