ndk实例总结补充:使用libuvc采集usb图像分析

2024-01-13 00:38

本文主要是介绍ndk实例总结补充:使用libuvc采集usb图像分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

版权声明:本文为CSDN博主「提辖鲁」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lj402159806/article/details/109614095

 

ndk实例总结补充
ndk实例总结:jni实例
ndk实例总结:opencv图像处理
ndk实例总结:安卓Camera与usbCamera原始图像处理
ndk实例总结补充:使用V4L2采集usb图像分析
ndk实例总结:使用fmpeg播放rtsp流
ndk实例总结:基于libuvc的双usbCamera处理
ndk实例总结补充:使用libuvc采集usb图像分析
ndk实例总结:jni日志存储

前言
本文是对ndk实例总结:基于libuvc的双usbCamera处理中jni部分的补充,主要分析下使用libuvc采集usb图像的流程

libuvc介绍
libuvc是用于USB视频设备的跨平台库,支持USB Video Class(UVC)设备(例如用户网络摄像头)的枚举,控制和流传输

有以下特性:

UVC设备发现和管理API
具有异步/回调和同步/轮询模式的视频流(从设备到主机)
对标准设备设置的读/写访问
各种格式之间的转换:RGB,YUV,JPEG等
代码实现分析
这里分析下ndk实例总结:基于libuvc的双usbCamera处理中的jni代码,主要是摄像头的打开关闭,预览与拍照,以及更改摄像头参数等

初始化摄像头
首先是初始化摄像头

int connect(int index, int vid, int pid, int fd, int busNum, int devAddr, std::string c_usbfs) {
    uvc_error_t res = uvc_init2(&ctx[index], NULL, c_usbfs.c_str());

    if (res < 0) {
        LOGE("connect uvc_init2 error, res %d, index %d", res, index);
        return JNI_ERR;
    }
    LOGI("connect UVC initialized, index %d", index);

    res = uvc_get_device_with_fd(ctx[index], &dev[index], vid, pid, NULL, fd, busNum, devAddr);

    if (res < 0) {
        LOGE("connect uvc_get_device_with_fd error, res %d , index %d", res, index);
        //        close(fd);
        return JNI_ERR;
    } else {
        LOGI("connect Device found, index %d", index);
        res = uvc_open(dev[index], &devh[index]);
        if (res < 0) {
            LOGE("connect uvc_open error, res %d, index %d", res, index);
            uvc_unref_device(dev[index]);
            dev[index] = nullptr;
            devh[index] = nullptr;
            //            close(fd);
            ::fd[index] = 0;
            return JNI_ERR;
        } else {
            LOGI("connect Device opened, index %d", index);
        }
    }

    if (res == 0) {
        ::fd[index] = fd;
        is_connect[index] = true;
    }

    res = start_stream(index);

    LOGI("connect, res %d, index %d", res, index);

    return res;
}

初始化摄像头主要关注三个核心方法:uvc_init2、uvc_get_device_with_fd、uvc_open

uvc_init2是UVCCamera中特有的函数,在libuvc本身的uvc_init基础上加上了usbfs参数来实现uvc context的初始化

uvc_get_device_with_fd也是UVCCamera特有的函数,作用是获取通过vid、pid、fd、busNum、devAddr这些信息来标识的摄像头

uvc_open就是打开摄像头

打开视频流
打开摄像头成功后,就需要设置视频流参数和打开视频流了

uvc_error_t start_stream(int index) {
    uvc_error_t res = uvc_get_stream_ctrl_format_size_fps(
            devh[index], &ctrl[index], UVC_FRAME_FORMAT_MJPEG, IMG_WIDTH, IMG_HEIGHT, 1, 30);
    LOGI("start_stream, stream_mode %d, index %d", stream_mode, index);
    if (res < 0) {
        LOGE("start_stream uvc_get_stream_ctrl_format_size_fps error, res %d, index %d", res,
             index);
        return res;
    }
    if (stream_mode) {
        res = uvc_stream_open_ctrl(devh[index], &strmhp[index], &ctrl[index]);
        LOGI("start_stream uvc_stream_open_ctrl, res %d, index %d", res, index);

        res = uvc_stream_start_bandwidth(strmhp[index], NULL, NULL, 0.49, 0);
        LOGI("start_stream uvc_stream_start_bandwidth, res %d, index %d", res, index);

        uvc_frame_t *frame;
        res = uvc_stream_get_frame(strmhp[index], &frame, 50 * 1000);
        LOGI("start_stream uvc_stream_get_frame, res %d, frame %p, index %d", res, frame, index);
    } else {
        res = uvc_start_streaming_bandwidth(
                devh[index], &ctrl[index], uvc_preview_frame_callback, (int *) index, 0.49, 0);
        LOGI("start_stream uvc_start_streaming_bandwidth, res %d, index %d", res, index);
    }

    if (res == 0) {
        is_start_stream[index] = true;
    } else {
        LOGE("start_stream error, res %d, index %d", res, index);
    }

    return res;
}

