title: How to obtain correct dimension from Camera2 and ImageReader?
body: <p>In my Android application, I use Camera2 API to allow the user to take a snapshot. The code mostly is straight out of standard Camera2 sample application.</p>
  
  <p>When the image is available, it is obtained by calling <code>acquireNextImage</code> method:</p>
  
  <pre><code> public void onImageAvailable(ImageReader reader) {
       mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
   }
  </code></pre>
  
  <p>In my case, when I obtain the width and height of the <code>Image</code> object, it reports it as 4160x3120. However, in reality, it is 3120x4160. The real size can be seen when I dump the buffer into a jpg file:</p>
  
  <pre><code> ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
   byte[] bytes = new byte[buffer.remaining()];
   buffer.get(bytes);
   // Dump "bytes" to file
  </code></pre>
  
  <p>For my purpose, I need the correct width and height. Wondering if the width and height are getting swapped because the sensor orientation is 90 degrees.</p>
  
  <p>If so, I can simply swap the dimensions if I detect that the sensor orientation is 90 or 270. I already have this value:</p>
  
  <pre><code>  mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
  </code></pre>
  
  <p>I feel this is a general problem and not specific to my code. Regards.</p>
  
  <p>Edit:</p>
  
  <p>Turns out the image size reported is correct. Jpeg image metadata stores a field called "Orientation." Most image viewers know how to interpret this value.</p>