对安卓里CamcorderProfile类的学习

用法

手机支持的最低分辨率:
1.直接获取
CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
2.按时间流逝,质量变化情况来
CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_LOW);
3.按高帧数下的质量来
CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH_SPEED_LOW);
手机支持的最高分辨率:
1.直接获取
CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
2.按时间流逝,质量变化情况来
CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_HIGH);
3.按高帧数下的质量来
CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH_SPEED_HIGH);

以下是个人对这个类的理解和猜测了,废话多,不喜勿看

CamcorderProfile类是什么

同类名一样,这个类是保存摄像机配置信息的一个实体类,准确说是保存音视频配置信息
下面看源码里对它的说明:
/**
 * Retrieves the
 * predefined camcorder profile settings for camcorder applications.
 * These settings are read-only.
 *
 * 

The compressed output from a recording session with a given * CamcorderProfile contains two tracks: one for audio and one for video. * *

Each profile specifies the following set of parameters: *

    *
  • The file output format *
  • Video codec format *
  • Video bit rate in bits per second *
  • Video frame rate in frames per second *
  • Video frame width and height, *
  • Audio codec format *
  • Audio bit rate in bits per second, *
  • Audio sample rate *
  • Number of audio channels for recording. *
*/
意思呢就是手机里已经有预先定义好的配置信息,你可以直接拿来用(当然,也可以自己设置)。
预先定义好的配文件里指定了这些信息(顺序同上面的说明):
  *
  • 文件输出格式
      *
  • 视频编解码格式
      *
  • 视频比特率,以位/秒为单位
      *
  • 视频帧速率(以每秒帧数为单位)
      *
  • 视频帧宽和高度,
      *
  • 音频编解码格式
      *
  • 音频比特率,以位/秒为单位,
      *
  • 音频采样率
      *
  • 录制的音频通道数。

  • 所以CamcorderProfile里含有以下变量:
        public int fileFormat;
        public int videoCodec;
        public int videoBitRate;
        public int videoFrameRate;
        public int videoFrameWidth;
        public int videoFrameHeight;
        public int audioCodec;
        public int audioBitRate;
        public int audioSampleRate;
        public int audioChannels
    当然,以上是预先定义好的配文件里指定了的属性,未指定的属性还有:
        public int duration;//在会话终止之前的默认记录持续时间(以秒为单位),不知道可不可以理解为录像的最大持续时间
        public int quality;//分辨率

    已经有了预先定义好的配置文件,也可以自己定制,那么在这里不妨猜想下得到一个CamcorderProfile实例的方式

    CamcorderProfile实例的获取方式

    1.构造函数

     private CamcorderProfile(int duration,
                                 int quality,
                                 int fileFormat,
                                 int videoCodec,
                                 int videoBitRate,
                                 int videoFrameRate,
                                 int videoWidth,
                                 int videoHeight,
                                 int audioCodec,
                                 int audioBitRate,
                                 int audioSampleRate,
                                 int audioChannels) {
    
            this.duration         = duration;
            this.quality          = quality;
            this.fileFormat       = fileFormat;
            this.videoCodec       = videoCodec;
            this.videoBitRate     = videoBitRate;
            this.videoFrameRate   = videoFrameRate;
            this.videoFrameWidth  = videoWidth;
            this.videoFrameHeight = videoHeight;
            this.audioCodec       = audioCodec;
            this.audioBitRate     = audioBitRate;
            this.audioSampleRate  = audioSampleRate;
            this.audioChannels    = audioChannels;
        }
    这就是自己设置属性了,虽然是private方法,但是可以用反射啊。
    也可以得到一个实例对象,直接修改里面的值,里面的属性变量都是public嘛

    2.两个get方法

    第一个函数只需传入一个int型的分辨率变量
    在函数里面将遍历所有的Camera对象(摄像头),然后找到哪个是正在使用的,然后根据你传入的分辨率返回相应的那个摄像头已经预先设置的配置信息
    
    
    public static CamcorderProfile get(int quality) {
            int numberOfCameras = Camera.getNumberOfCameras();
            CameraInfo cameraInfo = new CameraInfo();
            for (int i = 0; i < numberOfCameras; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                    return get(i, quality);
                }
            }
            return null;
        }

    第二个函数需传入一个cameraID和分辨率,对你传进来的分辨率变量进行大小判定,只要没超过大小边界就返回调用方法放回

    一个实例对象,否则报错

    
    
    public static CamcorderProfile get(int cameraId, int quality) {
            if (!((quality >= QUALITY_LIST_START &&
                   quality <= QUALITY_LIST_END) ||
                  (quality >= QUALITY_TIME_LAPSE_LIST_START &&
                   quality <= QUALITY_TIME_LAPSE_LIST_END) ||
                   (quality >= QUALITY_HIGH_SPEED_LIST_START &&
                   quality <= QUALITY_HIGH_SPEED_LIST_END))) {
                String errMessage = "Unsupported quality level: " + quality;
                throw new IllegalArgumentException(errMessage);
            }
            return native_get_camcorder_profile(cameraId, quality);
        }
    那么,大小边界是什么呢?静态方法里调用的类变量只能是静态变量。静态变量,对了,这个类里面有没有已经定义好的属性
    然后对外提供呢?答案是有的

    对外提供的属性

        public static final int QUALITY_LOW  = 0;
        public static final int QUALITY_HIGH = 1;
        public static final int QUALITY_QCIF = 2;
        public static final int QUALITY_CIF = 3;
        public static final int QUALITY_480P = 4;
        public static final int QUALITY_720P = 5;
        public static final int QUALITY_1080P = 6;
        public static final int QUALITY_QVGA = 7;
        public static final int QUALITY_2160P = 8;
        // Start and end of quality list
        private static final int QUALITY_LIST_START = QUALITY_LOW;
        private static final int QUALITY_LIST_END = QUALITY_2160P;
    
        public static final int QUALITY_TIME_LAPSE_LOW  = 1000;
        public static final int QUALITY_TIME_LAPSE_HIGH = 1001;
        public static final int QUALITY_TIME_LAPSE_QCIF = 1002;
        public static final int QUALITY_TIME_LAPSE_CIF = 1003;
        public static final int QUALITY_TIME_LAPSE_480P = 1004;
        public static final int QUALITY_TIME_LAPSE_720P = 1005;
        public static final int QUALITY_TIME_LAPSE_1080P = 1006;
        public static final int QUALITY_TIME_LAPSE_QVGA = 1007;
        public static final int QUALITY_TIME_LAPSE_2160P = 1008;
    
        // Start and end of timelapse quality list
        private static final int QUALITY_TIME_LAPSE_LIST_START = QUALITY_TIME_LAPSE_LOW;
        private static final int QUALITY_TIME_LAPSE_LIST_END = QUALITY_TIME_LAPSE_2160P;
    
       
        public static final int QUALITY_HIGH_SPEED_LOW = 2000;
        public static final int QUALITY_HIGH_SPEED_HIGH = 2001;
        public static final int QUALITY_HIGH_SPEED_480P = 2002;
        public static final int QUALITY_HIGH_SPEED_720P = 2003;
        public static final int QUALITY_HIGH_SPEED_1080P = 2004;
        public static final int QUALITY_HIGH_SPEED_2160P = 2005;
    
        // Start and end of high speed quality list
        private static final int QUALITY_HIGH_SPEED_LIST_START = QUALITY_HIGH_SPEED_LOW;
        private static final int QUALITY_HIGH_SPEED_LIST_END = QUALITY_HIGH_SPEED_2160P;
    哇,原来上面的大小是边界是这个意思啊,原来开头的三种不同的三种获取方式是这样的,当然最常见的还是第一种直接获取
    的方式——!~。

    类里面的其他方法

    
    
    boolean hasProfile(int quality) //当前的摄像头是否有此分辨率下的配置文件(即是否支持此分辨率)
    boolean hasProfile(int cameraId, int quality) //指定的摄像头是否有此分辨率下的配置文件(即是否支持此分辨率)
    及获取实例的原生方法
    static {
            System.loadLibrary("media_jni");
            native_init();
        }
     private static native final void native_init();
        private static native final CamcorderProfile native_get_camcorder_profile(
                int cameraId, int quality);
        private static native final boolean native_has_camcorder_profile(
                int cameraId, int quality);有兴趣的可以继续深入学习
    //最后不妨猜测下,底层的运行方式:
    //以一个800W像素和1300W像素的摄像头为例,不妨设对480P、720P、1080P、2160P的支持
    //分别为:true/true/false/false,true/true/true/false,当对1300W的摄像头请求2160P支持时,hasProfile(2160P)
    //,发现并没有配置文件返回false。但是如果get(2160P)的话,发现没有这么高分辨的配置文件,所以向下兼容,会返回
    //的1080P的配置文件,始终会返回一个最大的值
    //以上只列出了大部分的信息,还有些许不常见的注意事项请看源码。
    //另外Camera类已经被@Deprecated,受到影响不知道谷歌以后会不会修改CamcorderProfile这个类
    
    

    你可能感兴趣的:(安卓源码)