2015-10-06 66 views
0

我一直在这方面努力了一段时间。Phonegap camera.getPicture()缺少文件扩展名和标题数据

当我用camera.getPicture()和ft.upload()在phonegap应用程序中上传图像时,图像上传时没有文件扩展名。我读它是因为缓存的事情,提供了一个链接到实际的文件入口什么的。

这是令人讨厌的,但我继续前进,因为该图像在我的服务器上正常上传,即使没有文件扩展名也可以很好地显示。

但今天,我们认为图像有时会旋转90°。

我立即在图像数据的缺失部分和这个问题之间建立了联系,我猜(我不确定)在这一点上我是对的。

我读旋转90°,可以通过缺少的头元数据引起的图像,所以我想不仅是文件扩展名失踪毕竟..

有人能解释我什么我在代码中失踪,该做什么或朝哪个方向看?那将是真棒。

这里是我的代码部分(我可以给你更多的如果需要的话)

navigator.camera.getPicture(function(uri) { 
    try { 
    var imageURI = uri; 
    ... 
    var ft = new FileTransfer(); 
    ft.upload(imageURI, "some_script.php", function(r) { 
     ... 

注:存储在数据库中的图像似乎罚款,当显示在标签图像的问题发生。

这里有一个文件获取旋转一次上传的例子(我手动添加了.jpg扩展名,所以我可以上传它在noelshack否则不能)。正如你所看到的,链接图像是确定的,但一旦在标记它被旋转

TL;博士

如何上传图片文件完全与phonegap包括文件扩展名&元数据标题,而不仅仅是一种缓存的文件条目。

回答

1

的iOS代码

function capturePhoto() { 
    navigator.camera.getPicture(uploadPhoto, onFail, { 
        quality: 50, 
        // allowEdit: true, 
        correctOrientation: true, 
        destinationType: Camera.DestinationType.FILE_URL, 
        // destinationType: Camera.DestinationType.DATA_URL 
        sourceType: Camera.PictureSourceType.CAMERA 
        } 
              ); 
    } 

    // function onPhotoDataSuccess(imageData) { 
    // localStorage.setItem("ImageData",imageData); 
    // localStorage.setItem("captureImgFlag",captureImgFlag); 
    // window.location = 'profileUserImgUploadInGallary.html'; 
    // } 

    function onFail(message) { 
    // alert('Failed because: ' + message); 
    } 

    function uploadPhoto(imageURI){ 
     console.log(imageURI); 
    spinnerplugin.show(); 
    var UserId = localStorage.getItem('UserId'); 
    // imgPostGallary 
    // var img = document.getElementById('imgPostGallary'); 
       // var imageURI = img.src; 
       // var imageURI = imageData; 
       // img.src = imageURI; 
    // var ImageDataUp = localStorage.getItem('ImageDataUp'); 
    // var imageURI = ImageDataUp; 
    var options = new FileUploadOptions(); 
    options.fileKey="file"; 
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
    options.mimeType="image/jpeg"; 

    var ft = new FileTransfer(); 
    ft.upload(imageURI, encodeURI("http://XYZ/uploadimg?user_id="+UserId+""), winGallary, fail, options); 
    console.log(ft.upload); 
    } 

    function winGallary(rGallary) { 
     console.log("Code = " + rGallary.responseCode); 
     console.log("Response = " + rGallary.response); 
     console.log("Sent = " + rGallary.bytesSent); 
     spinnerplugin.hide(); 
     window.location = 'profileUserImgUploadInGallary.html'; 
    } 

    function fail(error) { 
    console.log("upload error source " + error.source); 
    console.log("upload error target " + error.target); 
    } 
+0

谢谢,这段代码在android和ios上都工作正常。也许窗口我会尝试,但我想它会 – BelgianR

+0

亚代码工作在几乎所有的平台,没有任何错误。 &Thank你BelgianR。 –

0

你好,这里是完整的例子它为我拍摄照片和设置图像标记和上传服务器上的照片。你仍然面临着任何问题。

<img id="profileImageId"> 
    <script type="text/javascript"> 
       var profileImage = ''; 
       function profileCapturePhotoEdit() { 
        navigator.camera.getPicture(profileonPhotoDataSuccess, onFail, { 
        quality: 50, 
        // allowEdit: true, 
        correctOrientation: true, // using this your image not roted 90 degree 
        destinationType: Camera.DestinationType.DATA_URL, 
        sourceType: Camera.PictureSourceType.CAMERA } 
              ); 
       } 

       function profileonPhotoDataSuccess(imageData) { 
        localStorage.setItem("imageDataProfile","data:image/jpeg;base64," + imageData); 
        var imageDataProfile = localStorage.getItem("imageDataProfile"); 
        document.getElementById('profileImageId').src = imageDataProfile; 
       } 

       function onFail(message) { 
        // alert('Failed because: ' + message); 
       } 

       </script> 

       <!-- uploadProfileImage --> 
      <button onclick="uploadProfileImage();"> 
       Upload Profile Image 
      </button> 

       <script type="text/javascript"> 
       function uploadProfileImage() { 
       var UserId = localStorage.getItem('UserId'); 
       var img = document.getElementById('profileImageId'); 
       var imageURI = img.src; 
       var options = new FileUploadOptions(); 
       options.fileKey="file"; // your file key in your .php file change here 
       options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); 
       options.mimeType="image/jpeg"; // your extension 

       var ft = new FileTransfer(); 
       ft.upload(imageURI, encodeURI("http://XYZ?user_id="+UserId+""), winProfile, failProfile, options); 
       } 

       function winProfile(r) { 
       console.log("Code = " + r.responseCode); 
       console.log("Response = " + r.response); 
       console.log("Sent = " + r.bytesSent); 
       // alert('Send success'); 
       } 

       function failProfile(error) { 
       console.log("upload error source " + error.source); 
       console.log("upload error target " + error.target); 
       } 

      </script> 
+0

感谢你的代码,我想出如何让正确的元数据。我需要的所有代码都是将Camera.DestinationType.FILE_URI更改为Camera.DestinationType.DATA_URL,并在成功回调中向imageUri变量添加“data:image/jpeg; base64”。 但它仍然给我一个没有扩展名的文件,但无论什么,因为图像现在正确显示 – BelgianR

+0

但是,根据phonegap doc:在较新的设备上使用相机拍摄的照片的图像质量相当不错,来自相册即使指定了质量参数,也不会缩减到较低质量。使用Base64对这些图像进行编码已导致许多较新设备出现内存问题。 **因此,强烈建议将FILE_URI用作'Camera.destinationType'。** – BelgianR

+0

它不适用于IOS。我一直得到错误代码1文件没有发现,即使图像显示在元素 – BelgianR