1*c8dee2aaSAndroid Build Coastguard Worker# ImageDecoder API 2*c8dee2aaSAndroid Build Coastguard Worker 3*c8dee2aaSAndroid Build Coastguard WorkerDate Updated: June 16, 2020 4*c8dee2aaSAndroid Build Coastguard Worker 5*c8dee2aaSAndroid Build Coastguard Worker## Summary and Links 6*c8dee2aaSAndroid Build Coastguard Worker 7*c8dee2aaSAndroid Build Coastguard WorkerThe [ImageDecoder API](https://github.com/dalecurtis/image-decoder-api/blob/master/explainer.md) 8*c8dee2aaSAndroid Build Coastguard Workerhandles decoding of both still and animated images. 9*c8dee2aaSAndroid Build Coastguard WorkerSimilar to the larger [web codecs](https://github.com/WICG/web-codecs/blob/master/explainer.md) 10*c8dee2aaSAndroid Build Coastguard Workerproposal which is focused more on video and audio. 11*c8dee2aaSAndroid Build Coastguard WorkerThe ImageDecoder API could be used with `CanvasKit.MakeImageFromCanvasImageSource` 12*c8dee2aaSAndroid Build Coastguard Workerfor creating CanvasKit compatible `SkImage`s. 13*c8dee2aaSAndroid Build Coastguard WorkerFor still images, the `createImageBitmap(blob)` API achieves the same result. 14*c8dee2aaSAndroid Build Coastguard Worker 15*c8dee2aaSAndroid Build Coastguard Worker- [Explainer](https://github.com/dalecurtis/image-decoder-api/blob/master/explainer.md) 16*c8dee2aaSAndroid Build Coastguard Worker- [Prototype](https://chromium-review.googlesource.com/c/chromium/src/+/2145133) 17*c8dee2aaSAndroid Build Coastguard Worker- [Discourse](https://discourse.wicg.io/t/proposal-imagedecoder-api-extension-for-webcodecs/4418) 18*c8dee2aaSAndroid Build Coastguard Worker 19*c8dee2aaSAndroid Build Coastguard WorkerCurrently available as a prototype behind the `--enable-blink-features=WebCodecs` flag 20*c8dee2aaSAndroid Build Coastguard Workerin Chrome Canary, works in version 85.0.4175.0. 21*c8dee2aaSAndroid Build Coastguard Worker 22*c8dee2aaSAndroid Build Coastguard Worker## Running the prototype 23*c8dee2aaSAndroid Build Coastguard Worker 24*c8dee2aaSAndroid Build Coastguard Worker1. Download and install [Chrome Canary](https://www.google.com/chrome/canary/). Verify that you 25*c8dee2aaSAndroid Build Coastguard Workerhave version 85.0.4175.0 or later. 26*c8dee2aaSAndroid Build Coastguard Worker2. Close ALL open instances of chromium browsers, including chrome. 27*c8dee2aaSAndroid Build Coastguard Worker2. Run Chrome Canary with the `--enable-blink-features=WebCodecs` flag. 28*c8dee2aaSAndroid Build Coastguard Worker 29*c8dee2aaSAndroid Build Coastguard Worker**MacOS**: Run `/applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --enable-blink-features=WebCodecs` 30*c8dee2aaSAndroid Build Coastguard Worker 31*c8dee2aaSAndroid Build Coastguard Worker**Windows, Linux**: [https://www.chromium.org/developers/how-tos/run-chromium-with-flags](https://www.chromium.org/developers/how-tos/run-chromium-with-flags) 32*c8dee2aaSAndroid Build Coastguard Worker 33*c8dee2aaSAndroid Build Coastguard Worker3. Navigate to: [http://storage.googleapis.com/dalecurtis/test-gif.html?src=giphy.gif](http://storage.googleapis.com/dalecurtis/test-gif.html?src=giphy.gif) 34*c8dee2aaSAndroid Build Coastguard Worker4. You should see a cute animated cat illustration. 35*c8dee2aaSAndroid Build Coastguard Worker 36*c8dee2aaSAndroid Build Coastguard Worker## Example API Usage with CanvasKit 37*c8dee2aaSAndroid Build Coastguard Worker 38*c8dee2aaSAndroid Build Coastguard WorkerWith a still image: 39*c8dee2aaSAndroid Build Coastguard Worker```jsx 40*c8dee2aaSAndroid Build Coastguard Workerconst response = await fetch(stillImageUrl); // e.g. png or jpeg 41*c8dee2aaSAndroid Build Coastguard Workerconst data = await response.arrayBuffer(); 42*c8dee2aaSAndroid Build Coastguard Worker 43*c8dee2aaSAndroid Build Coastguard Workerconst imageDecoder = new ImageDecoder({ data }); 44*c8dee2aaSAndroid Build Coastguard Workerconst imageBitmap = await imageDecoder.decode(); 45*c8dee2aaSAndroid Build Coastguard Worker 46*c8dee2aaSAndroid Build Coastguard Workerconst skImage = CanvasKit.MakeImageFromCanvasImageSource(imageBitmap); 47*c8dee2aaSAndroid Build Coastguard Worker// do something with skImage, such as drawing it 48*c8dee2aaSAndroid Build Coastguard Worker``` 49*c8dee2aaSAndroid Build Coastguard Worker 50*c8dee2aaSAndroid Build Coastguard WorkerWith an animated image: 51*c8dee2aaSAndroid Build Coastguard Worker```jsx 52*c8dee2aaSAndroid Build Coastguard Workerconst response = await fetch(animatedImageUrl); // e.g. gif or mjpeg 53*c8dee2aaSAndroid Build Coastguard Workerconst data = await response.arrayBuffer(); 54*c8dee2aaSAndroid Build Coastguard Worker 55*c8dee2aaSAndroid Build Coastguard Workerconst imageDecoder = new ImageDecoder({ data }); 56*c8dee2aaSAndroid Build Coastguard Worker 57*c8dee2aaSAndroid Build Coastguard Workerfor (let frame = 0; frame < imageDecoder.frameCount; frame++) { 58*c8dee2aaSAndroid Build Coastguard Worker const imageBitmap = await imageDecoder.decode(frame); 59*c8dee2aaSAndroid Build Coastguard Worker const skImage = CanvasKit.MakeImageFromCanvasImageSource(imageBitmap); 60*c8dee2aaSAndroid Build Coastguard Worker // do something with skImage, such as drawing it 61*c8dee2aaSAndroid Build Coastguard Worker} 62*c8dee2aaSAndroid Build Coastguard Worker``` 63