Rk3568平台通过Jni读取摄像头当前状态值实践案例
本文要点:
可以通过native方式读取当前摄像头状态
示例,读取摄像头的亮度
目录
获取摄像头设备地址
增加native方法
头文件增加方法
增加native方法
可操作硬件指令示例
获取摄像头设备地址
grep '' /sys/class/video4linux/video*/name
增加native方法
//device = "/dev/video14"
public native int getVideoBrightness(String device);
头文件增加方法
JNIEXPORT jint JNICALL Java_com_a_b_getVideoBrightness(JNIEnv *env, jobject thiz, jstring dev_name);
增加native方法
JNIEXPORT jint JNICALL
Java_com_media_rtsp_HsRtspServer_getVideoBrightness(JNIEnv *env, jobject thiz, jstring dev_name) {
//定义v4l2 control 用于读取设备硬件信息
struct v4l2_control ctrl;
int fd = 0;
//device 的String name 转为jni可识别的格式
const char *dev_path = env->GetStringUTFChars(dev_name, nullptr);
if (dev_path == nullptr) {
LOGE("dev_name is nulllptr!");
return 0x00;
}
//打开设备,只读
if ((fd = open(dev_path, O_RDONLY)) < 0x00) {
LOGE("open error :%s", strerror(errno));
return 0x00;
}
//制定操作为查询亮度
ctrl.id = V4L2_CID_BRIGHTNESS;
/**
* VIDIOC_G_CTRL 该命令用于读取视频设备的某个控制项(Control)的当前值。例如:
亮度(Brightness)
对比度(Contrast)
饱和度(Saturation)
曝光(Exposure)
白平衡(White Balance)
其他设备支持的参数。
*
*
*/
ioctl(fd, VIDIOC_G_CTRL, &ctrl); //fd为/dev/video0的文件句柄
LOGI("Audio Brightness : %d\n", ctrl.value);
env->ReleaseStringUTFChars(dev_name, dev_path);
close(fd);
//返回摄像头亮度
return ctrl.value;
}
返回结果示例:5
可操作硬件指令示例
#define V4L2_CID_BRIGHTNESS (V4L2_CID_BASE + 0) // 相机亮度
#define V4L2_CID_CONTRAST (V4L2_CID_BASE + 1) // 相机对比度
#define V4L2_CID_SATURATION (V4L2_CID_BASE + 2) // 相机饱和度
#define V4L2_CID_HUE (V4L2_CID_BASE + 3) // 相机色相
#define V4L2_CID_AUDIO_VOLUME (V4L2_CID_BASE + 5)
#define V4L2_CID_AUDIO_BALANCE (V4L2_CID_BASE + 6)
#define V4L2_CID_AUDIO_BASS (V4L2_CID_BASE + 7)
#define V4L2_CID_AUDIO_TREBLE (V4L2_CID_BASE + 8)
#define V4L2_CID_AUDIO_MUTE (V4L2_CID_BASE + 9)
#define V4L2_CID_AUDIO_LOUDNESS (V4L2_CID_BASE + 10)
#define V4L2_CID_BLACK_LEVEL (V4L2_CID_BASE + 11)
#define V4L2_CID_AUTO_WHITE_BALANCE (V4L2_CID_BASE + 12) // 白平衡
#define V4L2_CID_DO_WHITE_BALANCE (V4L2_CID_BASE + 13)
#define V4L2_CID_RED_BALANCE (V4L2_CID_BASE + 14)
#define V4L2_CID_BLUE_BALANCE (V4L2_CID_BASE + 15)
#define V4L2_CID_GAMMA (V4L2_CID_BASE + 16)
#define V4L2_CID_WHITENESS (V4L2_CID_GAMMA)
#define V4L2_CID_EXPOSURE (V4L2_CID_BASE + 17)
#define V4L2_CID_AUTOGAIN (V4L2_CID_BASE + 18)
#define V4L2_CID_GAIN (V4L2_CID_BASE + 19)
#define V4L2_CID_HFLIP (V4L2_CID_BASE + 20)
#define V4L2_CID_VFLIP (V4L2_CID_BASE + 21)
#define V4L2_CID_POWER_LINE_FREQUENCY (V4L2_CID_BASE + 24)