cronapp-cordova-plugin-ratio-crop 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # RatioCrop
2
+ Take or select picture, crops images with aspect ratio and get base64.
3
+
4
+ ### Installation
5
+ 1. Install the Cordova and Ionic Native plugins:
6
+ ```
7
+ $ ionic cordova plugin add cordova-plugin-camera
8
+ $ npm install --save @ionic-native/camera
9
+
10
+ $ ionic cordova plugin add cordova-plugin-ratio-crop
11
+ $ npm install --save ionic-cordova-plugin-ratio-crop
12
+ ```
13
+
14
+ 2. Add plugins to _providers_ section on your app's module
15
+ ```js
16
+ import { RatioCrop } from 'ionic-cordova-plugin-ratio-crop';
17
+ import { Camera } from '@ionic-native/camera';
18
+
19
+ ...
20
+
21
+ @NgModule({
22
+ declarations: [
23
+ MyApp
24
+ ],
25
+ imports: [
26
+ ...
27
+ ],
28
+ bootstrap: [IonicApp],
29
+ entryComponents: [
30
+ MyApp
31
+ ],
32
+ providers: [
33
+ ...
34
+ Camera,
35
+ RatioCrop,
36
+ ...
37
+ ]
38
+ })
39
+ export class AppModule { }
40
+ ```
41
+
42
+ ### Supported platforms
43
+ * Android
44
+ * iOS
45
+
46
+ ### Usage
47
+
48
+ ```js
49
+ import { Camera } from '@ionic-native/camera';
50
+ import { RatioCrop, RatioCropOptions } from 'ionic-cordova-plugin-ratio-crop';
51
+
52
+ ...
53
+
54
+ private cropOptions: RatioCropOptions = {
55
+ quality: 75,
56
+ targetWidth: 1080,
57
+ targetHeight: 1080,
58
+ widthRatio: -1,
59
+ heightRatio: -1
60
+ };
61
+
62
+ ...
63
+
64
+ constructor(
65
+ private camera: Camera,
66
+ private crop: RatioCrop) { }
67
+
68
+ ...
69
+
70
+ takePicture() {
71
+ return this.camera.getPicture({
72
+ destinationType: this.camera.DestinationType.FILE_URI,
73
+ encodingType: this.camera.EncodingType.JPEG,
74
+ mediaType: this.camera.MediaType.PICTURE,
75
+ sourceType: this.camera.PictureSourceType.CAMERA,
76
+ allowEdit: false,
77
+ correctOrientation: true
78
+ })
79
+ .then((fileUri) => {
80
+ return this.crop.ratioCrop(fileUri, this.cropOptions);
81
+ })
82
+ .then((path) => {
83
+ return this.encodeToBase64(path);
84
+ })
85
+ }
86
+
87
+ selectPicture() {
88
+ return this.camera.getPicture({
89
+ allowEdit: false,
90
+ sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
91
+ mediaType: this.camera.MediaType.PICTURE,
92
+ destinationType: this.camera.DestinationType.FILE_URI
93
+ })
94
+ .then((fileUri) => {
95
+ return this.crop.ratioCrop(fileUri, this.cropOptions);
96
+ })
97
+ .then((path) => {
98
+ return this.encodeToBase64(path);
99
+ })
100
+ }
101
+
102
+ encodeToBase64(src) {
103
+ return new Promise((resolve, reject) => {
104
+ let xhr = new XMLHttpRequest();
105
+ xhr.onload = function () {
106
+ let reader = new FileReader();
107
+ reader.onloadend = function () {
108
+ resolve(reader.result);
109
+ }
110
+ reader.readAsDataURL(xhr.response);
111
+ };
112
+ xhr.open('GET', src);
113
+ xhr.responseType = 'blob';
114
+ xhr.send();
115
+ });
116
+ }
117
+
118
+ ```
119
+
120
+ ### Credits
121
+ - https://github.com/peterantonyrausch/cordova-plugin-ratio-crop
122
+ - https://github.com/peterantonyrausch/ratio-crop
123
+ - https://github.com/apache/cordova-plugin-camera
124
+ - https://github.com/jeduan/cordova-plugin-crop
125
+ - https://github.com/obeza/cordova-plugin-crop-with-ratio
126
+ - https://github.com/qwerqwermhc/Crop
127
+
128
+ ### Problems and Solutions
129
+ #### Problem
130
+ "No Image Selected" error on Android 4.4 device. (Camera Plugin + Background Mode Plugin)
131
+ ##### Solution
132
+ - https://stackoverflow.com/questions/49864382/cordova-plugin-camera-getting-no-image-selected-error-on-android-4-4-device
133
+ - https://github.com/katzer/cordova-plugin-background-mode/issues/263#issuecomment-373455199
134
+ ##### Explanation
135
+ - https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "cronapp-cordova-plugin-ratio-crop",
3
+ "version": "2.0.0",
4
+ "description": "RatioCrop based on qwerqwermhc/Crop.",
5
+ "cordova": {
6
+ "id": "cronapp-cordova-plugin-ratio-crop",
7
+ "platforms": [
8
+ "android",
9
+ "ios"
10
+ ]
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/lincolnminto/cordova-plugin-ratio-crop"
15
+ },
16
+ "keywords": [
17
+ "cordova",
18
+ "ecosystem:cordova",
19
+ "cordova-android",
20
+ "cordova-ios",
21
+ "image",
22
+ "image manipulation",
23
+ "crop"
24
+ ],
25
+ "license": "MIT"
26
+ }
package/plugin.xml ADDED
@@ -0,0 +1,72 @@
1
+ <?xml version='1.0' encoding='utf-8'?>
2
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
3
+ xmlns:android="http://schemas.android.com/apk/res/android" id="cronapp-cordova-plugin-ratio-crop" version="2.0.0">
4
+
5
+ <name>RatioCrop</name>
6
+
7
+ <description>Crop an image in a Cordova app</description>
8
+
9
+ <license>MIT</license>
10
+
11
+ <engines>
12
+ <engine name="cordova-android" version=">=4.0.0" />
13
+ <engine name="cordova-ios" version=">=3.5.0" />
14
+ </engines>
15
+
16
+ <js-module name="RatioCrop" src="www/ratioCrop.js">
17
+ <clobbers target="plugins.ratioCrop" />
18
+ </js-module>
19
+
20
+ <platform name="ios">
21
+ <config-file parent="/*" target="config.xml">
22
+ <feature name="RatioCrop">
23
+ <param name="ios-package" value="RatioCrop" />
24
+ </feature>
25
+ </config-file>
26
+
27
+ <header-file src="src/ios/RatioCrop.h" />
28
+ <source-file src="src/ios/RatioCrop.m" />
29
+ <header-file src="src/ios/Lib/PECropRectView.h" />
30
+ <source-file src="src/ios/Lib/PECropRectView.m" />
31
+ <header-file src="src/ios/Lib/PECropView.h" />
32
+ <source-file src="src/ios/Lib/PECropView.m" />
33
+ <header-file src="src/ios/Lib/PECropViewController.h" />
34
+ <source-file src="src/ios/Lib/PECropViewController.m" />
35
+ <header-file src="src/ios/Lib/PEResizeControl.h" />
36
+ <source-file src="src/ios/Lib/PEResizeControl.m" />
37
+ <header-file src="src/ios/Lib/UIImage+PECrop.h" />
38
+ <source-file src="src/ios/Lib/UIImage+PECrop.m" />
39
+ <resource-file src="src/ios/Lib/PEPhotoCropEditor.bundle" />
40
+
41
+ <framework src="QuartzCore.framework" />
42
+ <framework src="AVFoundation.framework" />
43
+ </platform>
44
+
45
+ <platform name="android">
46
+ <config-file parent="/*" target="res/xml/config.xml">
47
+ <feature name="RatioCrop">
48
+ <param name="android-package" value="com.peterantonyrausch.crop.RatioCrop" />
49
+ </feature>
50
+ </config-file>
51
+
52
+ <config-file parent="/*" target="res/values/crop-ratio-styles.xml">
53
+
54
+ </config-file>
55
+
56
+
57
+ <framework src="com.soundcloud.android:android-crop:1.0.0@aar" />
58
+
59
+ <config-file target="AndroidManifest.xml" parent="/manifest/application">
60
+ <activity android:name="com.soundcloud.android.crop.CropImageActivity" android:theme="@style/CropImageTheme" />
61
+ </config-file>
62
+
63
+ <config-file target="AndroidManifest.xml" parent="/manifest">
64
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
65
+ </config-file>
66
+
67
+ <source-file src="src/android/RatioCrop.java" target-dir="src/com/peterantonyrausch/crop" />
68
+
69
+ <source-file src="src/android/crop-ratio-styles.xml" target-dir="res/values/" />
70
+ </platform>
71
+
72
+ </plugin>
@@ -0,0 +1,138 @@
1
+ package com.peterantonyrausch.crop;
2
+
3
+ import android.app.Activity;
4
+ import android.content.Intent;
5
+ import android.net.Uri;
6
+ import android.os.Bundle;
7
+ import android.os.Environment;
8
+
9
+ import com.soundcloud.android.crop.Crop;
10
+
11
+ import org.apache.cordova.CallbackContext;
12
+ import org.apache.cordova.CordovaPlugin;
13
+ import org.apache.cordova.PluginResult;
14
+ import org.json.JSONArray;
15
+ import org.json.JSONException;
16
+ import org.json.JSONObject;
17
+
18
+ import java.io.File;
19
+
20
+ /**
21
+ * This class echoes a string called from JavaScript.
22
+ */
23
+ public class RatioCrop extends CordovaPlugin {
24
+ private CallbackContext callbackContext;
25
+ private Uri inputUri;
26
+ private Uri outputUri;
27
+
28
+ @Override
29
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
30
+ if (action.equals("ratioCropImage")) {
31
+ String imagePath = args.getString(0);
32
+ JSONObject options = args.getJSONObject(1);
33
+
34
+ int widthRatio = options.getInt("widthRatio");
35
+ int heightRatio = options.getInt("heightRatio");
36
+ int targetWidth = options.getInt("targetWidth");
37
+ int targetHeight = options.getInt("targetHeight");
38
+
39
+ this.inputUri = Uri.parse(imagePath);
40
+ this.outputUri = Uri.fromFile(
41
+ new File(getTempDirectoryPath() + "/" + System.currentTimeMillis() + "-ratiocropped.jpg"));
42
+ PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
43
+ pr.setKeepCallback(true);
44
+ callbackContext.sendPluginResult(pr);
45
+ this.callbackContext = callbackContext;
46
+ cordova.setActivityResultCallback(this);
47
+ Crop croper = Crop.of(this.inputUri, this.outputUri);
48
+ if (targetHeight > 0 && targetWidth > 0) {
49
+ croper.withMaxSize(targetWidth, targetHeight);
50
+ }
51
+ if (widthRatio > 0 && heightRatio > 0) {
52
+ croper.withAspect(widthRatio, heightRatio);
53
+ }
54
+
55
+ croper.start(cordova.getActivity());
56
+
57
+ return true;
58
+ }
59
+ return false;
60
+ }
61
+
62
+ @Override
63
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
64
+ if (requestCode == Crop.REQUEST_CROP) {
65
+ if (resultCode == Activity.RESULT_OK) {
66
+ Uri imageUri = Crop.getOutput(intent);
67
+ this.callbackContext.success("file://" + imageUri.getPath() + "?" + System.currentTimeMillis());
68
+ this.callbackContext = null;
69
+ } else if (resultCode == Crop.RESULT_ERROR) {
70
+ try {
71
+ JSONObject err = new JSONObject();
72
+ err.put("message", "Error on cropping");
73
+ err.put("code", String.valueOf(resultCode));
74
+ this.callbackContext.error(err);
75
+ this.callbackContext = null;
76
+ } catch (JSONException e) {
77
+ e.printStackTrace();
78
+ }
79
+ } else if (resultCode == Activity.RESULT_CANCELED) {
80
+ try {
81
+ JSONObject err = new JSONObject();
82
+ err.put("message", "User cancelled");
83
+ err.put("code", "userCancelled");
84
+ this.callbackContext.error(err);
85
+ this.callbackContext = null;
86
+ } catch (JSONException e) {
87
+ e.printStackTrace();
88
+ }
89
+ }
90
+ }
91
+ super.onActivityResult(requestCode, resultCode, intent);
92
+ }
93
+
94
+ private String getTempDirectoryPath() {
95
+ File cache = null;
96
+
97
+ // SD Card Mounted
98
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
99
+ cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
100
+ + cordova.getActivity().getPackageName() + "/cache/");
101
+ }
102
+ // Use internal storage
103
+ else {
104
+ cache = cordova.getActivity().getCacheDir();
105
+ }
106
+
107
+ // Create the cache directory if it doesn't exist
108
+ cache.mkdirs();
109
+ return cache.getAbsolutePath();
110
+ }
111
+
112
+ public Bundle onSaveInstanceState() {
113
+ Bundle state = new Bundle();
114
+
115
+ if (this.inputUri != null) {
116
+ state.putString("inputUri", this.inputUri.toString());
117
+ }
118
+
119
+ if (this.outputUri != null) {
120
+ state.putString("outputUri", this.outputUri.toString());
121
+ }
122
+
123
+ return state;
124
+ }
125
+
126
+ public void onRestoreStateForActivityResult(Bundle state, CallbackContext callbackContext) {
127
+
128
+ if (state.containsKey("inputUri")) {
129
+ this.inputUri = Uri.parse(state.getString("inputUri"));
130
+ }
131
+
132
+ if (state.containsKey("outputUri")) {
133
+ this.inputUri = Uri.parse(state.getString("outputUri"));
134
+ }
135
+
136
+ this.callbackContext = callbackContext;
137
+ }
138
+ }
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <resources>
3
+ <style name="CropImageTheme" parent="Theme.AppCompat.Light">
4
+ <item name="cropImageStyle">@style/CropImageView</item>
5
+ </style>
6
+
7
+ <style name="CropImageView" parent="Theme.AppCompat.Light">
8
+ <item name="showHandles">always</item>
9
+ <item name="showThirds">true</item>
10
+ </style>
11
+ </resources>
@@ -0,0 +1,30 @@
1
+ //
2
+ // PECropRectView.h
3
+ // PhotoCropEditor
4
+ //
5
+ // Created by kishikawa katsumi on 2013/05/21.
6
+ // Copyright (c) 2013 kishikawa katsumi. All rights reserved.
7
+ //
8
+
9
+ #import <UIKit/UIKit.h>
10
+
11
+ @protocol PECropRectViewDelegate;
12
+
13
+ @interface PECropRectView : UIView
14
+
15
+ @property (nonatomic, weak) id<PECropRectViewDelegate> delegate;
16
+ @property (nonatomic) BOOL showsGridMajor;
17
+ @property (nonatomic) BOOL showsGridMinor;
18
+
19
+ @property (nonatomic) BOOL keepingAspectRatio;
20
+
21
+ @end
22
+
23
+ @protocol PECropRectViewDelegate <NSObject>
24
+
25
+ - (void)cropRectViewDidBeginEditing:(PECropRectView *)cropRectView;
26
+ - (void)cropRectViewEditingChanged:(PECropRectView *)cropRectView;
27
+ - (void)cropRectViewDidEndEditing:(PECropRectView *)cropRectView;
28
+
29
+ @end
30
+