title: Compressing a JPG image to a given size in KB?
body: <p>I have a byte array returned by the camera in input.</p>
  
  <p>I need to scale down my image to make its size equal to 500KB. I am trying to achieve this using a Bitmap, but I cannot find how to get the proper <code>compression</code> value.</p>
  
  <pre><code>public static byte[] compressCapture(byte[] capture) {
  
      // How to get the right compression value ?
      int compression = 50;
  
      Bitmap bitmap  = BitmapFactory.decodeByteArray(capture, 0, capture.length);
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.JPEG, compression, outputStream);
      return outputStream.toByteArray();
  }
  </code></pre>
  
  <p>The problem is that the compression is not proportional to the image size, meaning that <code>compression</code> equals to 50 will not divide the image size by 2.</p>
  
  <p>Is there a way to get a fixed image size in output that does not depend on the focus, or the smartphone model ?</p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>I do not want to use a File (I work with confidential data).</p>