Advertisement
vamsiampolu

setCameraOrientation

Jan 21st, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. //This is what I am currently doing for orientation:
  2.  
  3. private void startPreview() throws CameraHardwareException
  4.     {
  5.         if(mPausing || isFinishing())
  6.             return;
  7.         ensureCamera();
  8.         if(mStatus!=PREVIEW_STOPPED)
  9.             stopPreview();
  10.         setPreviewDisplay(surfaceHolder);
  11.         mUpdateSet=INIT_PARAMS;
  12.         setCameraParameters(INIT_PARAMS);
  13.         setDisplayOrientation();
  14.         try
  15.         {
  16.             Log.d(TAG, "Trying to start the preview");
  17.             mCamera.startPreview();
  18.         }
  19.         catch(Throwable ex)
  20.         {
  21.             closeCamera();
  22.             throw new RuntimeException("Could not start camera preview");
  23.         }
  24.         mPreviewing=true;
  25.     }
  26.    
  27.    private void setDisplayOrientation()
  28.     {
  29.         mDisplayRotation=getDisplayRotation(this);
  30.         mDisplayOrientation=getDisplayOrientation(mDisplayRotation, CameraInfo.CAMERA_FACING_BACK);
  31.         if(mCamera!=null)
  32.         {  
  33.             mCamera.setDisplayOrientation(mDisplayOrientation);
  34.             if(mParameters!=null)
  35.             {
  36.                 mParameters.setRotation(mDisplayOrientation);
  37.             }
  38.             else
  39.             {
  40.                 Camera.Parameters params=mCamera.getParameters();
  41.                 params.setRotation(mDisplayOrientation);
  42.                 mCamera.setParameters(params);
  43.             }
  44.         }
  45.     }
  46.  
  47. //I guess you have seen these before:
  48.  
  49. private int getDisplayOrientation(int degrees, int cameraId) {
  50.         // See android.hardware.Camera.setDisplayOrientation for
  51.         // documentation.
  52.         Camera.CameraInfo info = new Camera.CameraInfo();
  53.         Camera.getCameraInfo(cameraId, info);
  54.         int result;
  55.         if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  56.             result = (info.orientation + degrees) % 360;
  57.             result = (360 - result) % 360;  // compensate the mirror
  58.         } else {  // back-facing
  59.             result = (info.orientation - degrees + 360) % 360;
  60.         }
  61.         //add it here??? mCamera.setDisplayOrientation(result);
  62.         return result;
  63.     }
  64.    
  65.      private int getDisplayRotation(Activity activity) {
  66.             int rotation = activity.getWindowManager().getDefaultDisplay()
  67.                     .getRotation();
  68.             switch (rotation) {
  69.                 case Surface.ROTATION_0: return 0;
  70.                 case Surface.ROTATION_90: return 90;
  71.                 case Surface.ROTATION_180: return 180;
  72.                 case Surface.ROTATION_270: return 270;
  73.             }
  74.             return 0;
  75.         }
  76.  
  77. class MainHandler extends Handler
  78.     {
  79.         @Override
  80.         public void handleMessage(Message msg)
  81.         {
  82.             switch(msg.what)
  83.             {
  84.                 case FIRST_TIME_INIT:
  85.                     initializeFirstTime();
  86.                     break;
  87.                 case CLEAR_SCREEN_DELAY:
  88.                     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  89.                     break;
  90.                    
  91.                 case UPDATE_PARAMS_WHEN_IDLE:
  92.                     setCameraParametersWhenIdle(0);
  93.                     break;
  94.                    
  95.                 case CHECK_DISPLAY_ROTATION:
  96.                     if(getDisplayRotation(CameraActivity.this)!=mDisplayRotation)
  97.                         setDisplayOrientation();
  98.                     if(SystemClock.uptimeMillis()-mResumeTime<5000)
  99.                         handler.sendEmptyMessageDelayed(CHECK_DISPLAY_ROTATION,100);
  100.             }
  101.         }
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement