title: How to rotate a JPEG file on Android without losing quality and gaining file size?
body: <h2>Background</h2>
  
  <p>I need to rotate images taken by the camera so that they will always have a normal orientation.</p>
  
  <p>for this, I use the next code (used <a href="https://stackoverflow.com/a/11081918/878126"><strong>this post</strong></a> to get the image orientation)</p>
  
  <pre><code>//&lt;= get the angle of the image , and decode the image from the file
  final Matrix matrix = new Matrix();
  //&lt;= prepare the matrix based on the EXIF data (based on https://gist.github.com/9re/1990019 )
  final Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,false);
  bitmap.recycle();
  fileOutputStream = new FileOutputStream(tempFilePath);
  rotatedBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
  rotatedBitmap.recycle();
  </code></pre>
  
  <p>here the compression rate (AKA "quality" parameter) is 100.</p>
  
  <h2>The problem</h2>
  
  <p>The code works fine, but the result is larger than the original, much much larger.</p>
  
  <p>The original file is around 600-700 KB, while the resulting file is around 3MB ...</p>
  
  <p>This is even though both the input file and the output file are of the same format (JPEG).</p>
  
  <p>The camera settings are at "super fine" quality. not sure what it means, but I think it has something to do with the compression ratio.</p>
  
  <h2>What I've tried</h2>
  
  <p>I've tried to set the "filter" parameter to either false or true. both resulted in large files. </p>
  
  <p>Even without the rotation itself (just decode and encode), I get much larger files sizes...</p>
  
  <p>Only when I've set compression ratio to around 85, I get similar files sizes, but I wonder how the quality is affected compared to the original files.</p>
  
  <h2>The question</h2>
  
  <p>Why does it occur?</p>
  
  <p>Is there a way to get the exact same size and quality of the input file ? </p>
  
  <p>Will using the same compression rate as the original file make it happen? Is it even possible to get the compression rate of the original file? </p>
  
  <p>What does it mean to have a 100% compression rate ? </p>
  
  <hr>
  
  <p>EDIT: I've found <a href="https://stackoverflow.com/questions/543402/can-a-jpeg-compressed-image-be-rotated-without-a-loss-in-quality"><strong>this link</strong></a> talking about rotation of JPEG files without losing the quality and file size , but is there a solution for it on Android ? </p>
  
  <p><a href="http://graphicssoft.about.com/cs/digitalimaging/f/rotatequality.htm" rel="nofollow noreferrer"><strong>Here's another link</strong></a> that says it's possible, but I couldn't find any library that allows rotation of jpeg files without losing their quality</p>