uvc_get_stream_ctrl_format_size_fps也是UVCCamera特有的函数,作用是与摄像头协商视频流的图像帧的格式、宽高,与libuvc本身的uvc_get_stream_ctrl_format_size函数相比多了最小和最大帧率参数

libuvc本身支持两种获取视频流的方式,一种是异步回调方式,另一种是主动轮询方式

主动轮询方式需要先调用uvc_stream_open_ctrl函数后再调用uvc_stream_start_bandwidth函数,uvc_stream_start_bandwidth不需要传入callback函数和用户指针,使用uvc_stream_get_frame函数来获取一帧图像

异步回调方式需要调用uvc_start_streaming_bandwidth函数,在第三个参数中传入callback函数和用户指针(区分多个摄像头)

void uvc_preview_frame_callback(uvc_frame_t *frame, void *ptr) {
    int index = (int) ptr;
    _mutex[index].lock();
    if (data_list[index].size() >= 2) {
        uvc_frame_t *front = data_list[index].front();
        uvc_free_frame(front);
        data_list[index].pop_front();
    }
    uvc_frame_t *copy = uvc_allocate_frame(frame->data_bytes);
    uvc_duplicate_frame(frame, copy);
    data_list[index].push_back(copy);
    _mutex[index].unlock();
}

这样摄像头的每帧图像就会通过这个回调来提供,在这个回调中将图像复制一帧后放入缓冲队列里,作为缓冲帧

获取视频帧
打开视频流后就能获取视频帧了,同样也是异步与同步两种方式

int get_frame(uvc_frame_t *rgb565, int index) {
    if (stream_mode) {
        uvc_frame_t *frame;
        uvc_error_t res;
        _mutex[index].lock();
        res = uvc_stream_get_frame(strmhp[index], &frame, 50 * 1000);
        if (res || !frame) {
            _mutex[index].unlock();
            uvc_perror(res, "get_frame uvc_stream_get_frame error");
            LOGE("get_frame uvc_stream_get_frame error, res %d, frame %p, index %d", res, frame,
                 index);
            return -1;
        }
        uvc_frame_t *copy = uvc_allocate_frame(frame->data_bytes);
        uvc_duplicate_frame(frame, copy);
        _mutex[index].unlock();
        res = uvc_mjpeg2rgb565(copy, rgb565);
        if (res) {
            uvc_perror(res, "get_frame uvc_mjpeg2rgb565 error");
            LOGE("get_frame uvc_mjpeg2rgb565 error, res %d, index %d", res, index);
            uvc_free_frame(copy);
            return -1;
        }
        uvc_free_frame(copy);
    } else {
        _mutex[index].lock();
        if (data_list[index].empty()) {
            _mutex[index].unlock();
            return -1;
        }

        uvc_frame_t *front = data_list[index].front();
        data_list[index].pop_front();
        _mutex[index].unlock();

        uvc_error_t res;
        res = uvc_mjpeg2rgb565(front, rgb565);
        if (res) {
            uvc_perror(res, "get_frame uvc_mjpeg2rgb565 error");
            LOGE("get_frame uvc_mjpeg2rgb565 error, res %d, index %d", res, index);
            uvc_free_frame(front);
            return -1;
        }
        uvc_free_frame(front);
    }
    return 0;
}

同步方式通过uvc_stream_get_frame函数获取一帧图像,然后复制帧,转成rgb565格式(Android 预览使用)

异步方式直接从缓冲队列里取出第一帧,直接转成rgb565格式

拍照
拍照的流程和获取视频帧类似,区别只是不转成rgb565格式,而是直接将视频帧保存成jpg文件存在本地

