http://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways

I am using a Preview to display what the camera see's on the screen.

I can get everything working fine, surface created, surface set and the surface is displayed.

However it always displays the picture at an incorrect 90 degree angle in portrait mode.

Such as in the picture:

I am aware that using the following code will set the picture straight:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

However I have the Preview within an Activity that has other elements in it and it does not make sense for my Activity to be displayed in landscape mode. (Its disabled by default)

So I was wondering is there anyway to just change the orientation of the Preview? And leave the rest of my Activity correctly displayed in Portrait mode?

Or anyway to rotate the preview so that it is displayed correctly?

1 Answer

This issue appeared to start out as a bug with certain hardware see here but can be overcome by using the call to mCamera.setDisplayOrientation(degrees) available in API 8. So this is how I implement it:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)   {           if (isPreviewRunning)     {       mCamera.stopPreview();     }      Parameters parameters = mCamera.getParameters();     Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();      if(display.getRotation() == Surface.ROTATION_0)     {       parameters.setPreviewSize(height, width);                     mCamera.setDisplayOrientation(90);     }      if(display.getRotation() == Surface.ROTATION_90)     {       parameters.setPreviewSize(width, height);                   }      if(display.getRotation() == Surface.ROTATION_180)     {       parameters.setPreviewSize(height, width);             }      if(display.getRotation() == Surface.ROTATION_270)     {       parameters.setPreviewSize(width, height);       mCamera.setDisplayOrientation(180);     }      mCamera.setParameters(parameters);     previewCamera();              } 

And the previewCamera method :

public void previewCamera() {       try    {           mCamera.setPreviewDisplay(mSurfaceHolder);          mCamera.startPreview();     isPreviewRunning = true;   }   catch(Exception e)   {     Log.d(APP_CLASS, "Cannot start preview", e);     } } 

This was on an HTC Desire and I had to initially put in logging statements in each of the rotation checks to say what the rotation was and then debugged on the device and watched the logCat output while I rotated the device. For the HTC Desire, 0 was the phone as you would have expected (portrait), 90 degrees was turning the phone 90 degrees COUNTER-CLOCKWISE (I had assumed it would have been clockwise). In the code you'll see I didn't need to do any display rotation when the phone was at 90 or 180 degrees - the device seemed to handle this itself. Only one point not working properly: The 270 degree rotation is when you turn the device 90 degrees clockwise and the display rotation counters that ok but if you rotate the device 270 degrees counter-clockwise, it doesn't appear to compensate it properly.

P.S. Note the swapover of width and height in the appropriate rotations.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Python可以自学吗?有什么用途?
  2. Spark Structured Streaming高级特性
  3. Java实例开发教程:SpringBoot开发案例
  4. 内容分发战场,谁能活着离开?
  5. 快速提高技术的方法 & 长期欢迎投稿
  6. python和Java学哪个好找工作?Python学习
  7. flink两三事 ----(1)历史
  8. 参加Python培训保就业吗?Python学习分享!
  9. 1819、shell脚本基础-bash变量和逻辑运行
  10. Table API&SQL的基本概念及使用介绍