Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR Code Scanner #149

Merged
merged 2 commits into from Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions QR Code Scanner/index.html
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css"
/>
<link rel="stylesheet" href="style.css" />
<title>QR Code Scanner</title>
</head>
<body>
<div class="wrapper">
<form action="#" id="form1">
<input type="file" id="file" hidden>
<img src="" alt="qr-code">
<div class="content">
<i class="fas fa-cloud-upload"></i>
<p id="ptext">Upload QR Code to Scan</p>
</div>
</form>
<div class="details">
<textarea disabled id="textArea"></textarea>
<div class="buttons">
<button class="close">Close</button>
<button class="copy">Copy Text</button>
</div>
</div>
</div>

<div id="toasts"></div>
<script src="index.js"></script>
</body>
</html>
63 changes: 63 additions & 0 deletions QR Code Scanner/index.js
@@ -0,0 +1,63 @@
const wrapper = document.querySelector('.wrapper')
const form = document.querySelector('#form1')
const fileInput = document.querySelector('#file')
const infoText = document.getElementById('ptext')
const textArea = document.getElementById('textArea')
const copyBtn = document.querySelector('.copy')
const closeBtn = document.querySelector('.close')

const message = "Copied To Clipboard"

function createNotification(messagetext = message) {
const notif = document.createElement('div')
notif.classList.add('toast')
notif.innerText = messagetext
toasts.appendChild(notif)

setTimeout(()=>{
notif.remove()
},2500)
}

function fetchRequest(formData, file){
infoText.innerText = "Scanning QR Code..."
fetch("http://api.qrserver.com/v1/read-qr-code/",{
method: "POST", body: formData
}).then(res => res.json()).then(result =>{
result = result[0].symbol[0].data
infoText.innerText = result ? "Upload QR Code to Scan" : "Couldn't Scan QR Code"
if(!result) return
textArea.innerText = result
form.querySelector("img").src = URL.createObjectURL(file)
wrapper.classList.add("active")
// console.log(result);
}).catch(()=>{
infoText.innerText = "Couldn't Scan QR Code"
})
}

fileInput.addEventListener('change',e => {
let file = e.target.files[0];
if(!file) return;
let formData = new FormData()
formData.append("file",file)
fetchRequest(formData,file)
})


copyBtn.addEventListener("click",()=>{
let text = textArea.textContent;
navigator.clipboard.writeText(text)
createNotification()
})

form.addEventListener('click',()=>{
fileInput.click()
})

closeBtn.addEventListener("click",()=>{
wrapper.classList.remove("active")
setTimeout(()=>{
window.location.reload()
},550)
})
151 changes: 151 additions & 0 deletions QR Code Scanner/style.css
@@ -0,0 +1,151 @@
@import url('https://fonts.googleapis.com/css2?family=Muli&display=swap');

* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Muli', sans-serif;
}

body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background-image: linear-gradient(#AD1DEB,#6E72FC)
}

.wrapper {
height: 270px;
width: 420px;
border-radius: 7px;
background-image: linear-gradient(to right,#AD1DEB,#6E72FC);
padding: 30px 30px 35px;
transition: height 0.4s ease;
}

.wrapper.active{
height: 525px;
}

.wrapper form{
background: #fff;
height: 210px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
border-radius: 7px;
}

.wrapper.active form{
height: 225px;
pointer-events: none;
}

form img{
display: none;
max-width: 150px;
}

.wrapper.active form img{
display: block;
}

.wrapper.active form .content{
display: none;
}

form .content i{
color: #AD1DEB;
/* background: linear-gradient(to right,#AD1DEB,#6E72FC); */
font-size: 55px;
}

form .content p{
color: #6E72FC;
font-size: 1rem;
margin-top: 1rem;
}

.wrapper .details{
margin-top: 25px;
opacity: 0;
pointer-events: none;
}

.wrapper.active .details{
/* margin-top: 25px; */
opacity: 1;
pointer-events: auto;
transition: opacity 0.5s 0.5s ease;
}

#toasts {
position: fixed;
bottom: 10px;
right: 10px;
display: flex;
flex-direction: column;
align-items: flex-end;
}

.toast {
background-color: #fff;
color: rebeccapurple;
border-radius: 5px;
padding: 1rem 2rem;
color: rgb(38, 230, 38);
margin: 0.5rem;
}


.details textarea{
width: 100%;
height: 100px;
padding: 10px 15px;
outline: none;
background: none;
border: 1px solid #fff;
color: #fff;
font-size: 20px;
border-radius: 5px;
}

.details .buttons {
display: flex;
margin-top: 20px;
justify-content: space-between;
}

.buttons button {
outline: none;
border: none;
color: #AD1DEB;
cursor: pointer;
font-size: 1.2rem;
font-weight: bold;
border-radius: 5px;
background: #fff;
height: 55px;
width: calc(100% / 2 - 10px);
}

@media (max-width: 410px) {
.wrapper {
max-width: 350px;
}
}

@media (max-width: 385px) {
.wrapper {
max-width: 320px;
}
}

@media (max-width: 335px) {
.wrapper {
max-width: 295px;
}
}