@@ -471,6 +471,11 @@ public interface PreviewCallback
471471 * Called as preview frames are displayed. This callback is invoked
472472 * on the event thread {@link #open(int)} was called from.
473473 *
474+ * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
475+ * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
476+ * for the arrangement of the pixel data in the preview callback
477+ * buffers.
478+ *
474479 * @param data the contents of the preview frame in the format defined
475480 * by {@link android.graphics.ImageFormat}, which can be queried
476481 * with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
@@ -611,12 +616,17 @@ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
611616 * the frame is discarded. Applications should add buffers back when they
612617 * finish processing the data in them.
613618 *
614- * <p>The size of the buffer is determined by multiplying the preview
615- * image width, height, and bytes per pixel. The width and height can be
616- * read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel
617- * can be computed from
618- * {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
619- * using the image format from {@link Camera.Parameters#getPreviewFormat()}.
619+ * <p>For formats besides YV12, the size of the buffer is determined by
620+ * multiplying the preview image width, height, and bytes per pixel. The
621+ * width and height can be read from
622+ * {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel can be
623+ * computed from {@link android.graphics.ImageFormat#getBitsPerPixel(int)} /
624+ * 8, using the image format from
625+ * {@link Camera.Parameters#getPreviewFormat()}.
626+ *
627+ * <p>If using the {@link android.graphics.ImageFormat#YV12} format, the
628+ * size can be calculated using the equations listed in
629+ * {@link Camera.Parameters#setPreviewFormat}.
620630 *
621631 * <p>This method is only necessary when
622632 * {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
@@ -626,8 +636,8 @@ public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
626636 * hold the preview frame data, preview callback will return null and
627637 * the buffer will be removed from the buffer queue.
628638 *
629- * @param callbackBuffer the buffer to add to the queue.
630- * The size should be width * height * bits_per_pixel / 8 .
639+ * @param callbackBuffer the buffer to add to the queue. The size of the
640+ * buffer must match the values described above .
631641 * @see #setPreviewCallbackWithBuffer(PreviewCallback)
632642 */
633643 public final void addCallbackBuffer (byte [] callbackBuffer )
@@ -2289,12 +2299,44 @@ public List<int[]> getSupportedPreviewFpsRange() {
22892299 * {@link android.graphics.ImageFormat#NV21}, which
22902300 * uses the NV21 encoding format.</p>
22912301 *
2292- * @param pixel_format the desired preview picture format, defined
2293- * by one of the {@link android.graphics.ImageFormat} constants.
2294- * (E.g., <var>ImageFormat.NV21</var> (default),
2295- * <var>ImageFormat.RGB_565</var>, or
2296- * <var>ImageFormat.JPEG</var>)
2302+ * <p>Use {@link Parameters#getSupportedPreviewFormats} to get a list of
2303+ * the available preview formats.
2304+ *
2305+ * <p>It is strongly recommended that either
2306+ * {@link android.graphics.ImageFormat#NV21} or
2307+ * {@link android.graphics.ImageFormat#YV12} is used, since
2308+ * they are supported by all camera devices.</p>
2309+ *
2310+ * <p>For YV12, the image buffer that is received is not necessarily
2311+ * tightly packed, as there may be padding at the end of each row of
2312+ * pixel data, as described in
2313+ * {@link android.graphics.ImageFormat#YV12}. For camera callback data,
2314+ * it can be assumed that the stride of the Y and UV data is the
2315+ * smallest possible that meets the alignment requirements. That is, if
2316+ * the preview size is <var>width x height</var>, then the following
2317+ * equations describe the buffer index for the beginning of row
2318+ * <var>y</var> for the Y plane and row <var>c</var> for the U and V
2319+ * planes:
2320+ *
2321+ * {@code
2322+ * <pre>
2323+ * yStride = (int) ceil(width / 16.0) * 16;
2324+ * uvStride = (int) ceil( (yStride / 2) / 16.0) * 16;
2325+ * ySize = yStride * height;
2326+ * uvSize = uvStride * height / 2;
2327+ * yRowIndex = yStride * y;
2328+ * uRowIndex = ySize + uvSize + uvStride * c;
2329+ * vRowIndex = ySize + uvStride * c;
2330+ * size = ySize + uvSize * 2;</pre>
2331+ * }
2332+ *
2333+ * @param pixel_format the desired preview picture format, defined by
2334+ * one of the {@link android.graphics.ImageFormat} constants. (E.g.,
2335+ * <var>ImageFormat.NV21</var> (default), or
2336+ * <var>ImageFormat.YV12</var>)
2337+ *
22972338 * @see android.graphics.ImageFormat
2339+ * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
22982340 */
22992341 public void setPreviewFormat (int pixel_format ) {
23002342 String s = cameraFormatForPixelFormat (pixel_format );
@@ -2312,6 +2354,7 @@ public void setPreviewFormat(int pixel_format) {
23122354 *
23132355 * @return the preview format.
23142356 * @see android.graphics.ImageFormat
2357+ * @see #setPreviewFormat
23152358 */
23162359 public int getPreviewFormat () {
23172360 return pixelFormatForCameraFormat (get (KEY_PREVIEW_FORMAT ));
@@ -2325,6 +2368,7 @@ public int getPreviewFormat() {
23252368 * @return a list of supported preview formats. This method will always
23262369 * return a list with at least one element.
23272370 * @see android.graphics.ImageFormat
2371+ * @see #setPreviewFormat
23282372 */
23292373 public List <Integer > getSupportedPreviewFormats () {
23302374 String str = get (KEY_PREVIEW_FORMAT + SUPPORTED_VALUES_SUFFIX );
0 commit comments