其他分享
首页 > 其他分享> > 靓仔靓女如何用浏览器自拍和保存

靓仔靓女如何用浏览器自拍和保存

作者:互联网

一、前言

1.核心技术

2.音频采集的基本概念

3.音视频设备的基本原理

模数转换使用的采集定理称为奈奎斯特定理:

在进行模拟 / 数字信号的转换过程中,当采样率大于信号中最高频率的 2 倍时,采样之后的数字信号就完整地保留原始信号中的信息。

talk is cheap, 上代码,以下示例运行的时候会请求摄像头权限,同意即可,接下来就是见证奇迹的时刻!

二、示例

1.示例1-打开摄像头

这就是照像的核心功能,以后可以用来化妆,挤痘痘,整理发型

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>打开摄像头</title>
</head>
<body>
<h1>打开摄像头</h1>
<video autoplay playsinline></video>
</body>
</html>

<script>
    const mediaStreamContrains = {
        video: {
            frameRate: {min: 20},
            width: {min: 640, ideal: 1280},
            height: {min: 360, ideal: 720},
            aspectRatio: 16 / 9
        },
        audio: {
            echoCancellation: true,
            noiseSuppression: true,
            autoGainControl: true
        }
    };

    const localVideo = document.querySelector('video');

    function gotLocalMediaStream(mediaStream) {
        localVideo.srcObject = mediaStream;
    }

    function handleLocalMediaStreamError(error) {
        console.log('navigator.getUserMedia error: ', error);
    }

    navigator.mediaDevices.getUserMedia(mediaStreamContrains).then(
        gotLocalMediaStream
    ).catch(
        handleLocalMediaStreamError
    );
</script>

运行结果如下

示例2-拍照保存

这里展示了两个按钮,拍照和保存,yes,就是自拍的核心功能!

<html>
<head>
    <meta charset="UTF-8">
    <title>拍照一分钟,P图两小时</title>
</head>

<body>
<section>
    <div>
        <video autoplay playsinline id="player"></video>
    </div>

</section>
<section>
    <div>
        <button id="snapshot">拍照</button>
        <button id="download">下载</button>
    </div>
    <div>
        <canvas id="picture"></canvas>
    </div>
</section>
</body>
</html>


<script>
    'use strict';

    var videoplay = document.querySelector('video#player');

    function gotMediaStream(stream) {
        window.stream = stream;
        videoplay.srcObject = stream;
    }

    function handleError(err) {
        console.log('getUserMedia error:', err);
    }

    function start() {
        var constraints = {
            video: {
                width: 1280,
                height: 720,
                frameRate: 15,
                facingMode: 'enviroment'
            },
            audio: false
        }

        navigator.mediaDevices.getUserMedia(constraints)
            .then(gotMediaStream)
            .catch(handleError);
    }


    //拍照
    var snapshot = document.querySelector('button#snapshot');
    snapshot.onclick = function () {
        var picture = document.querySelector('canvas#picture');
        picture.width = 1280;
        picture.height = 720;
        picture.getContext('2d').drawImage(videoplay, 0, 0, picture.width, picture.height);
    };


    //下载
    function downLoad(url) {
        var oA = document.createElement("a");
        oA.download = 'photo';// 设置下载的文件名,默认是'下载'
        oA.href = url;
        document.body.appendChild(oA);
        oA.click();
        oA.remove(); // 下载之后把创建的元素删除
    }

    document.querySelector("button#download").onclick = function () {
        downLoad(picture.toDataURL("image/jpeg"));
    };

    start();

</script>

运行结果如下

就是这么简单!

重点方法和参数解释

http://market.szonline.net/amaz/26366.html
http://market.szonline.net/amaz/26365.html
http://market.szonline.net/amaz/26364.html
http://market.szonline.net/amaz/26363.html
http://market.szonline.net/amaz/26362.html

标签:function,picture,采集,靓女,音频,oA,靓仔,自拍,document
来源: https://www.cnblogs.com/cider/p/11961238.html