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