npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itmirror/uvc-camera-jssdk

v1.1.7

Published

封装uvc-camera安卓方法和上传功能的jssdk

Downloads

17

Readme

@itmirror/uvc-camera-jssdk

js-sdk for uvc-camera,封装安卓端方法调用和上传

安装

npm i @itmirror/uvc-camera-jssdk --save

使用

import uvcCamera from '@itmirror/uvc-camera-jssdk'

// web开发环境调试,需引入下面的测试数据
// 生产环境记得删除
import guaguaData from '@itmirror/uvc-camera-jssdk/lib/guaguaData.js'
uvcCamera.init('camera', { devData: guaguaData });

创建实例

// 第一个参数为父容器的id,需要设置宽高
let myCamera = uvcCamera.init('camera', {
    host: '', // 请求地址
    uploadAction: '/upload', // 上传路径
    successAction: '/success', // 成功路径
    fps: 20, // 帧率
    retry: 3, // 上传重试次数
    fileType: 'base64' // 返回的文件格式
    buttonEvent: function () {} // 硬件按钮触发
}, function () {
    console.log('init success');
})

修改配置

myCamera = uvcCamera.update({
    fps: 15,
    retry: 5,
    fileType: 'file'
})

开启摄像头

myCamera.open(function (json) {}) // 开启后,自动开始传输数据

开始传输数据

myCamera.start()

拍照

myCamera.takePicture(function (data) {
    console.log('图片为:', data)
})

换灯

// 可选值: green, red, blue
myCamera.setLight('red')

重拍上一张

myCamera.prev()

拍下一张

myCamera.next()

上传

myCamera.upload({
    options: {
        timeout: 10000
    },
    success: (ret) => {
        console.log('上传成功!')
    },
    fail: (err, ret) => {
        console.log('啊哦,上传失败了~')
    },
    progress: (percent) => {
        console.log('上传进度: ', percent)
    }
})

取消上传

myCamera.abortUpload()

清除全部文件

myCamera.clear()

停止传输数据

myCamera.stop()

关闭摄像头

myCamera.close()

销毁实例:可能会导致重新请求usb授权,原则上无需销毁

myCamera.destroy()

参数

| 字段 | 默认值 | 类型 | 描述 | | :---------: | :---------: | :---------: | :---------: |
|fps|20|Number|帧率,最大为20|
|retry|3|Number|上传失败重试次数|
|fileType|base64|String|拍照返回的文件类型: base64, file|
|buttonEvent|null|Function|硬件按钮事件|
|host | '' | String| 请求地址 |
|uploadAction|'/api/uploadImg'|String|上传路径|
|successAction|'/api/uploadImgSuccess'|String|上传完成通知路径|
|uploadMultiple|true|Boolean|是否一起上传|

完整示例

// html
<script src="../lib/guaguaData.js"></script>
<script src="../lib/index.js"></script>
// js
<script>
const $result = document.getElementById('result');
const $statusBtn = document.getElementById('statusBtn');
// 初始化
let isInit = false;
let isOpen = false;
let c;
init();

function init(cb = function() {}) {
    c = uvcCamera.init('camera', {
        host: 'http://192.168.61.156:7001', // 上传地址
        uploadAction: '/skinTest/uploadImg', // 上传地址
        successAction: '/skinTest/uploadImgSuccess', // 通知地址
        devData: guaguaData,
        buttonEvent: function() {
            console.log('button-handle');
            takePicture();
        }
    }, () => {
        isInit = true;
        cb();
        console.log('init success');
    });
}

function doChange() {
    if (!isInit) {
        init(changeStatus);
    } else {
        changeStatus();
    }
}

// 开始 or 停止
let isStop = true;
function changeStatus() {
    if (!isOpen) {
        // 开启摄像头
        c.open((json) => {
            if (!json.status) {
                console.log('摄像头开启失败!')
                isStop = true;
                $statusBtn.innerText = '开始';
                return;
            }
            isOpen = true;
            isStop = false;
            $statusBtn.innerText = '停止';
        })
        return
    }

    isStop = !isStop;
    $statusBtn.innerText = isStop ? '开始' : '停止';
    isStop ? c.stop() : c.start();
};

// 换灯
let light_i = 0;
const lights = ['green', 'red', 'blue'];
function changeLight() {
    light_i++;
    light_i = light_i > lights.length - 1 ? 0 : light_i;
    c.setLight(lights[light_i])
}

// 拍照
function takePicture() {
    c.takePicture(function (data) {
        console.log('data is: ', data)
        const img = document.createElement('img');
        img.src = data;
        $result.appendChild(img);

        setTimeout(() => {
            c.next(); // 拍下一张
        }, 1500);
    });
}

// 上传
function upload() {
    c.upload({
        success: function(res) {
            console.log(res)
            console.log('上传成功!')
            c.destroy()
        },
        fail: function(err) {
            console.log(err)
            console.log('上传失败...')
        },
        progress: function(val) {
            console.log(val)
            console.log('上传进度: ', val);
        },
    })
}

// 销毁
function destroy() {
    if (c) {
        c.destroy();
        c = null;
        isStop = true;
        isInit = isOpen = false;
        $statusBtn.innerText = '开始';
    }
}
</script>

注意❗

  • 上传完成会清空所有拍摄的照片,请自行保存
  • 调用open之前需要等init完成
  • 对应安卓方法在 android.md