HTML调用相机权限拍照代码

<!DOCTYPE html>

<html lang="zh">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>拍照</title>

    <style>

        .container {

            position: relative;

        }

        video, canvas {

            display: block;

            width: 640px;

            height: 480px;

        }

        .controls {

            position: absolute;

            top: 10px;

            left: 10px;

            background: rgba(0, 0, 0, 0.5);

            padding: 10px;

            border-radius: 5px;

        }

        button, input[type="range"] {

            margin: 5px;

        }

        button {

            background: #555;

            border: none;

            color: #fff;

            padding: 10px;

            border-radius: 5px;

            cursor: pointer;

        }

        button:hover {

            background: #777;

        }

        input[type="range"] {

            width: 100px;

        }

    </style>

</head>

<body>

    <div class="container">

        <video id="video" autoplay></video>

        <canvas id="canvas"></canvas>

        <div class="controls">

            <button id="capture">拍照</button>

            <button id="save">保存</button>

            <button id="toggleCamera">切换摄像头</button>

            <label>亮度:

                <input type="range" id="brightness" min="0" max="2" step="0.1" value="1">

            </label>

            <label>对比度:

                <input type="range" id="contrast" min="0" max="2" step="0.1" value="1">

            </label>

            <label>饱和度:

                <input type="range" id="saturation" min="0" max="2" step="0.1" value="1">

            </label>

            <label>色调:

                <input type="range" id="hue-rotate" min="0" max="360" step="1" value="0">

            </label>

        </div>

    </div>

    <script>

        let currentStream;

        let currentCamera = 0;

 

        const video = document.getElementById('video');

        const canvas = document.getElementById('canvas');

        const ctx = canvas.getContext('2d');

        const captureButton = document.getElementById('capture');

        const saveButton = document.getElementById('save');

        const toggleCameraButton = document.getElementById('toggleCamera');

        const brightnessInput = document.getElementById('brightness');

        const contrastInput = document.getElementById('contrast');

        const saturationInput = document.getElementById('saturation');

        const hueRotateInput = document.getElementById('hue-rotate');

 

        async function startCamera() {

            if (currentStream) {

                currentStream.getTracks().forEach(track => track.stop());

            }

            const constraints = {

                video: { facingMode: currentCamera ? 'environment' : 'user' }

            };

            currentStream = await navigator.mediaDevices.getUserMedia(constraints);

            video.srcObject = currentStream;

        }

 

        function applyFilters() {

            const brightness = brightnessInput.value;

            const contrast = contrastInput.value;

            const saturation = saturationInput.value;

            const hueRotate = hueRotateInput.value;

            

            video.style.filter = `brightness(${brightness}) contrast(${contrast}) saturate(${saturation}) hue-rotate(${hueRotate}deg)`;

        }

 

        captureButton.addEventListener('click', () => {

            canvas.width = video.videoWidth;

            canvas.height = video.videoHeight;

            ctx.drawImage(video, 0, 0);

        });

 

        saveButton.addEventListener('click', () => {

            const link = document.createElement('a');

            link.href = canvas.toDataURL('image/png');

            link.download = 'photo.png';

            link.click();

        });

 

        toggleCameraButton.addEventListener('click', () => {

            currentCamera = 1 - currentCamera;

            startCamera();

        });

 

        brightnessInput.addEventListener('input', applyFilters);

        contrastInput.addEventListener('input', applyFilters);

        saturationInput.addEventListener('input', applyFilters);

        hueRotateInput.addEventListener('input', applyFilters);

 

        startCamera();

    </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值