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