更新记录

2.2(2024-06-17)

1、优化相机Button事件setButtonCallback接口。 2、增加enableCustomVideoCapture接口,USB相机视频数据推到腾讯实时音视频(TRTC)。

2.1(2024-03-13)

修复Button按键回调事件,增加灯光handleXuReadAndWrite灯光控制接口。

2.0(2024-01-06)

1、更新预览拍照接口。 2、增加视频录制接口。 3、增加视频推流接口。

查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:5.0 - 14.0 armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 ×

原生插件通用使用流程:

  1. 购买插件,选择该插件绑定的项目。
  2. 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
  3. 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
  4. 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
  5. 开发完毕后正式云打包

付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios

注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择


UVCCamera多USB摄像头预览、拍照、录视频、RTMP推流插件接口说明

注:在vue3模式下,需要在项目根目录下添加vite.config.js文件,插件页面右上角蓝色按钮导入示例项目即可获取该文件,同时可以参考示例代码。

一、插件对象和标签

//插件对象
var UVCCamera = uni.requireNativePlugin('Lizii-uvccamera-sdk');

//UVCCamera USB摄像头预览标签,多个摄像头多个预览标签,注:当前页面必需nvue页面
<UVCCameraView ref="cameraView" style="width:375rpx;height:240rpx">
</UVCCameraView>

//UVCCamera USB摄像头预览标签(副窗口),一个摄像头可以多个预览标签(副窗口),注:当前页面必需nvue页面
<UVCCameraSecondView ref="cameraSecondView" style="width:375rpx;height:240rpx">
</UVCCameraSecondView>

二、UVCCamera插件接口

1. 检测并授权相机和麦克风权限,Android 9.0之后必须先授权相机权限。

//检测相机和麦克风权限
UVCCamera.checkPermissions(function(res){
    console.log(res);
    if (res.result == true) {
        //授权成功
    } else {
        //权限被拒绝
    }
});

三、USB摄像头预览标签

1. 获取摄像头预览标签对象,建议在mounted()里面获取,不能在onLoad里面获取。

mounted() {
    //获取摄像头预览标签对象
    cameraView = this.$refs.cameraView;

    //设置回调事件
    cameraView.setStateCallback(function(res){
        console.log(res);
    });

    //初始化相机
    cameraView.initCamera();
}

3. 设置相机状态回调事件, 建议在mounted()里调用一次即可。

//设置相机状态回调事件
cameraView.setStateCallback(function(res){
    console.log(res);
    if (res.type == "onAttach") {//usb插上回调
        if (that.deviceId == 0) {
            that.deviceId = res.data.deviceId;

            if (that.deviceId > 0) {
                //请求usb授权并打开相机
                cameraView.requestPermission(that.deviceId);
            }
        }
    } else if (res.type == "onCameraOpen") {//打开相机成功回调
        //开始预览
        cameraView.startPreview();

        //多预览
        cameraSecondView.addSurface(that.deviceId);
    } else if (res.type == "onCameraClose") {//关闭相机回调

    } else if (res.type == "onDetach") {//usb拔掉回调
        if (that.deviceId == res.data.deviceId) {
            //重新初始化相机
            cameraView.reInitCamera();
            that.deviceId = 0;
        }
    } else if (res.type == "onCancel") {//usb权限被拒绝(无权限)

    } else if (res.type == "onError") {//错误回调

    }
});

3. 设置相机Button回调事件, 建议在打开相机成功回调onCameraOpen里调用。

//设置相机Button回调事件
cameraView.setButtonCallback(function(res){
    console.log(res);
    if (res.type == "onButton") {
        var button = res.data.button;
        var state = res.data.state;
    }
});

4. 初始化相机, 建议在mounted()里调用,方便监听usb插上回调onAttach。

//初始化相机
cameraView.initCamera();

5. 重新初始化相机, 建议在usb拔掉回调onDetach里调用,方便监听usb插上回调onAttach。

//重新初始化相机
cameraView.reInitCamera();

6. 销毁相机, 一般不需要调用,页面关闭会自动销毁相机。

//销毁相机
cameraView.destroyCamera();

7. 请求usb授权并打开相机, 一般在usb插上回调onAttach里调用, 多个相机会回调多次,根据业务逻辑选择绑定一个相机设备。

//请求usb授权并打开相机
var deviceId = res.data.deviceId;
cameraView.requestPermission(deviceId);

8. 获取支持分辨率, 一般在打开相机成功回调onCameraOpen里调用。

//获取支持分辨率
cameraView.getSupportedSizeList(function(res){
    console.log(res);
});

9. 设置分辨率, 一般在打开相机成功回调onCameraOpen里调用。

//设置分辨率
var width = 1280;
var height = 720;
cameraView.setPreviewSize(width, height);

10. 设置预览配置, 一般在打开相机成功回调onCameraOpen里调用。

//设置预览配置
cameraView.setPreviewConfig({
    //previewSizeIndex:0,//根据支持的分辨率列表,按照索引index设置分辨率
    mirror:0,//镜像 int MIRROR_NORMAL = 0;MIRROR_HORIZONTAL = 1;MIRROR_VERTICAL = 2;MIRROR_BOTH = 3;MIRROR_NUM = 4;
    rotation:0//方向 取值范围 0 90 180 270
});

11. 开始预览, 一般在打开相机成功回调onCameraOpen里调用。

//开始预览
cameraView.startPreview();

12. 停止预览

//停止预览
cameraView.stopPreview();

13. 设置预览控件填充模式,默认居中裁剪。

//设置预览控件填充模式
var mode = 1;// 0:等比缩放居中留黑边  1:等比缩放居中裁剪
cameraView.setRenderMode(mode);

14. 拍照

//图片配置
cameraView.setImageConfig({
    quality:90
});

//拍照
var nowTime = (new Date()).getTime();
var outpath = plus.io.convertLocalFileSystemURL("_doc/capture_" + nowTime + ".jpg");
cameraView.capture(outpath, function(res){
    console.log(res);
    if (res.type == "onCaptureSuccess") {
        var imagePath = res.data;//图片路径
        that.imagePath = "file://" + imagePath;

        uni.showToast({
            title:"拍照成功"
        });
    } else if (res.type == "onCaptureError") {
        var error = res.data.error;
        var message = res.data.message;
        uni.showToast({
            title:"拍照失败:" + message
        });
    }
});

15. 开始录制视频

//视频配置
cameraView.setVideoConfig({
    frameRate:30,
    bitrate:8388608,
    gop:1,
    audioEnable:false,//以下参数audioEnable:true有效
    audioBitrate:64000,
    audioSampleRate:8000,
    audioChannelCount:1,
    audioMinBufferSize:1024
});

//录制视频
var nowTime = (new Date()).getTime();
var outpath = plus.io.convertLocalFileSystemURL("_doc/video_" + nowTime + ".mp4");
cameraView.startRecord(outpath, function(res){
    console.log(res);
    if (res.type == "onRecordSuccess") {
        var videoPath = res.data;//图片路径
        that.videoPath = "file://" + videoPath;

        uni.showToast({
            title:"录制视频成功"
        });
    } else if (res.type == "onRecordError") {
        var error = res.data.error;
        var message = res.data.message;
        uni.showToast({
            title:"录制视频失败:" + message
        });
    }
});

16. 停止录制视频

//停止录制视频
cameraView.stopRecord();

17. 开始RTMP推流,注意:此接口需要勾选LivePusher模块打包。

