Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
4
Indexable
private readFileAsUint8Array(file: File) : Promise<Uint8Array> {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
  
      reader.onload = (e) => {
        const img = new Image();
  
        img.onload = () => {
          const canvas = document.createElement('canvas');
          const ctx = canvas.getContext('2d');
          
          canvas.width = img.width;
          canvas.height = img.height;
  
          ctx!.drawImage(img, 0, 0);
          console.log("Jere");
  
          // Get the raw pixel data from the canvas
          const imageData = ctx!.getImageData(0, 0, canvas.width, canvas.height).data;
  
          // Convert the pixel data to Uint8Array
          const uint8Array = new Uint8Array(imageData);
  
          // Resolve the promise with the Uint8Array
          resolve(uint8Array);
        };
      };
      
      console.log("Failed to retreive image");
      // Reject the promise if there is an error
      reader.onerror = (error) => reject(error);
    });
  }
Editor is loading...
Leave a Comment