如何在surfaceview中将Android相机更改为人像?
Posted
技术标签:
【中文标题】如何在surfaceview中将Android相机更改为人像?【英文标题】:how to change the Android-camera to portrait in surfaceview? 【发布时间】:2012-08-18 20:27:23 【问题描述】:我正在为 android 平板电脑编写代码,我希望我的应用程序使用 surfaceView
相机预览的纵向视图。默认是横向的,我尝试了以下代码来旋转到纵向视图:
public void surfaceCreated(SurfaceHolder holder)
// The Surface has been created, acquire the camera and tell it where to draw.
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
// If we aren't landscape (the default), tell the camera we want portrait mode
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
params.set("orientation", "portrait"); // "landscape"
// And Rotate the final picture if possible
// This works on 2.0 and higher only
//params.setRotation(90);
// Use reflection to see if it exists and to call it so you can support older versions
try
Method rotateSet = Camera.Parameters.class.getMethod("setRotation", new Class[] Integer.TYPE );
Object arguments[] = new Object[] new Integer(270) ;
rotateSet.invoke(params, arguments);
catch (NoSuchMethodException nsme)
// Older Device
Log.v("CameraView","No Set Rotation");
catch (IllegalArgumentException e)
Log.v("CameraView","Exception IllegalArgument");
catch (IllegalAccessException e)
Log.v("CameraView","Illegal Access Exception");
catch (InvocationTargetException e)
Log.v("CameraView","Invocation Target Exception");
mCamera.setParameters(params);
try
mCamera.setPreviewDisplay(holder);
catch (IOException exception)
mCamera.release();
mCamera = null;
但它不起作用。请问有人可以解决吗?
【问题讨论】:
也尝试了 Object arguments[] = new Object[] new Integer(90) ;但我仍然无法修复它。 既然你已经让surfaceview工作了,你能给我举个例子吗?我需要从 ipcamera 流式传输到我的 android,并且我想使用 surfaceview 【参考方案1】:您可能希望使用setDisplayOrientation
函数,如下所示:
public void surfaceCreated(SurfaceHolder holder)
if (Build.VERSION.SDK_INT >= 8) mCamera.setDisplayOrientation(90);
使用相机 params.set("orientation"...
的东西在不同设备之间并不一致,并且实际上是 SDK 8 之前的语言。
【讨论】:
以上是关于如何在surfaceview中将Android相机更改为人像?的主要内容,如果未能解决你的问题,请参考以下文章
如何在Android中修复相机预览(surfaceview)的正确纵横比?
如何在 SurfaceView Android 上制作裁剪相机视图