//VIDEO_RESOLUTION_TYPE_360_640 = 0;
//VIDEO_RESOLUTION_TYPE_540_960 = 1;
//VIDEO_RESOLUTION_TYPE_720_1280 = 2;
//VIDEO_RESOLUTION_TYPE_640_360 = 3;
//VIDEO_RESOLUTION_TYPE_960_540 = 4;
//VIDEO_RESOLUTION_TYPE_1280_720 = 5;
//VIDEO_RESOLUTION_TYPE_320_480 = 6;
//VIDEO_RESOLUTION_TYPE_180_320 = 7;
//VIDEO_RESOLUTION_TYPE_270_480 = 8;
//VIDEO_RESOLUTION_TYPE_320_180 = 9;
//VIDEO_RESOLUTION_TYPE_480_270 = 10;
//VIDEO_RESOLUTION_TYPE_240_320 = 11;
//VIDEO_RESOLUTION_TYPE_360_480 = 12;
//VIDEO_RESOLUTION_TYPE_480_640 = 13;
//VIDEO_RESOLUTION_TYPE_320_240 = 14;
//VIDEO_RESOLUTION_TYPE_480_360 = 15;
//VIDEO_RESOLUTION_TYPE_640_480 = 16;
//VIDEO_RESOLUTION_TYPE_480_480 = 17;
//VIDEO_RESOLUTION_TYPE_270_270 = 18;
//VIDEO_RESOLUTION_TYPE_160_160 = 19;

//开始推流
cameraView.startPush({
    config:{
        videoResolution:5//VIDEO_RESOLUTION_TYPE_1280_720 = 5;//推流分辨率和预览分辨率保持一致
    },
    rtmpURL:"rtmp://www.xxxxxx.com/xxx/xxx"//推流地址
});

18. 停止RTMP推流,注意:此接口需要勾选LivePusher模块打包。

//停止推流
cameraView.stopPush();

19. 开启和停止自定义采集视频数据并推到腾讯实时音视频(TRTC),注意:此接口需要加上腾讯实时音视频(TRTC)插件打包。

//开启和停止自定义采集视频数据并推到腾讯实时音视频(TRTC)
let streamType = 0;//0:高清大画面 1:低清小画面  2:辅流画面
let enable = true;//true:开启自定义采集视频数据 false:停止自定义采集视频数据
cameraView.enableCustomVideoCapture(streamType, enable);

20. 控制或设定摄像头灯光。

//控制或设定摄像头灯光
// flag   value
// 0X11   0XFF 白光,
// 0X12   0XFF 偏光,
// 0X13   0XFF UV光,
// 0X10   0XFF 关灯
let type = 0;
let flag = 0X11;
let value = 0XFF;
cameraView.handleXuReadAndWrite(type, flag, value);

21. UVCControl操作,控制亮度、饱和度、对比度等等。

//UVCControl操作
cameraView.invokeControl({
    method:"setBrightnessPercent",//UVCControl接口名称
    value:50//参数,有参数接口设置时设置,注:无参数接口不需要这个字段
}, function(res){
    console.log(res);
    if (res.result == true) {
        //成功
        //var data = res.data;//UVCControl接口返回值,无返回值接口无此数据
    } else {
        //失败
        var msg = res.msg;
    }
});

