title: Android: Display camera still capture on TextureView Quickly?
body: <p>I'm using the Android Camera2 API to take still capture images and displaying them on a TextureView (for later image editing).</p>
  
  <p>I have been scouring the web for a faster method to:</p>
  
  <ol>
  <li>Decode Camera Image buffer into bitmap</li>
  <li>Scale bitmap to size of screen and rotate it (since it comes in rotated 90 degrees)</li>
  <li>Display it on a texture view</li>
  </ol>
  
  <p>Currently I've managed an execution time of around 0.8s for the above, but this is too long for my particular application.</p>
  
  <p>A few solutions I've considered were:</p>
  
  <ol>
  <li>Simply taking a single frame of the preview (timing-wise this was fast, except that I had no control over auto flash)</li>
  <li>Trying to get instead a YUV_420_888 formatted image and then somehow turning that into a bitmap (there's a lot of stuff online that might help but my initial attempts bore no fruit as of yet)</li>
  <li>Simply sending a reduced quality image from the camera itself, but from what I've read it looks like the <a href="http://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html#JPEG_QUALITY" rel="nofollow" title="JPEG_QUALITY">JPEG_QUALITY</a> parameter in CaptureRequests does nothing! I've also tried setting BitmapFactory options <a href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize" rel="nofollow" title="inSampleSize">inSampleSize</a> but without any noticeable improvement in speed.</li>
  <li>Finding some way to directly manipulate the jpeg byte array from image buffer to transform it and then converting to bitmap, all in one shot</li>
  </ol>
  
  <p>For your reference, the following code takes the image buffer, decodes and transforms it, and displays it on the textureview:</p>
  
  <pre><code>Canvas canvas = mTextureView.lockCanvas();
  // obtain image bytes (jpeg) from image in camera fragment
  // mFragment.getImage() returns Image object
  ByteBuffer buffer = mFragment.getImage().getPlanes()[0].getBuffer();
  byte[] bytes = new byte[buffer.remaining()];
  buffer.get(bytes);
  // decoding process takes several hundred milliseconds
  Bitmap src = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  mFragment.getImage().close();
  
  // resize horizontally oriented images
  if (src.getWidth() &gt; src.getHeight()) {
      // transformation matrix that scales and rotates
      Matrix matrix = new Matrix();
      if (CameraLayout.getFace() == CameraCharacteristics.LENS_FACING_FRONT) {
          matrix.setScale(-1, 1);
      }
      matrix.postRotate(90);
      matrix.postScale(((float) canvas.getWidth()) / src.getHeight(),
          ((float) canvas.getHeight()) / src.getWidth());
      // bitmap creation process takes another several hundred millis!
      Bitmap resizedBitmap = Bitmap.createBitmap(
          src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
      canvas.drawBitmap(resizedBitmap, 0, 0, null);
      } else {
          canvas.drawBitmap(src, 0, 0, null);
  }
  // post canvas to texture view
  mTextureView.unlockCanvasAndPost(canvas);
  </code></pre>
  
  <p>This is my first question on stack overflow, so I apologize if I haven't quite followed common conventions.
  Thanks in advance for any feedback.</p>