<!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>