Skip to content

Commit

Permalink
Revert "Merge pull request #43 from ASBConsulting/rotation"
Browse files Browse the repository at this point in the history
This reverts commit 476b6b1, reversing
changes made to a87a16d.
  • Loading branch information
JoschkaSchulz committed Jun 21, 2018
1 parent fe27fff commit 87be54c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 133 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-image-resizer",
"version": "0.1.1",
"name": "cordova-plugin-simple-image-resizer",
"version": "0.1.0",
"description": "Plugin for resizing images only with the uri of the image.",
"cordova": {
"id": "cordova-plugin-image-resizer",
Expand Down
2 changes: 0 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
</config-file>

<source-file src="src/android/ImageResizer.java" target-dir="src/info/protonet/imageresizer" />
<preference name="ANDROID_EXIFINTERFACES_VERSION" default="27.+"/>
<framework src="com.android.support:exifinterface:$ANDROID_EXIFINTERFACES_VERSION"/>
</platform>

<platform name="ios">
Expand Down
84 changes: 3 additions & 81 deletions src/android/ImageResizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -17,7 +14,6 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand All @@ -33,8 +29,6 @@ public class ImageResizer extends CordovaPlugin {
private int quality;
private int width;
private int height;
private boolean fixRotation = false;
private boolean base64 = false;

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
Expand All @@ -57,58 +51,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
quality = jsonObject.getInt("quality");
width = jsonObject.getInt("width");
height = jsonObject.getInt("height");
if(jsonObject.has("fixRotation")){
fixRotation = jsonObject.getBoolean("fixRotation");
}
if(jsonObject.has("base64")){
base64 = jsonObject.getBoolean("base64");
}

// load the image from uri
Bitmap bitmap = loadScaledBitmapFromUri(uri, width, height);

if(bitmap == null){
Log.e("Protonet", "There was an error reading the image");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
}
// save the image as jpeg on the device
Uri scaledFile = saveFile(bitmap);

if(fixRotation){
// Get the exif rotation in degrees, create a transformation matrix, and rotate
// the bitmap
int rotation = getRoationDegrees(getRotation(uri));
Matrix matrix = new Matrix();
if (rotation != 0f) {matrix.preRotate(rotation);}
bitmap = Bitmap.createBitmap(
bitmap,
0,
0,
bitmap.getWidth(),
bitmap.getHeight(),
matrix,
true);
}

if(base64){
// convert the bitmap to a b64 string and return
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,
"data:image/jpeg;base64,"+encoded));
}else {
// save the image as jpeg on the device
Uri scaledFile = saveFile(bitmap);

if(scaledFile == null){
Log.e("Protonet", "There was an error saving the thumbnail");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,
scaledFile.toString()));
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, scaledFile.toString()));
return true;
} else {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
Expand All @@ -121,34 +71,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return false;
}

/**
* Gets the image rotation from the image EXIF Data
*
* @param exifOrientation ExifInterface.ORIENTATION_* representation of the rotation
* @return the rotation in degrees
*/
private int getRoationDegrees(int exifOrientation){
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}

/**
* Gets the image rotation from the image EXIF Data
*
* @param uriString the URI of the image to get the rotation for
* @return ExifInterface.ORIENTATION_* representation of the rotation
*/
private int getRotation(String uriString){
try {
ExifInterface exif = new ExifInterface(uriString);
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
return ExifInterface.ORIENTATION_NORMAL;
}
}

/**
* Loads a Bitmap of the given android uri path
*
Expand Down
1 change: 0 additions & 1 deletion src/ios/ImageResizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
#import <Photos/Photos.h>
@interface ImageResizer : CDVPlugin
- (void) resize:(CDVInvokedUrlCommand*)command;
- (UIImage*) rotateImage:(UIImage*) image withRotation:(int) rotation;
@end
48 changes: 1 addition & 47 deletions src/ios/ImageResizer.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,12 @@ - (void) resize:(CDVInvokedUrlCommand*)command
NSString* fileName = [arguments objectForKey:@"fileName"];

BOOL asBase64 = [[arguments objectForKey:@"base64"] boolValue];
BOOL fixRotation = [[arguments objectForKey:@"fixRotation"] boolValue];

// //Get the image from the path
NSURL* imageURL = [NSURL URLWithString:imageUrlString];

sourceImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageURL]];

int rotation = 0;

switch ([sourceImage imageOrientation]) {
case UIImageOrientationUp:
rotation = 0;
break;
case UIImageOrientationDown:
rotation = 180;
break;
case UIImageOrientationLeft:
rotation = 270;
break;
case UIImageOrientationRight:
rotation = 90;
break;
default:
break;
}


PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:@[fileName] options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//this gets called for every asset from its localIdentifier you saved
Expand Down Expand Up @@ -107,10 +87,6 @@ - (void) resize:(CDVInvokedUrlCommand*)command

tempImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"image resizer:%@", (tempImage ? @"image exsist" : @"null" ));

if(fixRotation){
tempImage = [self rotateImage:tempImage withRotation:rotation];
}

UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(tempImage, [quality floatValue] / 100.0f );
Expand Down Expand Up @@ -143,26 +119,4 @@ - (void) resize:(CDVInvokedUrlCommand*)command
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (UIImage*) rotateImage:(UIImage*) image withRotation:(int) rotation{
CGFloat rot = rotation * M_PI / 180;

// Calculate Destination Size
CGAffineTransform t = CGAffineTransformMakeRotation(rot);
CGRect sizeRect = (CGRect) {.size = image.size};
CGRect destRect = CGRectApplyAffineTransform(sizeRect, t);
CGSize destinationSize = destRect.size;

// Draw image
UIGraphicsBeginImageContext(destinationSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, destinationSize.width / 2.0f, destinationSize.height / 2.0f);
CGContextRotateCTM(context, rot);
[image drawInRect:CGRectMake(-image.size.width / 2.0f, -image.size.height / 2.0f, image.size.width, image.size.height)];

// Save image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

@end

0 comments on commit 87be54c

Please sign in to comment.