Crop Image from camera before upload (Phonegap)

旧时模样 提交于 2019-12-05 17:30:01

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.

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.

I found the solution for this(it is too late but used for someone like me) but after taking image you need to pass the image path to the plugin(Native android) for croping.

put the following code in your image capture or pick image from gallery(in your 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 
});

after that crop.image.js should be like (include this file name in your 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]
        );
    }
}

Your java code is like

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");
    }*/
}

}

Don't forget to add this class in your CONFIG.XML and add the necessary permissions. Feel free to ask any doubts.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!