CameraProfile.java revision 09b9005769f2b717f637131578ce6cfa6bd62bd9
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.media;
18
19import java.util.Arrays;
20import java.util.HashMap;
21
22/**
23 * The CameraProfile class is used to retrieve the pre-defined still image
24 * capture (jpeg) quality levels (0-100) used for low, medium, and high
25 * quality settings in the Camera application.
26 *
27 */
28public class CameraProfile
29{
30    /**
31     * Define three quality levels for JPEG image encoding.
32     */
33    /*
34     * Don't change the values for these constants unless getImageEncodingQualityLevels()
35     * method is also changed accordingly.
36     */
37    public static final int QUALITY_LOW    = 0;
38    public static final int QUALITY_MEDIUM = 1;
39    public static final int QUALITY_HIGH   = 2;
40
41    /*
42     * Cache the Jpeg encoding quality parameters
43     */
44    private static final HashMap<Integer, int[]> sCache = new HashMap<Integer, int[]>();
45
46    /**
47     * Returns a pre-defined still image capture (jpeg) quality level
48     * used for the given quality level in the Camera application for
49     * the default camera.
50     *
51     * @param quality The target quality level
52     */
53    public static int getJpegEncodingQualityParameter(int quality) {
54        return getJpegEncodingQualityParameter(0, quality);
55    }
56
57    /**
58     * Returns a pre-defined still image capture (jpeg) quality level
59     * used for the given quality level in the Camera application for
60     * the specified camera.
61     *
62     * @param cameraId The id of the camera
63     * @param quality The target quality level
64     */
65    public static int getJpegEncodingQualityParameter(int cameraId, int quality) {
66        if (quality < QUALITY_LOW || quality > QUALITY_HIGH) {
67            throw new IllegalArgumentException("Unsupported quality level: " + quality);
68        }
69        synchronized (sCache) {
70            int[] levels = sCache.get(cameraId);
71            if (levels == null) {
72                levels = getImageEncodingQualityLevels(cameraId);
73                sCache.put(cameraId, levels);
74            }
75            return levels[quality];
76        }
77    }
78
79    static {
80        System.loadLibrary("media_jni");
81        native_init();
82    }
83
84    private static int[] getImageEncodingQualityLevels(int cameraId) {
85        int nLevels = native_get_num_image_encoding_quality_levels(cameraId);
86        if (nLevels != QUALITY_HIGH + 1) {
87            throw new RuntimeException("Unexpected Jpeg encoding quality levels " + nLevels);
88        }
89
90        int[] levels = new int[nLevels];
91        for (int i = 0; i < nLevels; ++i) {
92            levels[i] = native_get_image_encoding_quality_level(cameraId, i);
93        }
94        Arrays.sort(levels);  // Lower quality level ALWAYS comes before higher one
95        return levels;
96    }
97
98    // Methods implemented by JNI
99    private static native final void native_init();
100    private static native final int native_get_num_image_encoding_quality_levels(int cameraId);
101    private static native final int native_get_image_encoding_quality_level(int cameraId, int index);
102}
103