There is the following code for processing photos, but all my attempts to make the correct orientation are in vain. Tell me, what could be the error?

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera; mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { Log.d("", "Error setting camera preview: " + e.getMessage()); } } public void surfaceDestroyed(SurfaceHolder holder) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (mHolder.getSurface() == null) return; mCamera.stopPreview(); setCameraDisplayOrientation(); try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e) { Log.d("", "Error starting camera preview: " + e.getMessage()); } } public void setCameraDisplayOrientation() { if (mCamera == null) { Log.d("CAMERA", "setCameraDisplayOrientation - camera null"); return; } Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(0, info); WindowManager winManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int rotation = winManager.getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } mCamera.setDisplayOrientation(result); Camera.Parameters parameters = mCamera.getParameters(); int rotate = (degrees + 270) % 360; parameters.setRotation(rotate); mCamera.setParameters(parameters); } 

}

tried to use exif, but the result got worse

 public void rotateBitmap(File tempFile) { if (tempFile != null) { BitmapFactory.Options opts = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeFile(tempFile.getAbsolutePath(), opts); ExifInterface exif = null; try { exif = new ExifInterface(tempFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); Log.d("TAG", "Exif: " + orientation); exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(0)); Matrix matrix = new Matrix(); switch (orientation) { case ExifInterface.ORIENTATION_NORMAL: matrix.postRotate(0); break; case ExifInterface.ORIENTATION_ROTATE_90: matrix.postRotate(90); break; case ExifInterface.ORIENTATION_ROTATE_180: matrix.postRotate(180); break; case ExifInterface.ORIENTATION_ROTATE_270: matrix.postRotate(270); Log.d("TAG", "Exif: " + orientation); break; default: break; } Bitmap destBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); try { OutputStream outputStream = new FileOutputStream(tempFile); destBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } 

orientation is always = 8, and the image goes upside down ...

0