/*
UVCControl接口列表:

public int[] updateScanningModeLimit();
public boolean isScanningModeEnable();
public void setScanningMode(int mode);
public int getScanningMode();
public void resetScanningMode();

public int[] updateAutoExposureModeLimit();
public boolean isAutoExposureModeEnable();
public void setAutoExposureMode(int mode);
public int getAutoExposureMode();
public void resetAutoExposureMode();
public void setExposureTimeAuto(boolean auto);
public boolean isExposureTimeAuto();

public int[] updateAutoExposurePriorityLimit();
public boolean isAutoExposurePriorityEnable();
public void setAutoExposurePriority(int priority);
public int getAutoExposurePriority();
public void resetAutoExposurePriority();

public int[] updateExposureTimeAbsoluteLimit();
public boolean isExposureTimeAbsoluteEnable();
public void setExposureTimeAbsolute(int time);
public int getExposureTimeAbsolute();
public void resetExposureTimeAbsolute();

public int[] updateExposureTimeRelativeLimit();
public boolean isExposureTimeRelativeEnable();
public void setExposureTimeRelative(int step);
public int getExposureTimeRelative();
public void resetExposureTimeRelative();

public int[] updateFocusAbsoluteLimit();
public boolean isFocusAbsoluteEnable();
public void setFocusAbsolute(int focus);
public int getFocusAbsolute();
public void resetFocusAbsolute();
public void setFocusAbsolutePercent(int percent);
public int getFocusAbsolutePercent();

public int[] updateFocusRelativeLimit();
public boolean isFocusRelativeEnable();
public void setFocusRelative(int focus);
public int getFocusRelative();
public void resetFocusRelative();

public int[] updateIrisAbsoluteLimit();
public boolean isIrisAbsoluteEnable();
public void setIrisAbsolute(int iris);
public int getIrisAbsolute();
public void resetIrisAbsolute();

public int[] updateIrisRelativeLimit();
public boolean isIrisRelativeEnable();
public void setIrisRelative(int iris);
public int getIrisRelative();
public void resetIrisRelative();

public int[] updateZoomAbsoluteLimit();
public boolean isZoomAbsoluteEnable();
public void setZoomAbsolute(int zoom);
public int getZoomAbsolute();
public void resetZoomAbsolute();
public void setZoomAbsolutePercent(int percent);
public int getZoomAbsolutePercent();

public int[] updateZoomRelativeLimit();
public boolean isZoomRelativeEnable();
public void setZoomRelative(int zoom);
public int getZoomRelative();
public void resetZoomRelative();

public int[] updatePanAbsoluteLimit();
public boolean isPanAbsoluteEnable();
public void setPanAbsolute(int pan);
public int getPanAbsolute();
public void resetPanAbsolute();

public int[] updateTiltAbsoluteLimit();
public boolean isTiltAbsoluteEnable();
public void setTiltAbsolute(int pan);
public int getTiltAbsolute();
public void resetTiltAbsolute();

public int[] updatePanRelativeLimit();
public boolean isPanRelativeEnable();
public void setPanRelative(int PanRelative);
public int getPanRelative();
public void resetPanRelative();

public int[] updateTiltRelativeLimit();
public boolean isTiltRelativeEnable();
public void setTiltRelative(int TiltRelative);
public int getTiltRelative();
public void resetTiltRelative();

public int[] updateRollAbsoluteLimit();
public boolean isRollAbsoluteEnable();
public void setRollAbsolute(int roll);
public int getRollAbsolute();
public void resetRollAbsolute();

public int[] updateRollRelativeLimit();
public boolean isRollRelativeEnable();
public void setRollRelative(int roll);
public int getRollRelative();
public void resetRollRelative();
public int[] updateFocusAutoLimit();
public boolean isFocusAutoEnable();
public void setFocusAuto(boolean state);
public boolean getFocusAuto();
public void resetFocusAuto();

public int[] updatePrivacyLimit();
public boolean isPrivacyEnable();
public void setPrivacy(boolean state);
public boolean getPrivacy();
public void resetPrivacy();

public int[] updateBrightnessLimit();
public boolean isBrightnessEnable();
public void setBrightness(int brightness);
public int getBrightness();
public void resetBrightness();
public void setBrightnessPercent(int percent);
public int getBrightnessPercent();

public int[] updateContrastLimit();
public boolean isContrastEnable();
public void setContrast(int contrast);
public int getContrast();
public void resetContrast();
public void setContrastPercent(int percent);
public int getContrastPercent();

public int[] updateHueLimit();
public boolean isHueEnable();
public void setHue(int hue);
public int getHue();
public void resetHue();
public void setHuePercent(int percent);
public int getHuePercent();

public int[] updateSaturationLimit();
public boolean isSaturationEnable();
public void setSaturation(int saturation);
public int getSaturation();
public void resetSaturation();
public void setSaturationPercent(int percent);
public int getSaturationPercent();

public int[] updateSharpnessLimit();
public boolean isSharpnessEnable();
public void setSharpness(int sharpness);
public int getSharpness();
public void resetSharpness();
public void setSharpnessPercent(int percent);
public int getSharpnessPercent();

public int[] updateGammaLimit();
public boolean isGammaEnable();
public void setGamma(int gamma);
public int getGamma();
public void resetGamma();
public void setGammaPercent(int percent);
public int getGammaPercent();

public int[] updateWhiteBalanceLimit();
public boolean isWhiteBalanceEnable();
public void setWhiteBalance(int whiteBalance);
public int getWhiteBalance();
public void resetWhiteBalance();
public void setWhiteBalancePercent(int percent);
public int getWhiteBalancePercent();

public int[] updateWhiteBalanceCompoLimit();
public boolean isWhiteBalanceCompoEnable();
public void setWhiteBalanceCompo(int component);
public int getWhiteBalanceCompo();
public void resetWhiteBalanceCompo();

public int[] updateBacklightCompLimit();
public boolean isBacklightCompEnable();
public void setBacklightComp(int backlight_compensation);
public int getBacklightComp();
public void resetBacklightComp();

public int[] updateGainLimit();
public boolean isGainEnable();
public void setGain(int gain);
public int getGain();
public void resetGain();
public void setGainPercent(int percent);
public int getGainPercent();

public int[] updatePowerlineFrequencyLimit();
public boolean isPowerlineFrequencyEnable();
public void setPowerlineFrequency(int frequency);
public int getPowerlineFrequency();
public void resetPowerlineFrequency();

public int[] updateHueAutoLimit();
public boolean isHueAutoEnable();
public void setHueAuto(boolean state);
public boolean getHueAuto();
public void resetHueAuto();

public int[] updateWhiteBalanceAutoLimit();
public boolean isWhiteBalanceAutoEnable();
public void setWhiteBalanceAuto(boolean whiteBalanceAuto);
public boolean getWhiteBalanceAuto();
public void resetWhiteBalanceAuto();

public int[] updateWhiteBalanceCompoAutoLimit();
public boolean isWhiteBalanceCompoAutoEnable();
public void setWhiteBalanceCompoAuto(boolean whiteBalanceCompoAuto);
public boolean getWhiteBalanceCompoAuto();
public void resetWhiteBalanceCompoAuto();

public int[] updateDigitalMultiplierLimit();
public boolean isDigitalMultiplierEnable();
public void setDigitalMultiplier(int digitalMultiplier);
public int getDigitalMultiplier();
public void resetDigitalMultiplier();

public int[] updateDigitalMultiplierLimitLimit();
public boolean isDigitalMultiplierLimitEnable();
public void setDigitalMultiplierLimit(int digitalMultiplierLimit);
public int getDigitalMultiplierLimit();
public void resetDigitalMultiplierLimit();

public int[] updateAnalogVideoStandardLimit();
public boolean isAnalogVideoStandardEnable();
public void setAnalogVideoStandard(int analogVideoStandard);
public int getAnalogVideoStandard();
public void resetAnalogVideoStandard();

public int[] updateAnalogVideoLockStateLimit();
public boolean isAnalogVideoLockStateEnable();
public void setAnalogVideoLockState(int analogVideoLockState);
public int getAnalogVideoLockState();
public void resetAnalogVideoLockState();

public int[] updateContrastAutoLimit();
public boolean isContrastAutoEnable();
public void setContrastAuto(boolean state);
public boolean getContrastAuto();
public void resetContrastAuto();

public void updateCameraParams();//*/

四、USB摄像头预览标签(副窗口)

1. 获取摄像头预览标签(副窗口)对象,建议在mounted里面获取,不能在onLoad里面获取。

mounted() {
    //获取摄像头预览标签对象
    cameraSecondView = this.$refs.cameraSecondView;

    //注意:这里不能调用cameraSecondView的任何接口,等onViewCreated回调之后才能调用标签对象的接口。
}

2. 设置预览控件填充模式,默认居中裁剪。

//设置预览控件填充模式
var mode = 1;// 0:等比缩放居中留黑边  1:等比缩放居中裁剪
cameraSecondView.setRenderMode(mode);

3. 预览(副窗口), 一般在打开相机成功回调onCameraOpen里调用。

//预览(副窗口)
var deviceId = that.deviceId;//设备id
cameraSecondView.addSurface(deviceId);

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问