2012-06-22 203 views
4

我想問你..裁剪圖片上傳前(PhoneGap的)

我有一個代碼...使用PhoneGap的..但我感到困惑如何調用/裁剪圖像拍照後它從相機/文件管理器...

這裏的代碼...

<!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頁圖像裁剪,所以有一個上傳按鈕上傳裁剪圖像...

任何人都可以幫助m呃那樣做?

+2

啊,是的,如果我在之前看到它,並且我不確定我會回答,你的接受率上的_work。 – MrTJ

回答

7

PhoneGap沒有內置的裁剪功能。某些平臺(iPhone肯定)允許用戶在使用相機拍攝照片後剪裁圖片,但在之前使用,如果您將allowEdit = true參數傳遞給getPicture,它將返回到JavaScript代碼。但是你不能從腳本中獲得控制權。

您必須使用JavaScript自己實現裁剪功能。用HTML5的canvas標籤預計會更容易。你可以找到一個漂亮的教程here

+0

JavaScript不能在phonegap中工作。如果你有任何演示? –

+0

原始教程中發佈的代碼很可能已過時。不幸的是,自從過去兩年以來我一直沒有與Phonegap一起工作,所以恐怕我無法幫助你。 – MrTJ

+0

好的,謝謝...你能分享你的手機體驗嗎?它怎麼樣?其實我是新來的? –

2

我找到了解決方案(這太晚了,但用於像我這樣的人),但拍攝圖像後,你需要將圖像路徑傳遞給插件(本機android)進行裁剪。

把下面的代碼在你的圖像採集或從圖庫中選擇圖片(在您的index.html):

navigator.camera.getPicture(function(imageURI){ 
    window.resolveLocalFileSystemURI(imageURI, function(fileEntry){ 
      fileEntry.file(function(fileObj) { 
        var imagedata ="sample/new001.img"; 
        // able to get the image location using phonegap 
       cropImage.createEvent(imagedata); 

      }); 

    }, fail); 
    }, fail, { quality: 50, 
     destinationType: destinationType.NATIVE_URI, 
     sourceType: pictureSource.PHOTOLIBRARY 
}); 
crop.image.js應該像後

(包括本文件名在您的index.html)

var cropImage = { 
    createEvent: function(fileName) { 
     cordova.exec(
       null, // success callback function 
       null, // error callback function 
      'CropImage', // mapped to our native Java class called "CropImage" 
      'GetImageName', // with this action name 
      [fileName] 
     ); 
    } 
} 

Java代碼就像

public class CropImage extends CordovaPlugin{ 
public final String ACTION_GET_IMAGE_NAME = "GetImageName"; 
Uri myUri; 
@Override 
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { 
    // Log.e(TAG,"Inside Version plugin."); 
    boolean result = false; 
    if(action.equals(ACTION_GET_IMAGE_NAME)) { 
     try { 
      myUri = Uri.parse(args.getString(0)); 
      cropCapturedImage(myUri); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     result = true; 
    } 


    return result; 
} 

public void cropCapturedImage(Uri picUri){ 
    //call the standard crop action intent 
    Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
    //indicate image type and Uri of image 
    cropIntent.setDataAndType(picUri, "image/*"); 
    //set crop properties 
    cropIntent.putExtra("crop", "true"); 
    cropIntent.putExtra("aspectX", 1); 
    cropIntent.putExtra("aspectY", 1); 
    cropIntent.putExtra("outputX", 256); 
    cropIntent.putExtra("outputY", 256); 
    // for save the image in same location with same name. 
    File f = new File(Environment.getExternalStorageDirectory()+"your image location here"); 
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));    
    cropIntent.putExtra("output", Uri.fromFile(f)); 
    cropIntent.putExtra("return-data", false); 

    //start the activity - we handle returning in onActivityResult 
    this.cordova.startActivityForResult((CordovaPlugin) this,cropIntent, 2); 
} 
public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    //Log.e("final", String.valueOf(requestCode)); 
    /*if(requestCode == 2){ 
     //Create an instance of bundle and get the returned data 
     Bundle extras = intent.getExtras(); 
     //get the cropped bitmap from extras 
     Bitmap thePic = extras.getParcelable("data"); 
    }*/ 
} 

} 

不要忘記在你的CONFIG.XML中添加這個類並添加必要的權限。隨意問任何疑問。

+0

任何演示代碼可用? –