| <!DOCTYPE html> |
| <html lang="fa" dir="rtl"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>کلاینت QR Code</title> |
| <style> |
| body { |
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; |
| background-color: #f0f2f5; |
| color: #1c1e21; |
| display: flex; |
| justify-content: center; |
| align-items: flex-start; |
| padding: 20px; |
| gap: 30px; |
| flex-wrap: wrap; |
| } |
| .container { |
| background-color: #fff; |
| padding: 25px; |
| border-radius: 8px; |
| box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); |
| width: 400px; |
| display: flex; |
| flex-direction: column; |
| gap: 15px; |
| } |
| h2 { |
| margin: 0 0 10px 0; |
| color: #0056b3; |
| border-bottom: 2px solid #0056b3; |
| padding-bottom: 5px; |
| } |
| textarea, input[type="file"] { |
| width: 100%; |
| padding: 10px; |
| border: 1px solid #ccc; |
| border-radius: 4px; |
| box-sizing: border-box; |
| resize: vertical; |
| } |
| button { |
| background-color: #007bff; |
| color: white; |
| padding: 12px 20px; |
| border: none; |
| border-radius: 4px; |
| cursor: pointer; |
| font-size: 16px; |
| transition: background-color 0.3s; |
| } |
| button:disabled { |
| background-color: #a0c9ff; |
| cursor: not-allowed; |
| } |
| button:hover:not(:disabled) { |
| background-color: #0056b3; |
| } |
| .result-box { |
| margin-top: 15px; |
| padding: 15px; |
| border: 1px dashed #ddd; |
| border-radius: 4px; |
| min-height: 50px; |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| background-color: #fafafa; |
| } |
| #qr-image { |
| max-width: 100%; |
| height: auto; |
| } |
| .loader { |
| border: 4px solid #f3f3f3; |
| border-radius: 50%; |
| border-top: 4px solid #3498db; |
| width: 30px; |
| height: 30px; |
| animation: spin 1s linear infinite; |
| } |
| @keyframes spin { |
| 0% { transform: rotate(0deg); } |
| 100% { transform: rotate(360deg); } |
| } |
| .hidden { |
| display: none; |
| } |
| </style> |
| </head> |
| <body> |
|
|
| |
| <div class="container"> |
| <h2>ساخت QR Code</h2> |
| <textarea id="text-input" placeholder="متن مورد نظر را اینجا وارد کنید..." rows="4">دنیا</textarea> |
| <button id="generate-btn" onclick="generateQRCode()">ساخت کد</button> |
| <div id="generate-result" class="result-box"> |
| <div id="generate-loader" class="loader hidden"></div> |
| <img id="qr-image" src="" alt="QR Code" class="hidden" /> |
| <span id="generate-placeholder">تصویر QR Code اینجا نمایش داده میشود.</span> |
| </div> |
| </div> |
|
|
| |
| <div class="container"> |
| <h2>خواندن QR Code</h2> |
| <input type="file" id="file-input" accept="image/*"> |
| <button id="read-btn" onclick="readQRCode()">خواندن کد</button> |
| <div id="read-result" class="result-box"> |
| <div id="read-loader" class="loader hidden"></div> |
| <p id="read-text">نتیجه خواندن کد اینجا نمایش داده میشود.</p> |
| </div> |
| </div> |
|
|
| <script> |
| const API_BASE_URL = "https://cultrix-qrcode-read-generate.hf.space/gradio_api/"; |
| |
| |
| function generateSessionHash() { |
| return Math.random().toString(36).substring(2, 11); |
| } |
| |
| |
| async function callGradioApi(fnIndex, data, sessionHash) { |
| |
| const joinResponse = await fetch(API_BASE_URL + "queue/join?", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ |
| "fn_index": fnIndex, |
| "data": data, |
| "session_hash": sessionHash, |
| }), |
| }); |
| if (!joinResponse.ok) throw new Error("Failed to join queue."); |
| |
| |
| return new Promise((resolve, reject) => { |
| const interval = setInterval(async () => { |
| try { |
| const dataResponse = await fetch(API_BASE_URL + `queue/data?session_hash=${sessionHash}`); |
| if (!dataResponse.ok) { |
| clearInterval(interval); |
| reject(new Error("Failed to get data.")); |
| return; |
| } |
| const result = await dataResponse.json(); |
| |
| |
| if (result.msg === "process_completed") { |
| clearInterval(interval); |
| resolve(result); |
| } else if (result.msg === "queue_full" || result.msg === "unexpected_error") { |
| clearInterval(interval); |
| reject(new Error("API Error: " + result.msg)); |
| } |
| |
| } catch (error) { |
| clearInterval(interval); |
| reject(error); |
| } |
| }, 1000); |
| }); |
| } |
| |
| |
| async function generateQRCode() { |
| const textInput = document.getElementById('text-input').value; |
| if (!textInput.trim()) { |
| alert("لطفاً متنی را برای ساخت QR Code وارد کنید."); |
| return; |
| } |
| |
| const generateBtn = document.getElementById('generate-btn'); |
| const loader = document.getElementById('generate-loader'); |
| const placeholder = document.getElementById('generate-placeholder'); |
| const qrImage = document.getElementById('qr-image'); |
| |
| generateBtn.disabled = true; |
| loader.classList.remove('hidden'); |
| placeholder.classList.add('hidden'); |
| qrImage.classList.add('hidden'); |
| |
| try { |
| const sessionHash = generateSessionHash(); |
| const data = [textInput]; |
| const result = await callGradioApi(0, data, sessionHash); |
| |
| if (result.output && result.output.data && result.output.data[0]) { |
| const imageUrl = result.output.data[0].url; |
| qrImage.src = imageUrl; |
| qrImage.classList.remove('hidden'); |
| } else { |
| throw new Error("پاسخ دریافتی از سرور معتبر نیست."); |
| } |
| |
| } catch (error) { |
| console.error("Error generating QR Code:", error); |
| alert("خطا در ساخت QR Code: " + error.message); |
| placeholder.classList.remove('hidden'); |
| } finally { |
| generateBtn.disabled = false; |
| loader.classList.add('hidden'); |
| } |
| } |
| |
| |
| async function readQRCode() { |
| const fileInput = document.getElementById('file-input'); |
| if (fileInput.files.length === 0) { |
| alert("لطفاً یک فایل تصویر QR Code انتخاب کنید."); |
| return; |
| } |
| const file = fileInput.files[0]; |
| |
| const readBtn = document.getElementById('read-btn'); |
| const loader = document.getElementById('read-loader'); |
| const readText = document.getElementById('read-text'); |
| |
| readBtn.disabled = true; |
| loader.classList.remove('hidden'); |
| readText.textContent = "در حال پردازش..."; |
| |
| try { |
| |
| const formData = new FormData(); |
| formData.append("files", file); |
| const uploadResponse = await fetch(API_BASE_URL + "upload", { |
| method: "POST", |
| body: formData, |
| }); |
| if (!uploadResponse.ok) throw new Error("آپلود فایل با خطا مواجه شد."); |
| |
| const uploadResult = await uploadResponse.json(); |
| const tempFilePath = uploadResult[0]; |
| |
| |
| const sessionHash = generateSessionHash(); |
| const data = [{ |
| "path": tempFilePath, |
| "url": null, |
| "orig_name": file.name, |
| "size": file.size, |
| "mime_type": file.type, |
| "meta": {"_type": "gradio.FileData"} |
| }]; |
| const result = await callGradioApi(1, data, sessionHash); |
| |
| if (result.output && result.output.data && typeof result.output.data[0] === 'string') { |
| readText.textContent = "نتیجه: " + result.output.data[0]; |
| } else { |
| readText.textContent = "نتیجهای یافت نشد یا فرمت پاسخ نامعتبر است."; |
| } |
| |
| } catch (error) { |
| console.error("Error reading QR Code:", error); |
| readText.textContent = "خطا در خواندن QR Code: " + error.message; |
| } finally { |
| readBtn.disabled = false; |
| loader.classList.add('hidden'); |
| } |
| } |
| </script> |
|
|
| </body> |
| </html> |