<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Code Decoder Tool</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/@zxing/library@0.19.0/umd/index.min.js"></script>
<style>
body {
background: linear-gradient(to right, #ff9a9e, #fad0c4);
color: #333;
font-family: Arial, sans-serif;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
.container {
background: #ffffff;
padding: 20px;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
max-width: 500px;
width: 90%;
}
.btn-primary {
background: #6a11cb;
background: -webkit-linear-gradient(to right, #6a11cb, #2575fc);
background: linear-gradient(to right, #6a11cb, #2575fc);
border: none;
}
.btn-primary:hover {
opacity: 0.9;
}
#video {
width: 100%;
border-radius: 10px;
margin-top: 10px;
}
.result {
margin-top: 15px;
font-size: 1.2rem;
font-weight: bold;
color: #2575fc;
text-align: center;
word-break: break-word;
}
</style>
</head>
<body>
<div class="container text-center">
<h1 class="mb-4">QR Code Decoder</h1>
<div>
<video id="video" autoplay muted playsinline></video>
</div>
<div class="mt-4">
<button id="startButton" class="btn btn-primary mb-2">Start Camera</button>
<button id="stopButton" class="btn btn-secondary mb-2">Stop Camera</button>
</div>
<div class="result" id="result">Scan a QR code to see the result here!</div>
</div>
<script>
const codeReader = new ZXing.BrowserQRCodeReader();
const videoElement = document.getElementById('video');
const resultElement = document.getElementById('result');
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
let stream = null;
async function startCamera() {
try {
const devices = await codeReader.listVideoInputDevices();
if (devices.length === 0) {
resultElement.textContent = 'No camera found!';
return;
}
stream = await navigator.mediaDevices.getUserMedia({ video: { deviceId: devices[0].deviceId } });
videoElement.srcObject = stream;
codeReader.decodeFromVideoDevice(devices[0].deviceId, videoElement, (result, err) => {
if (result) {
resultElement.textContent = `Decoded Text: ${result.text}`;
} else if (err && !(err instanceof ZXing.NotFoundException)) {
console.error(err);
}
});
} catch (err) {
console.error(err);
resultElement.textContent = 'Error starting camera!';
}
}
function stopCamera() {
if (stream) {
const tracks = stream.getTracks();
tracks.forEach(track => track.stop());
stream = null;
videoElement.srcObject = null;
codeReader.reset();
resultElement.textContent = 'Camera stopped.';
}
}
startButton.addEventListener('click', startCamera);
stopButton.addEventListener('click', stopCamera);
</script>
</body>
</html>