Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Générateur de Motif de Carrés</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- }
- .container {
- width: 800px;
- margin: 0 auto;
- text-align: center;
- }
- #pictureBox {
- width: 100%;
- height: 400px;
- border: 1px solid #ccc;
- margin-top: 20px;
- }
- .button {
- margin-top: 20px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>Générateur de Motif de Carrés</h1>
- <input type="file" id="fileInput" class="button">
- <button id="generateButton" class="button" disabled>Générer</button>
- <button id="saveButton" class="button" style="display: none;">Enregistrer</button>
- <textarea id="textBox" rows="10" cols="50" style="margin-top: 20px;" readonly></textarea>
- <canvas id="pictureBox"></canvas>
- </div>
- <script>
- const fileInput = document.getElementById('fileInput');
- const generateButton = document.getElementById('generateButton');
- const saveButton = document.getElementById('saveButton');
- const pictureBox = document.getElementById('pictureBox');
- const textBox = document.getElementById('textBox');
- const canvas = pictureBox.getContext('2d');
- let loadedBitmap = null;
- let binaryText = '';
- fileInput.addEventListener('change', (event) => {
- const file = event.target.files[0];
- if (file) {
- const reader = new FileReader();
- reader.onload = (e) => {
- const image = new Image();
- image.src = e.target.result;
- image.onload = () => {
- pictureBox.width = image.width;
- pictureBox.height = image.height;
- canvas.drawImage(image, 0, 0);
- loadedBitmap = image;
- generateButton.disabled = false;
- };
- };
- reader.readAsDataURL(file);
- }
- });
- generateButton.addEventListener('click', () => {
- if (!loadedBitmap) {
- alert("Veuillez charger une image d'abord.");
- return;
- }
- const imageData = canvas.getImageData(0, 0, pictureBox.width, pictureBox.height);
- binaryText = generateBinaryPattern(imageData);
- textBox.value = binaryText;
- saveButton.style.display = 'block';
- });
- saveButton.addEventListener('click', () => {
- if (!binaryText.trim()) {
- alert("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
- return;
- }
- const binaryBytes = convertBinaryTextToBytes(binaryText);
- const blob = new Blob([binaryBytes], { type: 'application/zip' });
- const a = document.createElement('a');
- const fileName = prompt('Entrez le nom du fichier ZIP :') || 'binary_pattern.zip';
- a.href = URL.createObjectURL(blob);
- a.download = fileName;
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- });
- function generateBinaryPattern(imageData) {
- const threshold = 128;
- let binaryText = '';
- for (let y = 0; y < imageData.height; y++) {
- for (let x = 0; x < imageData.width; x++) {
- const index = (y * imageData.width + x) * 4;
- const grayscale = (imageData.data[index] + imageData.data[index + 1] + imageData.data[index + 2]) / 3;
- binaryText += grayscale < threshold ? '1' : '0';
- }
- binaryText += '\n';
- }
- return binaryText;
- }
- function convertBinaryTextToBytes(binaryText) {
- binaryText = binaryText.replace(/\s+/g, '');
- const numOfBytes = binaryText.length / 8;
- const bytes = new Uint8Array(numOfBytes);
- for (let i = 0; i < numOfBytes; i++) {
- const byteStr = binaryText.substr(i * 8, 8);
- const byteValue = parseInt(byteStr, 2);
- bytes[i] = byteValue;
- }
- return bytes;
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement