Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 实现OrientationEventListener以帮助没有CameraInfo的相机问题?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要实现OrientationEventListener以使相机正常工作. Google发布了onOrientationChanged的示例实现,如下所示:

@H_419_11@@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,我怎样才能完成与上面相似的事情?

解决方法

然方向发生了变化,但我发现我还必须更改相机预览以匹配,如果你想要旋转全屏图像,所以我使用类似的方法,但在相机表面视图的onLayout()方法中如下:

@H_419_11@/* * 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有用.

@H_675_24@

大佬总结

以上是大佬教程为你收集整理的android – 实现OrientationEventListener以帮助没有CameraInfo的相机问题?全部内容,希望文章能够帮你解决android – 实现OrientationEventListener以帮助没有CameraInfo的相机问题?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。