问题描述
我需要实现 OrientationEventListener
让相机正常工作。谷歌发布 onOrientationChanged
的样本实现看起来像这样:
I need to implement an OrientationEventListener
to get the camera working correctly. Google posted a sample implementation of onOrientationChanged
that looks like this:
@Override
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return;
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mParameters.setRotation(rotation);
}
但我反对建设API级别8,所以我没有 CameraInfo
。我怎么能完成类似的事情上面,而不 CameraInfo
But I'm building against API level 8, so I don't have CameraInfo
. How can I accomplish a similar thing as the above without CameraInfo
?
推荐答案
虽然方向的变化,我觉得我也必须改变相机preVIEW来搭配,如果你想在旋转全屏图像,所以我使用类似的方法,但在像这样一个镜头面视图onLayout()方法:
Although the orientation changes, I find I have to also change the camera preview to match, if you want full-screen images on rotation, so I use a similar method but in the onLayout() method of a camera surface view like so:
/*
* This class provides the surface for the camera.
*/
public class CameraSurfaceView extends ViewGroup implements SurfaceHolder.Callback
{
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
if (changed)
{
final int width = right - left;
final int height = bottom - top;
int previewWidth = width;
int previewHeight = height;
if (mPreviewSize != null)
{
Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation())
{
case Surface.ROTATION_0:
previewWidth = mPreviewSize.height;
previewHeight = mPreviewSize.width;
mCamera.setDisplayOrientation(90);
break;
case Surface.ROTATION_90:
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
break;
case Surface.ROTATION_180:
previewWidth = mPreviewSize.height;
previewHeight = mPreviewSize.width;
break;
case Surface.ROTATION_270:
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
mCamera.setDisplayOrientation(180);
break;
}
}
final int scaledChildHeight = previewHeight * width / previewWidth;
cameraView.layout(0, height - scaledChildHeight, width, height);
}
}
}
这很适合我使用的Android API 8(Android 2.2的)的HTC Desire的。
This has worked for me on an HTC Desire using Android API 8 (Android 2.2).
这篇关于实施OrientationEventListener帮助相机问题,而CameraInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!