上传前从Phonegap获取图像宽度和高度(Getting an image width and height from Phonegap before upload)

这应该很容易,但我找不到答案。 我需要获取图像的宽度和高度,通过Phonegap中的fileURI抓取,然后再将其上传到我们的服务器。 当然,这需要一些html5 / Phonegap的魔术,它会在上传之前完成。 这里有一些真正简化的代码来显示我在哪里:

function got_image(image_uri) { // Great, we've got the URI window.resolveLocalFileSystemURI(image_uri, function(fileEntry) { // Great, now we have a fileEntry object. // How do we get the image width and height? }) } // Get the image URI using Phonegap navigator.camera.getPicture(got_image, fail_function, some_settings)

知道遗失的是什么吗? 我希望我有西蒙麦克唐纳或沙兹龙的快速拨号,但我没有。

This should be easy but I can't find the answer. I need to get the width and height of an image, grabbed via a fileURI in Phonegap, before uploading it to our server. Certainly there's got to be some html5 / Phonegap magic that will do this before uploading. Here is some really reduced code to show where I'm at:

function got_image(image_uri) { // Great, we've got the URI window.resolveLocalFileSystemURI(image_uri, function(fileEntry) { // Great, now we have a fileEntry object. // How do we get the image width and height? }) } // Get the image URI using Phonegap navigator.camera.getPicture(got_image, fail_function, some_settings)

Any idea what the missing piece is? I wish I had Simon MacDonald or Shazron on speed dial, but I do not.

最满意答案

这是一个合适的解决方案。

将此函数传递给imageURI。 从PhoneGap的getPicture()方法返回URI。

像这样: get_image_size_from_URI(imageURI)

function get_image_size_from_URI(imageURI) { // This function is called once an imageURI is rerturned from PhoneGap's camera or gallery function window.resolveLocalFileSystemURI(imageURI, function(fileEntry) { fileEntry.file(function(fileObject){ // Create a reader to read the file var reader = new FileReader() // Create a function to process the file once it's read reader.onloadend = function(evt) { // Create an image element that we will load the data into var image = new Image() image.onload = function(evt) { // The image has been loaded and the data is ready var image_width = this.width var image_height = this.height console.log("IMAGE HEIGHT: " + image_height) console.log("IMAGE WIDTH: " + image_width) // We don't need the image element anymore. Get rid of it. image = null } // Load the read data into the image source. It's base64 data image.src = evt.target.result } // Read from disk the data as base64 reader.readAsDataURL(fileObject) }, function(){ console.log("There was an error reading or processing this file.") }) }) }

Here is a proper solution.

Pass this function an imageURI. URIs are returned from both of PhoneGap's getPicture() methods.

Like this: get_image_size_from_URI(imageURI)

function get_image_size_from_URI(imageURI) { // This function is called once an imageURI is rerturned from PhoneGap's camera or gallery function window.resolveLocalFileSystemURI(imageURI, function(fileEntry) { fileEntry.file(function(fileObject){ // Create a reader to read the file var reader = new FileReader() // Create a function to process the file once it's read reader.onloadend = function(evt) { // Create an image element that we will load the data into var image = new Image() image.onload = function(evt) { // The image has been loaded and the data is ready var image_width = this.width var image_height = this.height console.log("IMAGE HEIGHT: " + image_height) console.log("IMAGE WIDTH: " + image_width) // We don't need the image element anymore. Get rid of it. image = null } // Load the read data into the image source. It's base64 data image.src = evt.target.result } // Read from disk the data as base64 reader.readAsDataURL(fileObject) }, function(){ console.log("There was an error reading or processing this file.") }) }) }

更多推荐