33import android .graphics .Bitmap ;
44import android .graphics .Bitmap .CompressFormat ;
55import android .graphics .BitmapFactory ;
6- import android .graphics .Matrix ;
7- import android .media .ExifInterface ;
86import android .net .Uri ;
97import android .os .Environment ;
10- import android .util .Base64 ;
118import android .util .Log ;
129import org .apache .cordova .CallbackContext ;
1310import org .apache .cordova .CordovaPlugin ;
1714import org .json .JSONException ;
1815import org .json .JSONObject ;
1916
20- import java .io .ByteArrayOutputStream ;
2117import java .io .File ;
2218import java .io .FileNotFoundException ;
2319import java .io .FileOutputStream ;
@@ -33,8 +29,6 @@ public class ImageResizer extends CordovaPlugin {
3329 private int quality ;
3430 private int width ;
3531 private int height ;
36- private boolean fixRotation = false ;
37- private boolean base64 = false ;
3832
3933 public boolean execute (String action , JSONArray args , CallbackContext callbackContext ) throws JSONException {
4034 try {
@@ -57,58 +51,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
5751 quality = jsonObject .getInt ("quality" );
5852 width = jsonObject .getInt ("width" );
5953 height = jsonObject .getInt ("height" );
60- if (jsonObject .has ("fixRotation" )){
61- fixRotation = jsonObject .getBoolean ("fixRotation" );
62- }
63- if (jsonObject .has ("base64" )){
64- base64 = jsonObject .getBoolean ("base64" );
65- }
6654
6755 // load the image from uri
6856 Bitmap bitmap = loadScaledBitmapFromUri (uri , width , height );
6957
70- if (bitmap == null ){
71- Log .e ("Protonet" , "There was an error reading the image" );
72- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR ));
73- return false ;
74- }
58+ // save the image as jpeg on the device
59+ Uri scaledFile = saveFile (bitmap );
7560
76- if (fixRotation ){
77- // Get the exif rotation in degrees, create a transformation matrix, and rotate
78- // the bitmap
79- int rotation = getRoationDegrees (getRotation (uri ));
80- Matrix matrix = new Matrix ();
81- if (rotation != 0f ) {matrix .preRotate (rotation );}
82- bitmap = Bitmap .createBitmap (
83- bitmap ,
84- 0 ,
85- 0 ,
86- bitmap .getWidth (),
87- bitmap .getHeight (),
88- matrix ,
89- true );
90- }
91-
92- if (base64 ){
93- // convert the bitmap to a b64 string and return
94- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
95- bitmap .compress (CompressFormat .JPEG , 100 , byteArrayOutputStream );
96- byte [] byteArray = byteArrayOutputStream .toByteArray ();
97- String encoded = Base64 .encodeToString (byteArray , Base64 .DEFAULT );
98- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .OK ,
99- "data:image/jpeg;base64," +encoded ));
100- }else {
101- // save the image as jpeg on the device
102- Uri scaledFile = saveFile (bitmap );
103-
104- if (scaledFile == null ){
105- Log .e ("Protonet" , "There was an error saving the thumbnail" );
106- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR ));
107- return false ;
108- }
109- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .OK ,
110- scaledFile .toString ()));
111- }
61+ callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .OK , scaledFile .toString ()));
11262 return true ;
11363 } else {
11464 callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR ));
@@ -121,34 +71,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
12171 return false ;
12272 }
12373
124- /**
125- * Gets the image rotation from the image EXIF Data
126- *
127- * @param exifOrientation ExifInterface.ORIENTATION_* representation of the rotation
128- * @return the rotation in degrees
129- */
130- private int getRoationDegrees (int exifOrientation ){
131- if (exifOrientation == ExifInterface .ORIENTATION_ROTATE_90 ) { return 90 ; }
132- else if (exifOrientation == ExifInterface .ORIENTATION_ROTATE_180 ) { return 180 ; }
133- else if (exifOrientation == ExifInterface .ORIENTATION_ROTATE_270 ) { return 270 ; }
134- return 0 ;
135- }
136-
137- /**
138- * Gets the image rotation from the image EXIF Data
139- *
140- * @param uriString the URI of the image to get the rotation for
141- * @return ExifInterface.ORIENTATION_* representation of the rotation
142- */
143- private int getRotation (String uriString ){
144- try {
145- ExifInterface exif = new ExifInterface (uriString );
146- return exif .getAttributeInt (ExifInterface .TAG_ORIENTATION , ExifInterface .ORIENTATION_NORMAL );
147- } catch (IOException e ) {
148- return ExifInterface .ORIENTATION_NORMAL ;
149- }
150- }
151-
15274 /**
15375 * Loads a Bitmap of the given android uri path
15476 *
0 commit comments