Use PhoneGap to crop and compress images and upload them (Android version)

Source: Internet
Author: User

After installing the camera plugin of phonegap, you can crop or compress the image from the picture library, but the plugin itself has some problems and has been improved, and can now complete the above functions smoothly.

Installation Method

1 First install the camera plugin using the command line:

PhoneGap Plugin Add Org.apache.cordova.camera

2 After the installation is successful, download the following Cameralauncher.java file to replace the file with the same name in the plug-in package:

Http://phonegap.jiusem.net/camera/src/android/CameraLauncher.java

How to use

1 crop the picture. The following code demonstrates the complete process of cropping a picture to 100*100 and uploading it to the server, note the highlighted section:

$ (function () {$ (' #b1 '). Click (function () {navigator.camera.getPicture (Imgdata) {$ (' #pInfo '). HTML (imgdata) ;//$ (' #img '). attr (' src ', imgdata); var options = new Fileuploadoptions (); options.filekey= "file"; Options.filename= " Test.jpg "; options.mimetype=" Image/jpeg "; var ft = new Filetransfer (); Ft.upload (Imgdata, encodeURI (" http://... Test.php "), Win, fail, options), function () {}, {quality:50,    DestinationType:Camera.DestinationType.FILE_URL, c2/>sourcetype:0,
        //==== Key Settings =======================================    allowedit:true,//Crop box    targetwidth:100,//picture crop height    targetheight:100}); /Picture Clipping height
====== key Settings =========================================});//upload successful callback var win = function (r) {             Console.log (r);} Upload failed callback var = function (Error) {    alert ("Upload failed");});

2 compress the picture. The following code shows the process of limiting the size of a picture to 768*1280 and uploading, and the last picture is not necessarily 768*1280, but the width height does not exceed the set value, please note the highlighted part:

$ (function () {$ (' #b1 '). Click (function () {navigator.camera.getPicture (Imgdata) {$ (' #pInfo '). HTML (imgdata) ;//$ (' #img '). attr (' src ', imgdata); var options = new Fileuploadoptions (); options.filekey= "file"; Options.filename= " Test.jpg "; options.mimetype=" Image/jpeg "; var ft = new Filetransfer (); Ft.upload (Imgdata, encodeURI (" http://... Test.php "), Win, fail, options), function () {}, {      quality:50,    destinationType:Camera.DestinationType.FILE_ URL,    sourcetype:0,    //===== key section =========================================    targetwidth:768,//Picture output width    targetheight:1280//Image Output height
====== key part ==========================================}); /upload Successful callback var win = function (r) {           Console.log (r);} Upload failed callback var = function (Error) {    alert ("Upload failed");});

The above example contains file upload, if you need to test the complete example, you also need to install the file transfer plug-in, the document upload server code and normal form upload no difference.

Welcome to communicate with me, any questions or suggestions for improvement please leave a message or add me qq:291445576 (explanation) ~

Use PhoneGap to crop and compress images and upload them (Android version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.