English 中文(简体)
使用相机拍照后自动上传 Phonegap 照片
原标题:Automatically upload photo in Phonegap after taking picture with camera

使用 PhoneGap s API 来拍摄相机和文件, 我正试图把它做成这样, 这样当我在一个应用程序中拍摄照片时, 它会自动上传到 Im 使用的服务器上 。

我将两个 API s 复制到我的索引. html 文件, 我唯一修改的是添加另一个 javascript 变量图像Info, 该图像与 PhotoData 成功 的区块中的图像Data 相等。 当我调用该图像时, 此图像Info 被设定为上传Photo 的参数 。

我修改的唯一其它东西是设置线条: navigator.camera.DestinationType(1); ,这样输出就是 imageURI ,这就是 uploadPhoto 需要的。

我在HTML里唯一的代码是:

button onclick="capturePhoto(); uploadPhoto(imageData);">Capture Photo</button (in brackets of course)

但出于某种原因,这行不通 你在我的推理中看到了什么小问题或缺陷吗?

谢谢!

最佳回答

由于相机是事件驱动的, 您不能只关注连锁事件, 并希望最好... 在图片拍摄代码的 < code> 成功 < / code> 回调中, 您必须调用“ 强” 然后 < / 强” 代码来上传图片 。

EDIT

这里我正在使用 成功绘制图像时返回的 URI 。 但应该非常直截了当 。

function takePicture() {
  loadPhotoIntake();
  navigator.camera.getPicture(
    setPicture,
    function(e) {
      console.log("Error getting picture: " + e);
      document.getElementById( camera_status ).innerHTML = "Error getting picture.";
    },
    { quality: 70, destinationType: navigator.camera.DestinationType.FILE_URI});
};


function setPicture(uri) {
  var c=document.getElementById( camera_image );
  var ctx = c.getContext( 2d );
  var img = new Image();
  var canvasCopy = document.createElement("canvas");
  var copyContext = canvasCopy.getContext("2d");
  var maxWidth, maxHeight;
  img.onload = function(){
    // resizing code
      .....
    // draw
    ctx.drawImage(canvasCopy, 0, 0, camera_image.width, camera_image.height);
  };
  img.src = uri;
}
问题回答

查看 < a href=> http://docs.phonegap.com/en/1.0.0/phonegap_file_file.md.html#fileTransfer" rel=“nofollow” >FileTransfer API 将允许您直接将图像上传到您的文件服务器。 (假设您使用最新的电话)

这正是你想要的。源代码取自Phonegap s docs:

// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
function onDeviceReady() {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(uploadPhoto,
  function(message) { alert( get picture failed ); },
    { quality: 50, 
      destinationType: navigator.camera.DestinationType.FILE_URI,
    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
    );
  }
  function uploadPhoto(imageURI) {
    var options = new FileUploadOptions();
    options.fileKey="file";
    options.fileName=imageURI.substr(imageURI.lastIndexOf( / )+1);
    options.mimeType="image/jpeg";
    var params = new Object();
    params.value1 = "test";
    params.value2 = "param";
    options.params = params;
    var ft = new FileTransfer();
    ft.upload(imageURI, "http://some.server.com/upload.php", win, fail, options);
  }
  function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
  }
  function fail(error) {
    alert("An error has occurred: Code = " = error.code);
  }

看起来你可以使用 PhoneGap API 的 camera. getPicture (...) 。 这将启动默认的相机应用程序, 让用户拍照, 当它们完成后, 它会返回作为 base64 编码的字符串的数据, 或者给你图像文件的uri。 这样您就可以使用它上传到服务器。 您可以在找到的文档中读到更多关于 camera. getPicture 的更多信息 。 http:// a href=" http://docs.phonegap.com/en/1. 0.0/phonegap_ camera_camera. md.html" rel=“ nofollow” inhere. 它似乎从该文章中直线前进 。

我从来没有使用过PhoneGap 我两分钟内就找到了这个





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签