@@ -469,6 +469,11 @@ public interface PreviewCallback
469469 * Called as preview frames are displayed. This callback is invoked
470470 * on the event thread {@link #open(int)} was called from.
471471 *
472+ * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
473+ * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
474+ * for the arrangement of the pixel data in the preview callback
475+ * buffers.
476+ *
472477 * @param data the contents of the preview frame in the format defined
473478 * by {@link android.graphics.ImageFormat}, which can be queried
474479 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
@@ -609,12 +614,17 @@ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
609614 * the frame is discarded. Applications should add buffers back when they
610615 * finish processing the data in them.
611616 *
612- * <p>The size of the buffer is determined by multiplying the preview
613- * image width, height, and bytes per pixel. The width and height can be
614- * read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel
615- * can be computed from
616- * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
617- * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
617+ * <p>For formats besides YV12, the size of the buffer is determined by
618+ * multiplying the preview image width, height, and bytes per pixel. The
619+ * width and height can be read from
620+ * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
621+ * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
622+ * 8, using the image format from
623+ * {@link Camera.Parameters#getPreviewFormat()}.
624+ *
625+ * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
626+ * size can be calculated using the equations listed in
627+ * {@link Camera.Parameters#setPreviewFormat}.
618628 *
619629 * <p>This method is only necessary when
620630 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
@@ -624,8 +634,8 @@ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
624634 * hold the preview frame data, preview callback will return null and
625635 * the buffer will be removed from the buffer queue.
626636 *
627- * @param callbackBuffer the buffer to add to the queue.
628- * The size should be width * height * bits_per_pixel / 8 .
637+ * @param callbackBuffer the buffer to add to the queue. The size of the
638+ * buffer must match the values described above .
629639 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
630640 */
631641 public final void addCallbackBuffer (byte [] callbackBuffer )
@@ -2270,12 +2280,44 @@ public List<int[]> getSupportedPreviewFpsRange() {
22702280 * {@link android.graphics.ImageFormat#NV21}, which
22712281 * uses the NV21 encoding format.</p>
22722282 *
2273- * @param pixel_format the desired preview picture format, defined
2274- * by one of the {@link android.graphics.ImageFormat} constants.
2275- * (E.g., <var>ImageFormat.NV21</var> (default),
2276- * <var>ImageFormat.RGB_565</var>, or
2277- * <var>ImageFormat.JPEG</var>)
2283+ * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
2284+ * the available preview formats.
2285+ *
2286+ * <p>It is strongly recommended that either
2287+ * {@link android.graphics.ImageFormat#NV21} or
2288+ * {@link android.graphics.ImageFormat#YV12} is used, since
2289+ * they are supported by all camera devices.</p>
2290+ *
2291+ * <p>For YV12, the image buffer that is received is not necessarily
2292+ * tightly packed, as there may be padding at the end of each row of
2293+ * pixel data, as described in
2294+ * {@link android.graphics.ImageFormat#YV12}. For camera callback data,
2295+ * it can be assumed that the stride of the Y and UV data is the
2296+ * smallest possible that meets the alignment requirements. That is, if
2297+ * the preview size is <var>width x height</var>, then the following
2298+ * equations describe the buffer index for the beginning of row
2299+ * <var>y</var> for the Y plane and row <var>c</var> for the U and V
2300+ * planes:
2301+ *
2302+ * {@code
2303+ * <pre>
2304+ * yStride = (int) ceil(width / 16.0) * 16;
2305+ * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
2306+ * ySize = yStride * height;
2307+ * uvSize = uvStride * height / 2;
2308+ * yRowIndex = yStride * y;
2309+ * uRowIndex = ySize + uvSize + uvStride * c;
2310+ * vRowIndex = ySize + uvStride * c;
2311+ * size = ySize + uvSize * 2;</pre>
2312+ * }
2313+ *
2314+ * @param pixel_format the desired preview picture format, defined by
2315+ * one of the {@link android.graphics.ImageFormat} constants. (E.g.,
2316+ * <var>ImageFormat.NV21</var> (default), or
2317+ * <var>ImageFormat.YV12</var>)
2318+ *
22782319 * @see android.graphics.ImageFormat
2320+ * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
22792321 */
22802322 public void setPreviewFormat (int pixel_format ) {
22812323 String s = cameraFormatForPixelFormat (pixel_format );
@@ -2293,6 +2335,7 @@ public void setPreviewFormat(int pixel_format) {
22932335 *
22942336 * @return the preview format.
22952337 * @see android.graphics.ImageFormat
2338+ * @see #setPreviewFormat
22962339 */
22972340 public int getPreviewFormat () {
22982341 return pixelFormatForCameraFormat (get (KEY_PREVIEW_FORMAT ));
@@ -2306,6 +2349,7 @@ public int getPreviewFormat() {
23062349 * @return a list of supported preview formats. This method will always
23072350 * return a list with at least one element.
23082351 * @see android.graphics.ImageFormat
2352+ * @see #setPreviewFormat
23092353 */
23102354 public List <Integer > getSupportedPreviewFormats () {
23112355 String str = get (KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX );
0 commit comments