bool take_photo(int index, std::string path) {
    if (!is_start_stream[index]) {
        LOGE("take photo error, not start_stream, index %d", index);
        return false;
    }
    uvc_frame_t *copy;
    _mutex[index].lock();
    if (stream_mode) {
        uvc_frame_t *frame;
        uvc_frame_t *frame_test;
        uvc_error_t res = uvc_stream_get_frame(strmhp[index], &frame, 50 * 1000);
        uvc_error_t res_test = uvc_stream_get_frame(strmhp[index], &frame_test, 50 * 1000);
        if (res || res_test || !frame || !frame_test) {
            _mutex[index].unlock();
            uvc_perror(res, "take_photo uvc_stream_get_frame error");
            LOGE("take_photo uvc_stream_get_frame error, res %d, frame %p, frame_test %p, index %d",
                 res, frame, frame_test, index);
            return false;
        }
        copy = uvc_allocate_frame(frame->data_bytes);
        uvc_duplicate_frame(frame, copy);
    } else {
        if (data_list[index].empty()) {
            _mutex[index].unlock();
            LOGE("take_photo error, data_list empty, index %d", index);
            return false;
        }

        uvc_frame_t *frame = data_list[index].back();
        copy = uvc_allocate_frame(frame->data_bytes);
        uvc_duplicate_frame(frame, copy);
    }
    _mutex[index].unlock();

    unsigned char *buf = (unsigned char *) copy->data;

    store_MJPG_image(path.c_str(), buf, copy->data_bytes);

    uvc_free_frame(copy);

    return true;

设置摄像头参数
设置摄像头参数使用的都是libuvc自己的api,这里实现了曝光方式、曝光时间、增益和亮度,其他参数可以自行参考文档libuvc

int set_param(int index, bool autoExpo, float expoTime, float gain, int brightness) {
    if (!is_start_stream[index]) {
        LOGE("set param error, not start_stream, index %d", index);
        return false;
    }
    uint8_t mode;
    uvc_error_t res = uvc_set_ae_mode(devh[index], autoExpo ? 8 : 1);
    LOGI("set_param uvc_set_ae_mode, res %d, ae mode expect %d, index %d",
         res, autoExpo ? 8 : 1, index);
    res = uvc_get_ae_mode(devh[index], &mode, UVC_GET_CUR);
    LOGI("set_param uvc_get_ae_mode, res %d, ae mode current %d, index %d", res, mode, index);
    int ae_abs;
    res = uvc_set_exposure_abs(devh[index], (int) (10000 * expoTime));
    LOGI("set_param uvc_set_exposure_abs, res %d, expo time expect %d, index %d", res,
         (int) (10000 * expoTime), index);
    res = uvc_get_exposure_abs(devh[index], &ae_abs, UVC_GET_CUR);
    LOGI("set_param uvc_get_exposure_abs, res %d, expo time current %d, index %d",
         res, ae_abs, index);
    uint16_t value;
    res = uvc_set_gain(devh[index], (int) gain);
    LOGI("set_param uvc_set_gain, res %d, gain expect %d, index %d", res, (int) gain, index);
    res = uvc_get_gain(devh[index], &value, UVC_GET_CUR);
    LOGI("set_param uvc_get_gain, res %d, gain current %d, index %d", res, value, index);
    int16_t _brightness;
    res = uvc_set_brightness(devh[index], (int) round((brightness - 50) * 1.28));
    LOGI("set_param uvc_set_brightness, res %d, brightness expect %d, index %d",
         res, (int) round((brightness - 50) * 1.28), index);
    res = uvc_get_brightness(devh[index], &_brightness, UVC_GET_CUR);
    LOGI("set_param uvc_get_brightness, res %d, brightness current %d, index %d",
         res, _brightness, index);
    return res;
}

这里要注意的是,曝光方式有四种方式:1 MANUAL、2 AUTO、4 SHUTTER_PRIORITY、8 APERTURE_PRIORITY,我的摄像头经测试只有1和8可以设置,原因应该与摄像头有关,可以自己尝试下

曝光时间以0.0001秒为单位,比如10ms就设置100

亮度设置范围在-64到64,对应0到100,用我代码里的公式就行了

关闭摄像头
关闭摄像头比较简单,先关闭流再关闭摄像头,最后清空异步方式的缓冲帧就行了

void release(int index) {
    LOGI("start release, index %d", index);
    if (is_start_stream[index]) {
        is_start_stream[index] = false;
        LOGI("start stop stream, stream_mode %d, index %d", stream_mode, index);
        if (stream_mode) {
            uvc_stream_stop(strmhp[index]);
            uvc_stream_close(strmhp[index]);
            strmhp[index] = nullptr;
        } else {
            uvc_stop_streaming(devh[index]);
        }
        LOGI("finish stop stream, stream_mode %d, index %d", stream_mode, index);
    }
    if (is_connect[index]) {
        is_connect[index] = false;
        LOGI("start release device, index %d", index);
        uvc_close(devh[index]);
        devh[index] = nullptr;
        LOGI("uvc_close, index %d", index);
        uvc_unref_device(dev[index]);
        dev[index] = nullptr;
        LOGI("uvc_unref_device, index %d", index);
        uvc_exit(ctx[index]);
        ctx[index] = nullptr;
        LOGI("uvc_exit, index %d", index);
        //        close(fd[index]);
        fd[index] = 0;
        LOGI("finish release device, index %d", index);
    }
    LOGI("start clear data_list, index %d", index);
    _mutex[index].lock();
    while (!data_list[index].empty()) {
        LOGI("data_list size %d, index %d", data_list[index].size(), index);
        uvc_frame_t *front = data_list[index].front();
        uvc_free_frame(front);
        data_list[index].pop_front();
    }
    data_list[index].clear();
    _mutex[index].unlock();
    LOGI("finish clear data_list, index %d", index);
    LOGI("finish release, index %d", index);
}

最后完整代码可以参考demo

ndk开发基础学习系列
JNI和NDK编程(一)JNI的开发流程
JNI和NDK编程(二)NDK的开发流程
JNI和NDK编程(三)JNI的数据类型和类型签名
JNI和NDK编程(四)JNI调用Java方法的流程

完整demo
https://github.com/GavinAndre/AndroidJNIDemo

参考
libuvc
UVCCamera
libuvc document

 

这篇关于ndk实例总结补充:使用libuvc采集usb图像分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/599689

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(