title: What is the fastest way to compress image to a specified size?
body: <p>In my project, I need to upload some images(1-10) to server sometimes. The size varies from 1M to 10M. Before upload,each image should be compressed until size&lt;512KB. I do the compression like this:</p>
  
  <pre><code>public static byte[] compressImageA(Bitmap image, int maxSize) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      int options = 100;
      image.compress(Bitmap.CompressFormat.JPEG, options, baos);
      while (baos.size() / 1024 &gt; maxSize) {
          baos.reset();
          options -= 10;
          image.compress(Bitmap.CompressFormat.JPEG, options, baos);
      }
      if (image != null &amp;&amp; !image.isRecycled()) {
          try {
              image.recycle();
          } catch (Exception e) {
          }
      }
      return baos.toByteArray();
  }
  </code></pre>
  
  <p>This method can make it, but not fast enough.For example,it takes about 25 seconds to compress 10 images.(Size of each is between 1M and 10M).After some tests,I find <code>Bitmap.compress()</code> may be called many times and takes the most time during a compression.So what can I do to make it faster? I want <code>Bitmap.compress()</code> to be called only 1 time during each compression.Or is there any other way to compress image to a specified size more faster？</p>