Last active
December 7, 2015 12:37
-
-
Save frantic1048/c031892a994e749f1e6c to your computer and use it in GitHub Desktop.
get ImageData(pixel information) directly from HTML Input Element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* use on <input> like this, multiple is optional. | |
* <input | |
* type="file" | |
* multiple | |
* accept="image/*" | |
* onchange={handleFile} | |
* /> | |
*/ | |
function handleFile() { | |
function extractDataAndDoSomething(f) { | |
const canvas = document.createElement('canvas'); | |
const ctx = canvas.getContext('2d'); | |
const img = new Image(); | |
const fr = new FileReader(); | |
img.onload = () => { | |
let _imageData; | |
ctx.drawImage(img, 0, 0); | |
_imageData = ctx.getImageData(0, 0, img.width, img.height); | |
// do anyting you want here with parsed data | |
doSometingWith(_imageData); | |
}; | |
fr.onload = () => { img.src = fr.result; }; | |
fr.readAsDataURL(f); | |
} | |
for (const eachFile of new Array(...this.refs.fileInput.files)) { | |
extractDataAndDoSomething(eachFile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment