ImageFormat.java revision 525af5d5b0fcd4de9bac2f4be016cdd2f3534a40
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19public class ImageFormat {
20    /*
21     * these constants are chosen to be binary compatible with their previous
22     * location in PixelFormat.java
23     */
24
25    public static final int UNKNOWN = 0;
26
27    /**
28     * RGB format used for pictures encoded as RGB_565 see
29     * {@link android.hardware.Camera.Parameters#setPictureFormat(int)}.
30     */
31    public static final int RGB_565 = 4;
32
33    /**
34     * Planar 4:2:0 YCrCb format. This format assumes an horizontal stride of 16
35     * pixels for all planes and an implicit vertical stride of the image
36     * height's next multiple of two.
37     *   y_size = stride * ALIGN(height, 2)
38     *   c_size = ALIGN(stride/2, 16) * height
39     *   size = y_size + c_size * 2
40     *   cr_offset = y_size
41     *   cb_offset = y_size + c_size
42     *
43     * Whether this format is supported by the camera hardware can be determined
44     * by
45     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
46     */
47    public static final int YV12 = 0x32315659;
48
49    /**
50     * YCbCr format, used for video. Whether this format is supported by the
51     * camera hardware can be determined by
52     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
53     */
54    public static final int NV16 = 0x10;
55
56    /**
57     * YCrCb format used for images, which uses the NV21 encoding format. This
58     * is the default format for camera preview images, when not otherwise set
59     * with {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}.
60     */
61    public static final int NV21 = 0x11;
62
63    /**
64     * YCbCr format used for images, which uses YUYV (YUY2) encoding format.
65     * This is an alternative format for camera preview images. Whether this
66     * format is supported by the camera hardware can be determined by
67     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
68     */
69    public static final int YUY2 = 0x14;
70
71    /**
72     * Encoded formats. These are not necessarily supported by the hardware.
73     */
74    public static final int JPEG = 0x100;
75
76    /**
77     * Use this function to retrieve the number of bits per pixel of an
78     * ImageFormat.
79     *
80     * @param format
81     * @return the number of bits per pixel of the given format or -1 if the
82     *         format doesn't exist or is not supported.
83     */
84    public static int getBitsPerPixel(int format) {
85        switch (format) {
86            case RGB_565:
87                return 16;
88            case NV16:
89                return 16;
90            case YUY2:
91                return 16;
92            case YV12:
93                return 12;
94            case NV21:
95                return 12;
96        }
97        return -1;
98    }
99}
100