2013-02-28 98 views
4

这应该很容易,但我找不到答案。我需要通过Phonegap中的fileURI获取图片的宽度和高度,然后将其上传到我们的服务器。当然,这需要一些html5/Phonegap的魔术,它会在上传之前完成。这里是一些真正减少的代码,以显示我在哪里:上传前从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) 

任何想法是什么缺少的是什么?我希望在快速拨号上有Simon MacDonaldShazron,但我没有。

回答

6

这里做一个妥善的解决办法。

将此函数传递给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.") 
     }) 
    }) 
} 
+0

这个答案大部分适用,但效果并不好。 ,由于内存问题,我得到间歇性应用程序崩溃,我假设这是因为它试图一次读取数据,如果有人有想法,我很乐意找到一个更好的方法PhoneGap文档提示这个问题:“使用Base64对这些图像进行编码已导致许多较新设备出现内存问题。因此,强烈建议使用FILEURI作为'Camera.destinationType'。“http://docs.phonegap.com/en/2.5.0/cordova_camera_camera.md.html#Camera – 2013-03-09 01:25:41

+0

是不是使用FileURI? – 2016-02-22 03:15:03

1

我想你可以使用getPicture()的目标宽度和高度来限制最大尺寸。 如果你需要将它们发送到服务器,我认为服务器代码将有助于获得这些维度。

如果你真的需要通过JS来获得这些尺寸,你可以

var img = new Image; 
img.onload = function(){ 
    myCanvasContext.drawImage(img,0,0); 
}; 
img.src = "data:YOUR_BASE64_DATA"; 

你还可以用IMG

+0

感谢您的回答,但我们不想调整图像大小。即使我们这样做了,也无法确定源图像的高宽比以适当调整它的大小。您的第二个建议不提供从PhoneGap fileEntry对象获取base64数据的方法。 – 2013-02-28 17:35:39

+0

尽管您已经找到了方法已经太迟了,但您可以将{destinationType:Camera.DestinationType.DATA_URL}传递给getPicture方法的param以获取基本64数据而不是获取图像uri。 – Horst 2013-03-01 05:03:47

+0

实际上,这真的很棒。我很感谢你的回答。不幸的是,当我混淆了base64数据时,无论是使用你的方法还是使用我的方法,我都会发生间歇性的应用程序崩溃。 Phonegap文档涉及到这一点,但我不确定适当的选择是什么。 “使用Base64对这些图像进行编码导致许多新设备出现内存问题,因此强烈建议将FILEURI用作'Camera.destinationType'。“ – 2013-03-09 01:24:18

0

1.Phonegap提供了一种特定的图像的大小选择 Click here to see the options

2.如果需要知道原来的大小,不想具体,你可以尝试下面的代码:

function got_image(image_uri) { 
    window.resolveLocalFileSystemURI(image_uri, function(fileEntry) { 
     fileEntry.file(function (fileObj) { 
        var fileName = fileObj.fullPath; 
        var originalLogo = new Image(); 
        originalLogo.src =fileName; 
        //get the size by: originalLogo.width,originalLogo.height 
       }); 
    }) 
} 
+0

我是我的方法得到了一些内存问题,所以我尝试了你的方法,它总是返回0的宽度和高度,有什么想法吗? – 2013-03-09 01:22:30

+0

Ryan,来不及看到你的反馈,但很高兴看到你的解决方案。在图像加载完成后获取新图像对象的大小 – 2013-03-11 01:32:04

0

你可以这样做的一种方法是使用Capture API中的MediaFile。

var mediaFile = new MediaFile("myfile.jpg", "/path/to/myfile.jpg"); 
mediaFile.getFormatData(function(data) { 
    console.log("width: " data.width); 
    console.log("height: " data.height); 
}); 

应该比当前接受的解决方案少得多的代码。

+0

嘿西蒙 - 感谢您的回复。不幸的是,这段代码似乎并不适用于我。我目前正在iOS中测试代码总是返回宽度和高度Shazron在周末提到他认为在没有大量内存冲击的情况下无法安全地获取图片的宽度和高度,我想我可以放弃这一尝试并等待从我的服务器收到回复呃与适当的和高度。虽然不是理想的情况。 – 2013-03-11 17:57:35

+0

是的,它适用于Android,但它似乎不适用于iOS。您可能会等待来自服务器的响应。 – 2013-03-11 18:29:10

3

下面的代码解决了我今天同样的问题(在iOS上测试):

function got_image(image_uri) 
{  
    $('<img src="'+image_uri+'"/>').on('load',function(){ 
     alert(this.naturalWidth +" "+ this.naturalHeight); 
    }); 
} 

希望它能帮助!

+0

这种方法需要考虑的是,ios 9 safari会自然而然的将高度和宽度混合在一起。目前我自己正在遇到这个问题。 – Justin 2015-12-21 19:22:53

相关问题