Advertisement
Thunder-Menu

DecodeBmp.html

Aug 15th, 2023
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.59 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Générateur de Motif de Carrés</title>
  7.     <style>
  8.         body {
  9.             font-family: Arial, sans-serif;
  10.         }
  11.         .container {
  12.             width: 800px;
  13.             margin: 0 auto;
  14.             text-align: center;
  15.         }
  16.         #pictureBox {
  17.             width: 100%;
  18.             height: 400px;
  19.             border: 1px solid #ccc;
  20.             margin-top: 20px;
  21.         }
  22.         .button {
  23.             margin-top: 20px;
  24.         }
  25.     </style>
  26. </head>
  27. <body>
  28.     <div class="container">
  29.         <h1>Générateur de Motif de Carrés</h1>
  30.         <input type="file" id="fileInput" class="button">
  31.         <button id="generateButton" class="button" disabled>Générer</button>
  32.         <button id="saveButton" class="button" style="display: none;">Enregistrer</button>
  33.         <textarea id="textBox" rows="10" cols="50" style="margin-top: 20px;" readonly></textarea>
  34.         <canvas id="pictureBox"></canvas>
  35.     </div>
  36.     <script>
  37.         const fileInput = document.getElementById('fileInput');
  38.         const generateButton = document.getElementById('generateButton');
  39.         const saveButton = document.getElementById('saveButton');
  40.         const pictureBox = document.getElementById('pictureBox');
  41.         const textBox = document.getElementById('textBox');
  42.         const canvas = pictureBox.getContext('2d');
  43.         let loadedBitmap = null;
  44.         let binaryText = '';
  45.  
  46.         fileInput.addEventListener('change', (event) => {
  47.             const file = event.target.files[0];
  48.             if (file) {
  49.                 const reader = new FileReader();
  50.                 reader.onload = (e) => {
  51.                     const image = new Image();
  52.                     image.src = e.target.result;
  53.                     image.onload = () => {
  54.                         pictureBox.width = image.width;
  55.                         pictureBox.height = image.height;
  56.                         canvas.drawImage(image, 0, 0);
  57.                         loadedBitmap = image;
  58.                         generateButton.disabled = false;
  59.                     };
  60.                 };
  61.                 reader.readAsDataURL(file);
  62.             }
  63.         });
  64.  
  65.         generateButton.addEventListener('click', () => {
  66.             if (!loadedBitmap) {
  67.                 alert("Veuillez charger une image d'abord.");
  68.                 return;
  69.             }
  70.  
  71.             const imageData = canvas.getImageData(0, 0, pictureBox.width, pictureBox.height);
  72.             binaryText = generateBinaryPattern(imageData);
  73.             textBox.value = binaryText;
  74.             saveButton.style.display = 'block';
  75.         });
  76.  
  77.         saveButton.addEventListener('click', () => {
  78.             if (!binaryText.trim()) {
  79.                 alert("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
  80.                 return;
  81.             }
  82.  
  83.             const binaryBytes = convertBinaryTextToBytes(binaryText);
  84.             const blob = new Blob([binaryBytes], { type: 'application/zip' });
  85.             const a = document.createElement('a');
  86.             const fileName = prompt('Entrez le nom du fichier ZIP :') || 'binary_pattern.zip';
  87.             a.href = URL.createObjectURL(blob);
  88.             a.download = fileName;
  89.             document.body.appendChild(a);
  90.             a.click();
  91.             document.body.removeChild(a);
  92.         });
  93.  
  94.         function generateBinaryPattern(imageData) {
  95.             const threshold = 128;
  96.             let binaryText = '';
  97.  
  98.             for (let y = 0; y < imageData.height; y++) {
  99.                for (let x = 0; x < imageData.width; x++) {
  100.                    const index = (y * imageData.width + x) * 4;
  101.                    const grayscale = (imageData.data[index] + imageData.data[index + 1] + imageData.data[index + 2]) / 3;
  102.  
  103.                    binaryText += grayscale < threshold ? '1' : '0';
  104.                }
  105.                binaryText += '\n';
  106.            }
  107.  
  108.            return binaryText;
  109.        }
  110.  
  111.        function convertBinaryTextToBytes(binaryText) {
  112.            binaryText = binaryText.replace(/\s+/g, '');
  113.            const numOfBytes = binaryText.length / 8;
  114.            const bytes = new Uint8Array(numOfBytes);
  115.  
  116.            for (let i = 0; i < numOfBytes; i++) {
  117.                const byteStr = binaryText.substr(i * 8, 8);
  118.                const byteValue = parseInt(byteStr, 2);
  119.                bytes[i] = byteValue;
  120.            }
  121.  
  122.            return bytes;
  123.        }
  124.    </script>
  125. </body>
  126. </html>
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement