从相机裁剪图片上传前,(PhoneGap的) [英] Crop Image from camera before upload (Phonegap)

查看:164
本文介绍了从相机裁剪图片上传前,(PhoneGap的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你..

我有一个code ...使用PhoneGap的..但我感到困惑如何调用/裁剪图像把它从后摄像头/文件管理器...

i have a code... using phonegap.. but i was confused about how to call / crop the image after take it from camera / file manager...

这里的code ...

here the code...

<!DOCTYPE HTML>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="js/jquery.mobile-1.0.min.css" />
    <script src="js/jquery-1.6.4.min.js"></script>
    <script src="js/jquery.mobile-1.0.min.js"></script>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    var deviceReady = false;

    /**
     * Take picture with camera
     */
    function takePicture() {
        navigator.camera.getPicture(
            function(uri) {
                var img = document.getElementById('camera_image');
                img.style.visibility = "visible";
                img.style.display = "block";
                img.src = uri;
                window.location.hash = "#page2";
                /*document.getElementById('camera_status').innerHTML = "Success"; */


            },
            function(e) {
                console.log("Error getting picture: " + e);
                document.getElementById('camera_status').innerHTML = "Error getting picture.";
            },
            { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, targetWidth: 1153, targetHeight: 385
            }

            );
    };

    /**
     * Select picture from library
     */
    function selectPicture() {
        navigator.camera.getPicture(
            function(uri) {
                var img = document.getElementById('camera_image');
                img.style.visibility = "visible";
                img.style.display = "block";
                img.src = uri;
                document.getElementById('camera_status').innerHTML = "Success";
                window.location.hash = "#page2";
            },
            function(e) {
                console.log("Error getting picture: " + e);
                document.getElementById('camera_status').innerHTML = "Error getting picture.";
            },
            { quality: 50, targetWidth: 1153, targetHeight: 385, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
    };

    /**
     * Upload current picture
     */
    function uploadPicture() {

        // Get URI of picture to upload
        var img = document.getElementById('camera_image');
        var imageURI = img.src;
        if (!imageURI || (img.style.display == "none")) {
            document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
            return;
        }

        // Verify server has been entered
        server = document.getElementById('serverUrl').value;
        if (server) {

            // Specify transfer options
            var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";
            options.chunkedMode = false;

            // Transfer picture to server
            var ft = new FileTransfer();
            $.mobile.showPageLoadingMsg();
            ft.upload(imageURI, server, function(r) {
                document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
                viewUploadedPictures();
                $.mobile.hidePageLoadingMsg();
                window.location.hash = "#page3";
            }, function(error) {
                $.mobile.hidePageLoadingMsg();
                document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;               
            }, options);
        }
    }

    /**
     * View pictures uploaded to the server
     */
    function viewUploadedPictures() {

        // Get server URL
        server = document.getElementById('serverUrl').value;
        if (server) {

            // Get HTML that lists all pictures on server using XHR 
            var xmlhttp = new XMLHttpRequest();

            // Callback function when XMLHttpRequest is ready
            xmlhttp.onreadystatechange=function(){
                if(xmlhttp.readyState === 4){

                    // HTML is returned, which has pictures to display
                    if (xmlhttp.status === 200) {
                        document.getElementById('server_images').innerHTML = xmlhttp.responseText;
                    }

                    // If error
                    else {
                        document.getElementById('server_images').innerHTML = "Error retrieving pictures from server.";
                    }
                }
            };
            xmlhttp.open("GET", server , true);
            xmlhttp.send();         
        }   
    }

    /**
     * Function called when page has finished loading.
     */
    function init() {
        document.addEventListener("deviceready", function() {deviceReady = true;}, false);
        window.setTimeout(function() {
            if (!deviceReady) {
                alert("Error: PhoneGap did not initialize.  Demo will not run correctly.");
            }
        },2000);
    }

    </script>

  </head>
  <body onload="init();">
  <!-- Page 1 -->
<div data-role="page" id="page1">
   <!-- Page 1 Header -->
   <div data-role="header">
      <h1>Ambil Gambar</h1>
   </div>
   <!-- Page 1 Content -->
   <div data-role="content">
      <center>
    <a href="javascript:void(0)" onclick="takePicture();">
    <img src="image/camera.png" width="150px" height="150px">
    </a>
    <br>
    <br>
    <b>Atau</b>
    <br>
    <br>
    <a href="javascript:void(0)" onclick="selectPicture();">
    <img src="image/upload.png">
    </a>

    </center>

   </div>
   <!-- Page 1 Footer -->
   <div data-role="footer">
      <h4>Footer 1</h4>
   </div>
</div>
<!-- Page 2 -->
<div data-role="page" id="page2">
   <!-- Page 2 Header -->
   <div data-role="header">
      <h1>Header 2</h1>
   </div>
   <!-- Page 2 Content -->
   <div data-role="content">

       <img style="width:100%;visibility:hidden;display:none;" id="camera_image" src="" />
      <input type="button" onclick="uploadPicture();" value="Upload Picture" />
      <input id="serverUrl" type="text" value="http://kiosban.com/android/camera/upload.php" />
      Status : <span id="camera_status"></span>
      <a href="#page3">Skip</a>
   </div>
   <!-- Page 2 Footer -->
   <div data-role="footer">
      <h4>Footer 2</h4>
   </div>
</div>
<!-- Page 3 -->
<div data-role="page" id="page3">
   <!-- Page 3 Header -->
   <div data-role="header">
      <h1>Header 3</h1>
   </div>
   <!-- Page 3 Content -->
   <div data-role="content">
   <div id="server_images"></div>
      <h3>Server:</h3>
        <b>Images on server:</b>
        <div id="server_images"></div>

        <input type="button" onclick="viewUploadedPictures();" value="View Uploaded Pictures" />
   </div>
   <!-- Page 2 Footer -->
   <div data-role="footer">
      <h4>Footer 2</h4>
   </div>
</div>



  </body>
</html>

我要呼吁#第2页图像裁剪,所以有一个上传按钮上传裁剪图像...

I want to call image crop on #page2, so there is an upload button to upload the cropped image...

任何人可以帮助我做到这一点?

can anybody help me do that??

推荐答案

PhoneGap的不具有内置的作物特性。有些平台(iPhone肯定的)允许的用户的使用相机,但服用后裁剪图片的的将它,如果你传递返回到您的JavaScript code在allowEdit = true参数来Getpicture中。但你不会从你的脚本有控制这里。

PhoneGap does not have built-in crop features. Some platforms (iPhone for sure) allows the user to crop the picture after taking it with the camera but before it would be returned to your JavaScript code if you pass the allowEdit = true parameter to getPicture. But you won't have control here from your script.

您将不得不实现裁剪功能,从自己的JavaScript。这是比较容易然后用HTML5的canvas标签的预期。你可以在这里找到一个pretty教程

You'll have to implement the cropping feature yourself from JavaScript. It is easier then expected with the canvas tag of HTML5. You can find a pretty tutorial here.

这篇关于从相机裁剪图片上传前,(PhoneGap的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