精华内容
下载资源
问答
  • USB Camera

    2021-01-01 11:28:15
    <div><p>This patch adds USB Camera functionality to the ffmpeg producer. It supports syntax like: <p>PLAY 1-10 "device://video=Some Camera" or PLAY 1-10 DEVICE "video=Some Camera&#...
  • usb camera

    2020-12-08 23:54:32
    <div><p>Hello I would wanna ask how to modify the code to make it work with a usb camera.</p><p>该提问来源于开源项目:FutureSharks/rpi-security</p></div>
  • AndroidUSBCamera基于[saki4510t/UVCCamera](https://github.com/saki4510t/UVCCamera)开发,该项目对USB Camera(UVC设备)的使用和视频数据采集进行了高度封装,能够帮助开发者通过几个简单的API实现USB Camera设备...

        AndroidUSBCamera基于[saki4510t/UVCCamera](https://github.com/saki4510t/UVCCamera)开发,该项目对USB Camera(UVC设备)的使用和视频数据采集进行了高度封装,能够帮助开发者通过几个简单的API实现USB Camera设备的检测、连接、预览和音视频数据采集,最重要的是手机无需root,只需支持otg功能即可驱动。主要功能包括:  

    (1)支持USB Camera设备检测,画面实时预览;  
    (2)支持本地录制mp4格式视频,支持实时获取音视频数据流;  
    (3)支持jpg格式图片抓拍;  
    (4)支持获取camera支持的分辨率,和分辨率切换;  
    (5)支持屏蔽声音,重启Camera; 
    (6)支持相机自动对焦;  

    (7)支持调整对比度和亮度

    (8) 支持480P、720P、1080P and higher

    (9) 支持Android5.0,6.0,7.0,8.0,9.0


    如何使用AndroidUSBCamera项目 


    1. 添加依赖到本地工程
      
    第一步 添加JitPack仓库到工程gradle  
    Step 1. Add the JitPack repository to your build file
    Add it in your root build.gradle at the end of repositories:

     

    allprojects {
    repositories {
    ...
    maven { url 'http://raw.github.com/saki4510t/libcommon/master/repository/' }
    maven { url 'https://jitpack.io' }
    }
    }


    第二步 添加依赖到app Module的gradle   
    Step 2. Add the dependency  

     

     

     

    dependencies {
    compile 'com.github.jiangdongguo:AndroidUSBCamera:1.3.8'
    } 
    

     

    注:最新版为2,3,0

     

     2. 初始化引擎,注册USB设备事件监听器  
      Init AndroidUSBCamera engine,register the USB device event listener  
      

       private USBCameraManager.OnMyDevConnectListener listener = new USBCameraManager.OnMyDevConnectListener() {
            // 插入USB设备
            @Override
            public void onAttachDev(UsbDevice device) {
                if(mUSBManager == null || mUSBManager.getUsbDeviceCount() == 0){
                    showShortMsg("未检测到USB摄像头设备");
                    return;
                }
                // 请求打开摄像头
                if(! isRequest){
                    isRequest = true;
                    if(mUSBManager != null){
                        mUSBManager.requestPermission(0);
                    }
                }
            }
    
    
            // 拔出USB设备
            @Override
            public void onDettachDev(UsbDevice device) {
                if(isRequest){
                    // 关闭摄像头
                    isRequest = false;
                    mUSBManager.closeCamera();
                    showShortMsg(device.getDeviceName()+"已拨出");
                }
            }
    
    
            // 连接USB设备成功
            @Override
            public void onConnectDev(UsbDevice device,boolean isConnected) {
                if(! isConnected) {
                    showShortMsg("连接失败,请检查分辨率参数是否正确");
                    isPreview = false;
                }else{
                    isPreview = true;
                }
            }
    
    
            // 与USB设备断开连接
            @Override
            public void onDisConnectDev(UsbDevice device) {
                showShortMsg("连接失败");
            }
        };
        
        mUVCCameraView = (CameraViewInterface) mTextureView;
        mUVCCameraView.setCallback(new CameraViewInterface.Callback() {
            @Override
            public void onSurfaceCreated(CameraViewInterface view, Surface surface) {
                if(!isPreview && mUSBManager.isCameraOpened()) {
                     mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
                     @Override
                     public void onPreviewResult(boolean result) {
    
    
                     }
                  });
                  isPreview = true;
                  }
             }
                @Override
                public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) {
    
    
                }
    
    
                @Override
                public void onSurfaceDestroy(CameraViewInterface view, Surface surface) {
                    if(isPreview && mUSBManager.isCameraOpened()) {
                        mUSBManager.stopPreview();
                        isPreview = false;
                    }
                }
            });
            // 初始化引擎
            mUSBManager = USBCameraManager.getInstance();
            mUSBManager.initUSBMonitor(this,listener);
            mUSBManager.createUVCCamera(mUVCCameraView);


    3. 注册USB设备广播事件监听器,开始Camera预览  
      Register the USB device broadcast event listener and start the Camera Preview

     

     

     

    // 注册USB事件广播监听器
    if(mUSBManager != null){
          mUSBManager.registerUSB();
    }
    // 恢复Camera预览
     if(mUVCCameraView != null){
          mUVCCameraView.onResume();
     }

     


    4. 注销USB设备广播事件监听器,停止Camera预览  
      Unregister the USB device broadcast event listener and stop the Camera Preview

     

     

     

     

    // 注销USB事件广播监听器
     if(mUSBManager != null){
             mUSBManager.unregisterUSB();
      }
     // 暂停Camera预览
     if(mUVCCameraView != null){
             mUVCCameraView.onPause();
     }

     


     5. 图片抓拍
      Picture capturing

     

     

     

     

    if(mUSBManager == null || ! mUSBManager.isCameraOpened()){
              showShortMsg("抓拍异常,摄像头未开启");
              return;
     }
     mUSBManager.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() {
              @Override
              public void onCaptureResult(String path) {
                   showShortMsg("保存路径:"+path);
              }
     });

     


    6. 本地录制(可实时获取音视频数据流)
       recoring mp4,and get media real-stream  
       

     

     

     

    if(mUSBManager == null || ! mUSBManager.isCameraOpened()){
               showShortMsg("录制异常,摄像头未开启");
               return;
     }
    if(! mUSBManager.isRecording()){
                        String videoPath = USBCameraManager.ROOT_PATH+System.currentTimeMillis();
                        FileUtils.createfile(FileUtils.ROOT_PATH+"test666.h264");
                        RecordParams params = new RecordParams();
                        params.setRecordPath(videoPath);
                        params.setRecordDuration(0);    // 设置为0,不分割保存
                        params.setVoiceClose(false);    // 不屏蔽声音
                        mUSBManager.startRecording(params, new AbstractUVCCameraHandler.OnEncodeResultListener() {
                            @Override
                            public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) {
                                // type = 0,aac格式音频流
                                // type = 1,h264格式视频流
                                if(type == 1){
                                    FileUtils.putFileStream(data,offset,length);
                                }
                            }
    
    
                            @Override
                            public void onRecordResult(String videoPath) {
                                showShortMsg(videoPath);
                            }
                        });
    // 停止录制
    mUSBManager.stopRecording();


    7. 切换分辨率
      update Resulotion  
        

     

    mUSBManager.updateResolution(this, mUVCCameraView, 320, 240, new USBCameraManager.OnPreviewListener() {
                 @Override
                 public void onPreviewResult(boolean isSuccess) {
                        if(! isSuccess) {
                                showShortMsg("预览失败,不支持该分辨率");
                            }else {
                                showShortMsg("以切换到分辨率为320x240");
                            }
                        }
          });  
     // 获取Camera支持得分辨率  
     List<Size> list = mUSBManager.getSupportedPreviewSizes();
     // Camera自动对焦  
     mUSBManager.startCameraFoucs();


    8. 释放引擎资源
      release resource  

     

    // 释放资源
    if(mUSBManager != null){
           mUSBManager.release();
     }

    9. 添加权限
      add permissions  
       

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />


    USBCameraManager  API (Other)

     

    (1) void requestPermission(int index):请求授予开启USB摄像头权限;
    (2) int getUsbDeviceCount():返回查询到的可用USB Camera数目;
    (3) boolean isRecording():判断是否正在录制视频;
    (4) boolean isCameraOpened():判断USB摄像头是否正常打开;
    (5) void release():释放资源
    (6) USBMonitor getUSBMonitor():返回USBMonitor实例;
    (7) mUSBManager.setModelValue(USBCameraManager.MODE_CONTRAST,contrast++); 调整对比度
    (8) mUSBManager.setModelValue(USBCameraManager.MODE_BRIGHTNESS,brightness++);调整亮度

     

     

     

     

     

    注:在使用Android Studio移植UVCCamera时,很多朋友可能会遇到"open(“/dev/bus/usb/001/002”, O_RDWR, 0),报错,Permission denied"问题,这是由于Android Studio使用的ndk版本所致,建议使用ndk-r14即可。解决方法:local.properties-->指定ndk.dir版本。(注:这里使用的是离线方式)

     

    最新版为2.3.0,更新时间为2019年6月17日,使用方法请移步Github项目

     

     

    GitHub源码地址:https://github.com/jiangdongguo/AndroidUSBCamera(如果对您有用,欢迎star&fork以表支持~谢谢^_^!)

     

    Download APK

     In order to display the functions, I develop a simple released apk,which is based on version 2.3.1,and the build version is 28.0.3.Here is my configs and if you have any questions please issues to me ,I will follow it do my best.

    ext {
        javaSourceCompatibility = JavaVersion.VERSION_1_8
        javaTargetCompatibility = JavaVersion.VERSION_1_8
        compileSdkVersion = 28
        buildToolsVersion = '28.0.3'
        minSdkVersion = 21
        targetSdkVersion = 28
        commonLibVersion= '2.12.4'
    }
    

    下载APP:  安装密码 12345678

    displaying:

     

     

    展开全文
  • AndroidUSBCamera  AndroidUSBCamera is developed based on the saki4510t/UVCCamera, the project of USB Camera (UVC equipment) and the use of video data acquisition are highly packaged, and it can help...
  • AndroidUsbCamera:适用于GNULinux的Android USB Camera DriverBridge
  • MTK USB camera patch UVC

    2018-03-03 17:25:13
    MTK usb camera patch MTK usb camera patch MTK usb camera patch MTK usb camera patch
  • 最近要在我们的某Android设备上添加USB Camera,之前没有神马经验。加上以前做过这个的同事很忙,也不好相处,问了十句话才给我回复一句话这样子的情况,我很郁闷,就把添加的过程全部写出来本菜鸟也是刚接触Android...

    最近要在我们的某Android设备上添加USB Camera,之前没有神马经验。加上以前做过这个的同事很忙,也不好相处,问了十句话才给我回复一句话这样子的情况,我很郁闷,就把添加的过程全部写出来

    本菜鸟也是刚接触Android的开发。所以写的尽量简单明了。、

    其实Camera的开发各个书上或者博客上都有写,但是没有写具体如何做。笨鸟我也晃了几天没什么进展。

    以下摘抄自我桌上的一本 王石磊等编著:

    Android的Camera系统包括了Camera驱动程序层、Camera硬件抽象层、AudioService、Camera本地库、Camera的Java框架类和Java应用层对Camera系统的调用。

    图我就不贴了,网上到处都是。

    我当时要做的工作就是  需求:

    在我们的Android平台上 插上一个USB Camera设备(UVC),系统能够使用(拍照)。

    挺简单的功能,各个手机都支持拍照,可是我是菜鸟啊,我不会。

    根据上面那本书的描述,我们就一步步的来:

    1.硬件驱动

    因为USB camera设备驱动都会在linux系统中集成,所以只需要重新配置linux kernel使其支持uvc设备

    linux目录

    make menuconfig->

    Device Drivers  --->

    Multimedia support  --->

    [*]   Video capture adapters  --->

    [*]   V4L USB devices  --->

      USB Video Class (UVC)

    [*]     UVC input events device support

    选中最后两个模块就好了。

    重新编译kernel。载入我们的设备

    然后需要手动加载驱动:

    insmod /system/lib/modules/current/kernel/drivers/media/video/uvc/uvcvideo.ko

    就会出现/dev/video0 设备

    ps:我遇到一个问题,在ll /dev/video0 时候,video0设备是0600属性。

    在HAL层open这个设备时候permission deny。不知道为什么,而且在HAL层使用chmod函数也会出错。

    但是通过串口可以改变设备权限 chmod 0666 /dev/video0,这样open没有问题

    2.硬件抽象层

    可以参考:

    Android_src_code\development\tools\emulator\system\camera中的camera HAL实现

    这个原始的东东编译出来会到/system/lib/hw/下生成一个camera.goldfish.so,我们修改后,生成自己设备的so.

    camera.vendor.so,我当时是adb 到我们的设备把goldfish删掉了,这个push进去才生效的。

    因为我们这边在IceCream上实现过了,在JellyBean下就直接用了我们的项目没有做任何修改,这方面我也没有多看。

    3.AudioService、Camera本地库、Camera的Java框架类和Java应用层对Camera系统的调用

    这部分Android都做好了,所以实际上我也没做什么。

    直接把驱动和HAL层实现,就可以用android自带的camera apk拍照了。

    完结:

    很简单的功能,完成起来也算顺利,因为有一点前人的经验。也许真完全自己做起来也要一个月。

    本菜鸟开始学习Android开发了。

    展开全文
  • 一直想自己写一个从HAL层到应用层的Camera例子,android4.0上usb camera用不了 所以决定自己写一个 usb camera和coms原理都是一样的 基本v4l2 只不过源码数据格式不一样而已 下面我们就从HAL层开始以下是我的代码,...

    一直想自己写一个从HAL层到应用层的Camera例子,android4.0上usb camera用不了 所以决定自己写一个 usb camera和coms原理都是一样的 基本v4l2 只不过源码数据格式不一样而已 下面我们就从HAL层开始

    以下是我的代码,先上代码在 一步步说明

    fimcgzsd.c

    /*

    * Android USB Camera zc3xx Library

    *

    * Copyright (c) 2014 Store information technology guangzhou ltd

    * Copyright (c) 2014 hclydao

    *

    * This program is free software; you can redistribute it and/or modify

    * it under the terms of the GNU General Public License as published by

    * the Free Software Foundation; either version 2 of the License.

    */

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #include

    #define LOG_TAG "FimcGzsd"

    #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)

    #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG, __VA_ARGS__)

    #define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOG_TAG, __VA_ARGS__)

    #define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOG_TAG, __VA_ARGS__)

    #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOG_TAG, __VA_ARGS__)

    struct fimc_buffer {

    unsigned char *start;

    size_t length;

    };

    static int fd = -1;

    struct fimc_buffer *buffers=NULL;

    struct v4l2_buffer v4l2_buf;

    static int bufnum = 1;

    static int mwidth,mheight;

    /*

    *open usb camera device

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_open(JNIEnv * env, jclass obj, const jbyteArray devname)

    {

    jbyte *dev = (jbyte*)(*env)->GetByteArrayElements(env, devname, 0);

    fd = open(dev, O_RDWR, 0);

    if (fd<0)

    {

    LOGE("%s ++++ open errorn",dev);

    return -1;

    }

    (*env)->ReleaseByteArrayElements(env, devname, dev, 0);

    return fd;

    }

    /*

    * init device

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_init(JNIEnv * env, jclass obj, jint width, jint height,jint numbuf)

    {

    int ret;

    int i;

    bufnum = numbuf;

    mwidth = width;

    mheight = height;

    struct v4l2_format fmt;

    struct v4l2_capability cap;

    ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);

    if (ret < 0) {

    LOGE("%d :VIDIOC_QUERYCAP failedn",__LINE__);

    return -1;

    }

    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {

    LOGE("%d : no capture devicesn",__LINE__);

    return -1;

    }

    memset( &fmt, 0, sizeof(fmt));

    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;

    fmt.fmt.pix.width = width;

    fmt.fmt.pix.height = height;

    if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0)

    {

    LOGE("++++%d : set format failedn",__LINE__);

    return -1;

    }

    struct v4l2_requestbuffers req;

    req.count = numbuf;

    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    req.memory = V4L2_MEMORY_MMAP;

    ret = ioctl(fd, VIDIOC_REQBUFS, &req);

    if (ret < 0) {

    LOGE("++++%d : VIDIOC_REQBUFS failedn",__LINE__);

    return -1;

    }

    buffers = calloc(req.count, sizeof(*buffers));

    if (!buffers) {

    LOGE ("++++%d Out of memoryn",__LINE__);

    return -1;

    }

    for(i = 0; i< bufnum; ++i) {

    memset(&v4l2_buf, 0, sizeof(v4l2_buf));

    v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    v4l2_buf.memory = V4L2_MEMORY_MMAP;

    v4l2_buf.index = i;

    ret = ioctl(fd , VIDIOC_QUERYBUF, &v4l2_buf);

    if(ret < 0) {

    LOGE("+++%d : VIDIOC_QUERYBUF failedn",__LINE__);

    return -1;

    }

    buffers[i].length = v4l2_buf.length;

    if ((buffers[i].start = (char *)mmap(0, v4l2_buf.length,

    PROT_READ | PROT_WRITE, MAP_SHARED,

    fd, v4l2_buf.m.offset)) < 0) {

    LOGE("%d : mmap() failed",__LINE__);

    return -1;

    }

    }

    return 0;

    }

    /*

    *open usb camera device

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_streamon(JNIEnv * env, jclass obj)

    {

    int i;

    int ret;

    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    for(i = 0; i< bufnum; ++i) {

    memset(&v4l2_buf, 0, sizeof(v4l2_buf));

    v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    v4l2_buf.memory = V4L2_MEMORY_MMAP;

    v4l2_buf.index = i;

    ret = ioctl(fd, VIDIOC_QBUF, &v4l2_buf);

    if (ret < 0) {

    LOGE("%d : VIDIOC_QBUF failedn",__LINE__);

    return ret;

    }

    }

    ret = ioctl(fd, VIDIOC_STREAMON, &type);

    if (ret < 0) {

    LOGE("%d : VIDIOC_STREAMON failedn",__LINE__);

    return ret;

    }

    return 0;

    }

    /*

    *get one frame data

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_dqbuf(JNIEnv * env, jclass obj,const jbyteArray videodata)

    {

    int ret;

    jbyte *data = (jbyte*)(*env)->GetByteArrayElements(env, videodata, 0);

    v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    v4l2_buf.memory = V4L2_MEMORY_MMAP;

    ret = ioctl(fd, VIDIOC_DQBUF, &v4l2_buf);

    if (ret < 0) {

    LOGE("%s : VIDIOC_DQBUF failed, dropped framen",__func__);

    return ret;

    }

    memcpy(data,buffers[v4l2_buf.index].start,buffers[v4l2_buf.index].length);

    (*env)->ReleaseByteArrayElements(env, videodata, data, 0);

    return v4l2_buf.index;

    }

    /*

    *put in frame buffer to queue

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_qbuf(JNIEnv * env, jclass obj,jint index)

    {

    int ret;

    v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    v4l2_buf.memory = V4L2_MEMORY_MMAP;

    v4l2_buf.index = index;

    ret = ioctl(fd, VIDIOC_QBUF, &v4l2_buf);

    if (ret < 0) {

    LOGE("%s : VIDIOC_QBUF failedn",__func__);

    return ret;

    }

    return 0;

    }

    /*

    *streamoff

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_streamoff(JNIEnv * env, jclass obj,jint index)

    {

    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    int ret;

    ret = ioctl(fd, VIDIOC_STREAMOFF, &type);

    if (ret < 0) {

    LOGE("%s : VIDIOC_STREAMOFF failedn",__func__);

    return ret;

    }

    return 0;

    }

    /*

    *release

    */

    JNIEXPORT jint JNICALL Java_com_dao_usbcam_Fimcgzsd_release(JNIEnv * env, jclass obj)

    {

    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    int ret;

    int i;

    ret = ioctl(fd, VIDIOC_STREAMOFF, &type);

    if (ret < 0) {

    LOGE("%s : VIDIOC_STREAMOFF failedn",__func__);

    return ret;

    }

    for (i = 0; i < bufnum; i++) {

    ret = munmap(buffers[i].start, buffers[i].length);

    if (ret < 0) {

    LOGE("%s : munmap failedn",__func__);

    return ret;

    }

    }

    free (buffers);

    close(fd);

    return 0;

    }

    首先是open这个就不作说明了

    第二初始化init函数

    ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);

    if (ret < 0) {

    LOGE("%d :VIDIOC_QUERYCAP failedn",__LINE__);

    return -1;

    }

    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {

    LOGE("%d : no capture devicesn",__LINE__);

    return -1;

    }获取设备相关信息,检查是否支持capture模式

    memset( &fmt, 0, sizeof(fmt));

    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;

    fmt.fmt.pix.width = width;

    fmt.fmt.pix.height = height;

    if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0)

    {

    LOGE("++++%d : set format failedn",__LINE__);

    return -1;

    }设置格式,usb camera获取到的已经是jpeg格式 所以这里设置成RGB565格式

    struct v4l2_requestbuffers req;

    req.count = numbuf;

    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    req.memory = V4L2_MEMORY_MMAP;

    ret = ioctl(fd, VIDIOC_REQBUFS, &req);

    if (ret < 0) {

    LOGE("++++%d : VIDIOC_REQBUFS failedn",__LINE__);

    return -1;

    }申请缓冲区,这里申请numbuf个缓冲帧

    buffers = calloc(req.count, sizeof(*buffers));

    if (!buffers) {

    LOGE ("++++%d Out of memoryn",__LINE__);

    return -1;

    }

    for(i = 0; i< bufnum; ++i) {

    memset(&v4l2_buf, 0, sizeof(v4l2_buf));

    v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

    v4l2_buf.memory = V4L2_MEMORY_MMAP;

    v4l2_buf.index = i;

    ret = ioctl(fd , VIDIOC_QUERYBUF, &v4l2_buf);

    if(ret < 0) {

    LOGE("+++%d : VIDIOC_QUERYBUF failedn",__LINE__);

    return -1;

    }

    buffers[i].length = v4l2_buf.length;

    if ((buffers[i].start = (char *)mmap(0, v4l2_buf.length,

    PROT_READ | PROT_WRITE, MAP_SHARED,

    fd, v4l2_buf.m.offset)) < 0) {

    LOGE("%d : mmap() failed",__LINE__);

    return -1;

    }

    }映射虚拟内存到物理地址,获取每个缓冲区的物理地址

    streamon函数

    将缓冲区放入队列并开启数据流

    dqbuf函数

    获取一帧数据 返回当前缓冲区的序列号

    qbuf函数

    将指定缓冲区放入队列,获取到某一缓冲区的数据后需要重新将这个缓冲区放入队列

    后面两个我就不多说明了

    Android.mk文件:

    LOCAL_PATH:= $(call my-dir)

    include $(CLEAR_VARS)

    LOCAL_MODULE_TAGS := eng

    LOCAL_SRC_FILES:= fimcgzsd.c

    LOCAL_MODULE := libfimcgzsd

    LOCAL_LDLIBS := -llog

    LOCAL_SHARED_LIBRARIES := libc libcutils

    include $(BUILD_SHARED_LIBRARY)

    参考博文:https://blog.csdn.net/eastmoon502136/article/details/8190262

    ============================================

    作者:hclydao

    https://blog.csdn.net/hclydao

    版权没有,但是转载请保留此段声明

    ============================================

    展开全文
  • usbcamera_demo

    2017-08-03 09:10:46
    usbcamera_demo
  • AndroidUSBCamera-master.zip

    2020-04-22 01:02:46
    Android USBCamera,UVCCamera开发基础库,Android USBCamera,UVCCamera开发基础库,Android USBCamera,UVCCamera开发基础库
  • USBCamera.zip

    2020-04-13 09:30:10
    1.支持USB Camera设备检测,画面实时预览 2.支持jpg格式图片抓拍 仅提供了基础的功能,大家需要的功能可以根据需求进行自行添加
  • usb camera APK

    2014-06-17 11:05:43
    一个简单的usb camera previw 功能的APK。
  • <p>Would it be possible to change the camera source from the onboard camera to USB camera? <p>Kind Regards, JP</p><p>该提问来源于开源项目:dusty-nv/jetson-inference</p></div>
  • Android USBCamera,UVCCamera开发通用库

    热门讨论 2018-09-03 15:25:58
    Android USBCamera,UVCCamera开发通用库 https://blog.csdn.net/qq_31939617/article/details/82347939
  • USB camera redirection

    2021-01-12 04:25:44
    Now I want to share the USB camera (Logitec) connected to the pi so I can use it in video calls done via the thin client. But USB redirection doesn't work. The camera can be accessed on the pi and...
  • usb camera HAL

    2014-06-17 11:03:10
    一个简单的USB camera HAL源码,帮助学习android HAL的编写方法。
  • AndroidUSBCamera.zip

    2019-07-09 12:34:07
    自己编译的Android USB Camera android sdk,已经测试可以正常使用了
  • USB Camera开发包

    2011-02-25 18:56:33
    USB Camera开发包 USB Camera开发包 USB Camera开发包
  • usb camera demo

    2016-06-24 12:29:26
     * usb_camera_test.c - usb camera capture program  *  * Test opening usb camera, reading and saving data from usb camera by hcd.  */ #include #include #include #include #include
    /*
     * usb_camera_test.c - usb camera capture program
     *
     * Test opening usb camera, reading and saving data from usb camera by hcd.
     */


    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include <string.h>
    #include <errno.h>
    #include <getopt.h> /* getopt_long() */
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <linux/videodev2.h>
    #include <sys/ioctl.h>
    #include <malloc.h>
    #include <sys/mman.h>
    #include <unistd.h>



    #define CLEAR(x) memset(&(x), 0, sizeof (x))


    struct buffer {
    void *      start;
    size_t      length;
    };


    typedef struct {
    char *data;
    unsigned long length;
    } frame_t;


    struct buffer *         buffers        = NULL;
    static unsigned int     n_buffers       = 0;


    static int open_device(int *fd, char *dev_name)
    {
    *fd =open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
    if(*fd < 0){
    close(*fd);
    return -1;
    }


    return 0;
    }


    static int init_device (int fd, int force_format, int print_info)
    {
    struct v4l2_requestbuffers req;
    struct v4l2_capability cap;
    struct v4l2_cropcap cropcap;
    struct v4l2_crop crop;
    struct v4l2_format fmt;
    unsigned int min;
    int ret;
    struct v4l2_fmtdesc fmtdesc;


    if (-1 == ioctl (fd, VIDIOC_QUERYCAP, &cap)) {
    fprintf (stderr, "VIDIOC_QUERYCAP fail\n");
    goto err;
    }


    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
    fprintf (stderr, "VIDIOC_QUERYCAP fail\n");
    goto err;
    }


    if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
    fprintf (stderr, "device does not support streaming i/o\n");
    goto err;
    }


    if (print_info) {
    /* Print capability infomations */
    printf("Capability Informations:\n");
    printf(" driver: %s\n", cap.driver);
    printf(" card: %s\n", cap.card);
    printf(" bus_info: %s\n", cap.bus_info);
    printf(" version: %08X\n", cap.version);
    }


    /* Select video input, video standard and tune here. */
    CLEAR (cropcap);


    cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if (0 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
    crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    crop.c = cropcap.defrect; /* reset to default */


    if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop)) {
    switch (errno) {
    case EINVAL:
    /* Cropping not supported. */
    break;
    default:
    /* Errors ignored. */
    break;
    }
    }
    }


    /* enum video formats. */
    CLEAR(fmtdesc);
    if (print_info) {
    fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    printf("Enum format:\n");
    while ((ret = ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) == 0) {
    fmtdesc.index++;
    printf(" <%d> pixelformat = \"%c%c%c%c\", description = %s\n",fmtdesc.index,
    fmtdesc.pixelformat & 0xFF,
    (fmtdesc.pixelformat >> 8) & 0xFF,
    (fmtdesc.pixelformat >> 16) & 0xFF,
    (fmtdesc.pixelformat >> 24) & 0xFF,
    fmtdesc.description);
    }
    }


    /* set video formats. */
    CLEAR (fmt);
    char * p = (char *)(&fmt.fmt.pix.pixelformat);
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if (force_format) {
    if (ioctl (fd, VIDIOC_G_FMT, &fmt) < 0) {
    /* Errors ignored. */
    printf("get fmt fail\n");
    }


    //fmt.fmt.pix.width       = width;
    //fmt.fmt.pix.height      = height;
    //    init_mem_repo();
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
    fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;


    if (-1 == ioctl (fd, VIDIOC_S_FMT, &fmt)){
    printf("set format fail\n");
    // return -1;
    }
    }


    if (print_info) {
    if (ioctl (fd, VIDIOC_G_FMT, &fmt) < 0) {
    /* Errors ignored. */
    printf("get fmt fail\n");
    }


    printf("Used fmt:\n");
    printf(" fmt.type = %d\n", fmt.type);
    printf(" fmt.width = %d\n", fmt.fmt.pix.width);
    printf(" fmt.height = %d\n", fmt.fmt.pix.height);
    printf(" fmt.format = %c%c%c%c\n", p[0], p[1], p[2], p[3]);
    printf(" fmt.field = %d\n", fmt.fmt.pix.field);
    }


    /* Buggy driver paranoia. */
    min = fmt.fmt.pix.width * 2;
    if (fmt.fmt.pix.bytesperline < min)
    fmt.fmt.pix.bytesperline = min;


    min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
    if (fmt.fmt.pix.sizeimage < min)
    fmt.fmt.pix.sizeimage = min;


    CLEAR (req);
    req.count   = 8;
    req.type    = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    req.memory  = V4L2_MEMORY_MMAP;


    if (-1 == ioctl (fd, VIDIOC_REQBUFS, &req)) {
    fprintf (stderr, "VIDIOC_QUERYCAP fail\n");
    goto err;
    }


    if (req.count < 2) {
    fprintf (stderr, "Insufficient buffer memory\n");
    return -1;
    }


    buffers = calloc (req.count, sizeof (*buffers));
    if (!buffers) {
    fprintf (stderr, "Out of memory\n");
    return -1;
    }


    for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
    struct v4l2_buffer buf;


    CLEAR (buf);
    buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory      = V4L2_MEMORY_MMAP;
    buf.index       = n_buffers;


    if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf)){
    fprintf (stderr, "VIDIOC_QUERYCAP fail\n");
    goto err;
    }


    buffers[n_buffers].length = buf.length;
    buffers[n_buffers].start = mmap (NULL /* start anywhere */,
           buf.length,
           PROT_READ | PROT_WRITE /* required */,
           MAP_SHARED /* recommended */,
           fd, buf.m.offset);
    if (MAP_FAILED == buffers[n_buffers].start){
    fprintf (stderr, "mmap fail\n");
    goto err;
    }
    }


    return 0;
    err:
    return -1;
    }


    static void errno_exit (const char *s)
    {
    fprintf (stderr, "%s error %d, %s\n",s, errno, strerror (errno));
    exit (EXIT_FAILURE);
    }


    static void start_capturing (int fd)
    {
    unsigned int i;
    int ret;
    enum v4l2_buf_type type;


    //printf("n_buffers:%d\n",n_buffers);
    for (i = 0; i < n_buffers; ++i) {
    struct v4l2_buffer buf;


    CLEAR (buf);


    buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory      = V4L2_MEMORY_MMAP;
    buf.index       = i;
    ret = ioctl (fd, VIDIOC_QBUF, &buf);
    if (-1 == ret)
    errno_exit ("VIDIOC_QBUF");
    }


    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;


    ret = ioctl(fd, VIDIOC_STREAMON, &type);
    if (-1 == ret)
    errno_exit ("VIDIOC_STREAMON");
    }


    static void stop_capturing(int fd)
    {
    int ret;
    enum v4l2_buf_type type;


    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;


    ret = ioctl(fd, VIDIOC_STREAMOFF, &type);
    if (-1 == ret)
    errno_exit("VIDIOC_STREAMOFF");
    }


    static void uninit_device(void)
    {
    unsigned int i;


    for (i = 0; i < n_buffers; ++i)
    if (-1 == munmap(buffers[i].start, buffers[i].length))
    errno_exit("munmap");
    free(buffers);
    }


    static void close_device(int fd)
    {
    if (-1 == close(fd))
    errno_exit("close");


    fd = -1;
    }


    /*
     * read a frame of image from video device
     */
    static int get_buffer(int fd, struct v4l2_buffer *v4l_buf)
    {
    if (-1 == ioctl(fd, VIDIOC_DQBUF, v4l_buf))
    return 0;


    return 1;
    }


    /*
     * enqueue the frame again
     */
    static int put_buffer(int fd, struct v4l2_buffer *v4l_buf)
    {
    return ioctl(fd, VIDIOC_QBUF, v4l_buf);
    }


    static void get_fps(int fd)
    {
    struct v4l2_streamparm* getfps;


    getfps=(struct v4l2_streamparm *) calloc(1, sizeof(struct v4l2_streamparm));
    memset(getfps, 0, sizeof(struct v4l2_streamparm));
    getfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;


    if(ioctl(fd, VIDIOC_G_PARM, getfps) < 0){
    /* Errors ignored. */
    printf("get fps fail\n");
    }
    printf(" getfps.numerator = %d, getfps.denominator = %d\n",
    getfps->parm.capture.timeperframe.numerator,
    getfps->parm.capture.timeperframe.denominator);
    }


    static void usage(FILE *fp, char **argv, char *dev_name, int frame_count, char *pict_path)
    {
    fprintf(fp,
    "Usage: %s [options]\n\n"
    "Version 1.0\n"
    "Options:\n"
    "-d | --device name   Video device name [%s]\n"
    "-h | --help          Print this message\n"
    "-o | --output        Outputs stream to jpeg pict\n"
    "-f | --format        Force format to MJPEG\n"
    "-c | --count         Number of frames to grab [%i]\n"
    "-p | --path          pict output path [%s]\n"
    "-i | --info          print debug info\n"
    "",
    argv[0], dev_name, frame_count, pict_path);
    }


    static const char short_options[] = "d:hofc:p:i";


    static const struct option
    long_options[] = {
    { "device", required_argument, NULL, 'd' },
    { "help",   no_argument,       NULL, 'h' },
    { "output", no_argument,       NULL, 'o' },
    { "format", no_argument,       NULL, 'f' },
    { "count",  required_argument, NULL, 'c' },
    { "path",   required_argument, NULL, 'p' },
    { "info",   no_argument,       NULL, 'i' },
    { 0, 0, 0, 0 }
    };




    int main(int argc, char **argv)
    {
    char *dev_name = "/dev/video0";
    char *pict_path = "/";
    int fd_cam =  -1;
    int fd_pict = -1;
    // int out_buf;
    int out_buf = 1; 
    int force_format;
    int frame_count = 2;
    // int print_info = 0;
    int print_info = 1;
    int r, i, ret;
    struct timeval tv;
    fd_set fds;
    char file[20];
    frame_t *fm = malloc(sizeof(frame_t));
    struct v4l2_buffer buf;


    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;


    for (;;) {
    int idx;
    int c;


    c = getopt_long(argc, argv,
    short_options, long_options, &idx);


    if (-1 == c)
    break;


    switch (c) {
    case 0: /* getopt_long() flag */
    break;
    case 'd':
    dev_name = optarg;
    break;
    case 'h':
    usage(stdout, argv, dev_name, frame_count, pict_path);
    exit(EXIT_SUCCESS);
    case 'o':
    out_buf++;
    break;
    case 'f':
    force_format++;
    break;
    case 'c':
    errno = 0;
    frame_count = strtol(optarg, NULL, 0);
    if (errno)
    errno_exit(optarg);
    break;
    case 'p':
    pict_path = optarg;
    break;
    case 'i':
    print_info++;
    break;
    default:
    usage(stderr, argv, dev_name, frame_count, pict_path);
    exit(EXIT_FAILURE);
    }
    }


    fprintf(stdout, "===usb camera test start===\n");
    if (print_info)
    printf("open and init camera\n");
    ret = open_device(&fd_cam, dev_name);
    if (ret == -1) {
    printf("open camera failed\n");
    return -1;
    }


    ret = init_device (fd_cam, force_format, print_info);
    if (ret == -1) {
          printf("init camera failed\n");
    return -1;
    }


    if (print_info)
    printf("start capturing\n");
    start_capturing (fd_cam);


    for(i = 0; i < frame_count; i++)
    {
    FD_ZERO (&fds);
    FD_SET (fd_cam, &fds);
    tv.tv_sec = 2;
    tv.tv_usec = 0;


    r = select (fd_cam + 1, &fds, NULL, NULL, &tv);


    r = get_buffer(fd_cam, &buf);


    fm->data = buffers[buf.index].start;
    fm->length = buf.bytesused;


    if(r != 0) {
    int flag = 0;


    if (out_buf) {
    sprintf(file,"%s/pic%d.jpg", pict_path, i);
    fd_pict = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    if(fd_pict != -1) {
    if (print_info) {
    flag = 1;
    printf(" open pic%d and write\n", i);
    }
    write(fd_pict, fm->data, fm->length);
    close(fd_pict);
    }
    }
    if (flag == 0 && print_info == 0) {
    fflush(stdout);
    fprintf(stdout, ".");
    }
    }


    put_buffer(fd_cam, &buf);
    if (print_info)
    get_fps(fd_cam);
    }


    if (print_info)
    printf("stop capturing, close camera\n");
    stop_capturing(fd_cam);
    uninit_device();
    close_device(fd_cam);
    fprintf(stdout, "\n===usb camera test done===\n");


    return 0;

    }


    #if   0
    usb_camera_test.c is used to test opening usb camera,
    reading and saving data from usb camera by hcd.

    You can use it as bolow, for example:

    (1) ./usb_camera_test -o -f -c 2
    Func Desc: open "/dev/video0", save stream as jpeg picture
    to "/" catalog, set format as MJPEG, save 2 frames data.

    (2) ./usb_camera_test -o -p "/mnt/" -f -c 2 -i
    Func Desc: open "/dev/video0", save stream as jpeg picture
    to "/mnt/" catalog, set format as MJPEG, save 2 frames data,
    print debug info.

    (3) ./usb_camera_test -d "/dev/video1" -i
    Func Desc: open "/dev/video1", print debug info, not save
    stream data.

    #endif

    make file:



    展开全文
  • Android USBCamera,UVCCamera开发通用库 https://blog.csdn.net/qq_31939617/article/details/82347939
  • USB Camera (x2): unknown USB model (from OpenPlacer) but working with other software. USB Hub: tried two different hub (two different brand)</p><p>该提问来源于开源项目:openpnp/openpnp</p></div>
  • usb camera trial_1.4.8

    2015-06-27 21:54:23
    手机连接USB摄像头USB camera
  • 包含安卓各个版本的 libusb100.so libuvc.so libUVCCamera.so以及USBCamera使用案例
  • USB camera on Raspberry

    2020-12-26 13:48:40
    <div><p>Can I also use a 720p USB camera with a Raspberry?</p><p>该提问来源于开源项目:brutella/hkcam</p></div>
  • AndroidUSBCamera基于[saki4510t/UVCCamera](https://github.com/saki4510t/UVCCamera)开发,该项目对USB Camera(UVC设备)的使用和视频数据采集进行了高度封装,能够帮助开发者通过几个简单的API实现USB Camera设备...
  • <div><p>When the selected camera in Wire is a <em><strong>virtual effects camera</strong></em>*, and the virtual camera service is not ...change the selection in Wire to activate the attached USB camera....
  • 我采用的是<a href="https://github.com/jiangdongguo/AndroidUSBCamera">https://github.com/jiangdongguo/AndroidUSBCamera</a>这位博主的项目源码,编译成功后在安卓9手机上可以运行,但在安卓10及...
  • Using a USB camera?

    2020-12-28 17:16:05
    <div><p>Hi, well this is an only question or suggestion if is not possible. Can I use a USB camera instead of a Picamera??? </p><p>该提问来源于开源项目:kizniche/Mycodo</p></div>
  • C#实现USB Camera的连接

    2017-12-26 09:54:45
    利用AForge实现USB Camera的连接官方SDK,并且包含了AForge(含手册、源码、示例代码、引用库)所有资源,以及自己调试通过的例子。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,859
精华内容 1,543
关键字:

usbcamera