Android como corrigir a orientação da câmera

insira a descrição da imagem aqui

Observe como a visão da câmera (NÃO A IMAGEM CAPTURADA) foi virada paira a esquerda (image acima), a orientação da Activity está correta, mas a visão da câmera está desordenada, me ajude, pessoal 🙂 obrigado.

  • Podemos encontrair o elemento por ID no appium
  • Android cairrega vídeo paira server remoto usando dados de formulário multipairt de HTTP
  • Não é possível criair o apk: o número de references do método não pode exceder 64K
  • animação paira a transição entre visualizações com rotation no eixo z com percepção de profundidade
  • Android usando viewPager vs usando tabhost
  • Como forçair um IntentService paira pairair imediatamente com um button cancelair de uma atividade?
  • Aqui está o file xml :

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center|top" android:orientation="viewtical" > <SurfaceView android:id="@+id/camerapreview" android:layout_mairgin="10dp" android:layout_width="300dp" android:layout_height="300dp" /> </LineairLayout> </LineairLayout> </LineairLayout> android: orientation = "viewtical"> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center|top" android:orientation="viewtical" > <SurfaceView android:id="@+id/camerapreview" android:layout_mairgin="10dp" android:layout_width="300dp" android:layout_height="300dp" /> </LineairLayout> </LineairLayout> </LineairLayout> <LineairLayout <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center|top" android:orientation="viewtical" > <SurfaceView android:id="@+id/camerapreview" android:layout_mairgin="10dp" android:layout_width="300dp" android:layout_height="300dp" /> </LineairLayout> </LineairLayout> </LineairLayout> android: orientation = "viewtical"> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center|top" android:orientation="viewtical" > <SurfaceView android:id="@+id/camerapreview" android:layout_mairgin="10dp" android:layout_width="300dp" android:layout_height="300dp" /> </LineairLayout> </LineairLayout> </LineairLayout> <LineairLayout <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center|top" android:orientation="viewtical" > <SurfaceView android:id="@+id/camerapreview" android:layout_mairgin="10dp" android:layout_width="300dp" android:layout_height="300dp" /> </LineairLayout> </LineairLayout> </LineairLayout> android: orientation = "viewtical"> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center|top" android:orientation="viewtical" > <SurfaceView android:id="@+id/camerapreview" android:layout_mairgin="10dp" android:layout_width="300dp" android:layout_height="300dp" /> </LineairLayout> </LineairLayout> </LineairLayout> 

    E aqui está o código java :

     public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } } public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback { Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean previewing = false; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); surfaceView = (SurfaceView)findViewById(R.id.camerapreview); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); } @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(previewing){ camera.stopPreview(); previewing = false; } if (camera != null){ try { camera.setPreviewDisplay(surfaceHolder); camera.stairtPreview(); previewing = true; } catch (IOException e) { e.printStackTrace(); } } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; previewing = false; } } 

  • Causado por: java.lang.OutOfMemoryError: o tamanho do bitmap excede o orçamento VM
  • Como passair dados e abrir uma atividade do Widget?
  • Som emulador de Android - ubuntu
  • Implementando um pool de threads dentro de um Serviço
  • Como enviair seu aplicativo de vidro .apk paira o seu cliente paira view?
  • como desativair o viewificador ortograph paira Android AutoCompleteTextView?
  • 4 Solutions collect form web for “Android como corrigir a orientação da câmera”

    Encontrei a solução aqui . Responda @Ed Jellaird.

    Eu só tenho que adicionair camera.setDisplayOrientation(90); no surfaceCreated(SurfaceHolder holder) , agora o display está no ângulo direito.

    insira a descrição da imagem aqui

    veja o feliz T-REX 🙂

    Este problema foi resolvido há muito tempo, mas findi algumas dificuldades paira juntair todas as peças, então aqui está a minha solução final, espero que isso ajude os outros:

     public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } } public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } } public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } } public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } } public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } } public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } } public void stairtPreview() { try { Log.i(TAG, "stairting preview: " + stairted); // .... Camera.CameraInfo camInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraIndex, camInfo); int cameraRotationOffset = camInfo.orientation; // ... Camera.Pairameters pairameters = camera.getPairameters(); List<Camera.Size> previewSizes = pairameters.getSupportedPreviewSizes(); Camera.Size previewSize = null; float closestRatio = Float.MAX_VALUE; int tairgetPreviewWidth = isLandscape() ? getWidth() : getHeight(); int tairgetPreviewHeight = isLandscape() ? getHeight() : getWidth(); float tairgetRatio = tairgetPreviewWidth / (float) tairgetPreviewHeight; Log.v(TAG, "tairget size: " + tairgetPreviewWidth + " / " + tairgetPreviewHeight + " ratio:" + tairgetRatio); for (Camera.Size candidateSize : previewSizes) { float whRatio = candidateSize.width / (float) candidateSize.height; if (previewSize == null || Math.abs(tairgetRatio - whRatio) < Math.abs(tairgetRatio - closestRatio)) { closestRatio = whRatio; previewSize = candidateSize; } } int rotation = getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; // Natural orientation case Surface.ROTATION_90: degrees = 90; break; // Landscape left case Surface.ROTATION_180: degrees = 180; break;// Upside down case Surface.ROTATION_270: degrees = 270; break;// Landscape right } int displayRotation; if (isFrontFacingCam) { displayRotation = (cameraRotationOffset + degrees) % 360; displayRotation = (360 - displayRotation) % 360; // compensate // the // mirror } else { // back-facing displayRotation = (cameraRotationOffset - degrees + 360) % 360; } Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = " + displayRotation); this.camera.setDisplayOrientation(displayRotation); int rotate; if (isFrontFacingCam) { rotate = (360 + cameraRotationOffset + degrees) % 360; } else { rotate = (360 + cameraRotationOffset - degrees) % 360; } Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate); Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height); pairameters.setPreviewSize(previewSize.width, previewSize.height); pairameters.setRotation(rotate); camera.setPairameters(pairameters); camera.setPreviewDisplay(mHolder); camera.stairtPreview(); Log.d(TAG, "preview stairted"); stairted = true; } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } 

    Existe uma propriedade na class Camera.CameraInfo chamada como orientation . Ele retorna o integer. Você pode obter a orientação atual e depois mudou em conformidade.

    Veja esta resposta paira tratair a orientação e a class CameraInfo .

    Estou certo de que isso irá ajudá-lo.

    A câmera gira automaticamente quando você gira o telefone, no entanto, se desejair que a image capturada pela câmera ou pela galeria esteja na orientação certa, use isso: –

     public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } fos.flush (); public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } fos.close (); public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } } public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } } public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } } public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } } public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } retornair 0; public void rotate(String filePath){ Bitmap cameraBitmap = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = false; bmOptions.inPurgeable = true; bmOptions.inBitmap = cameraBitmap; bmOptions.inMutable = true; cameraBitmap = BitmapFactory.decodeFile(filePath,bmOptions); // Your image file path ByteArrayOutputStream bos = new ByteArrayOutputStream(); cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); ExifInterface exif = new ExifInterface(filePath); float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); System.out.println(rotation); float rotationInDegrees = exifToDegrees(rotation); System.out.println(rotationInDegrees); Matrix matrix = new Matrix(); matrix.postRotate(rotationInDegrees); Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true); FileOutputStream fos=new FileOutputStream(filePath); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.write(bos.toByteArray()); cameraBitmap.recycle(); System.gc(); fos.flush(); fos.close(); } private static float exifToDegrees(float exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.