Note that there are some explanatory texts on larger screens.

plurals
  1. POSave captured png as arraybuffer
    primarykey
    data
    text
    <p>I'm trying to save an image to dropbox, and having trouble getting the convertion correct. I have an img (captured using <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/API/Camera" rel="nofollow">this</a> sample) and I want to store it to dropbox that accepts an ArrayBuffer (sample <a href="https://github.com/dropbox/dropbox-js/blob/stable/guides/snippets.md" rel="nofollow">here</a>)</p> <p>This is the code I found that should to the two conversions, first to a base64, then into a ArrayBuffer</p> <pre><code> function getBase64Image(img) { // Create an empty canvas element var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; // Copy the image contents to the canvas var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); // Get the data-URL formatted image // Firefox supports PNG and JPEG. You could check img.src to // guess the original format, but be aware the using "image/jpg" // will re-encode the image. var dataURL = canvas.toDataURL("image/png"); return dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); } function base64ToArrayBuffer(string_base64) { var binary_string = window.atob(string_base64); var len = binary_string.length; var bytes = new Uint8Array(len); for (var i = 0; i &lt; len; i++) { var ascii = binary_string.charCodeAt(i); bytes[i] = ascii; } return bytes.buffer; } </code></pre> <p>Saving is started like this</p> <pre><code> var img = $('#show-picture')[0]; var data = base64ToArrayBuffer( getBase64Image(img)); dropbox.client.writeFile(moment().format('YYYYMMDD-HH-mm-ss')+'.png', data, function (error, stat) { if (error) { return dropbax.handleError(error); } // The image has been succesfully written. }); </code></pre> <p>Problem is that I get a corrupted file saved, and is a bit confused on what's wrong.</p> <p>*<em>EDIT *</em></p> <p>Here's the link to the original file <a href="https://www.dropbox.com/s/ekyhvu2t6d8ldh3/original.PNG" rel="nofollow">https://www.dropbox.com/s/ekyhvu2t6d8ldh3/original.PNG</a> and here to the corrupted. <a href="https://www.dropbox.com/s/f0oevj1z33brpur/20131219-22-23-14.png" rel="nofollow">https://www.dropbox.com/s/f0oevj1z33brpur/20131219-22-23-14.png</a></p> <p>I'm using this version of the dropbox.js: //cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.2/dropbox.min.js</p> <p>As you can see the corrupted is slighty bigger 23,3KB vs 32,6 KB Thanks for any help</p> <p>Larsi </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload