Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharekey Diff #10

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Build;
import android.os.Environment;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.renderscript.ScriptIntrinsicResize;
import android.renderscript.Type;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableMap;

Expand All @@ -25,8 +33,55 @@
* Created by ipusic on 12/27/16.
*/

class Compression {
public class Compression {

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
// https://medium.com/@petrakeas/alias-free-resize-with-renderscript-5bf15a86ce3
public static Bitmap resizeBitmapWithRenderScript(RenderScript rs, Bitmap src, int dstWidth) {
Bitmap.Config bitmapConfig = src.getConfig();
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
float srcAspectRatio = (float) srcWidth / srcHeight;
int dstHeight = (int) (dstWidth / srcAspectRatio);

float resizeRatio = (float) srcWidth / dstWidth;

/* Calculate gaussian's radius */
float sigma = resizeRatio / (float) Math.PI;
// https://android.googlesource.com/platform/frameworks/rs/+/master/cpu_ref/rsCpuIntrinsicBlur.cpp
float radius = 2.5f * sigma - 1.5f;
radius = Math.min(25, Math.max(0.0001f, radius));

/* Gaussian filter */
Allocation tmpIn = Allocation.createFromBitmap(rs, src);
Allocation tmpFiltered = Allocation.createTyped(rs, tmpIn.getType());
ScriptIntrinsicBlur blurIntrinsic = ScriptIntrinsicBlur.create(rs, tmpIn.getElement());

blurIntrinsic.setRadius(radius);
blurIntrinsic.setInput(tmpIn);
blurIntrinsic.forEach(tmpFiltered);

tmpIn.destroy();
blurIntrinsic.destroy();

/* Resize */
Bitmap dst = Bitmap.createBitmap(dstWidth, dstHeight, bitmapConfig);
Type t = Type.createXY(rs, tmpFiltered.getElement(), dstWidth, dstHeight);
Allocation tmpOut = Allocation.createTyped(rs, t);
ScriptIntrinsicResize resizeIntrinsic = ScriptIntrinsicResize.create(rs);

resizeIntrinsic.setInput(tmpFiltered);
resizeIntrinsic.forEach_bicubic(tmpOut);
tmpOut.copyTo(dst);

tmpFiltered.destroy();
tmpOut.destroy();
resizeIntrinsic.destroy();

return dst;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
File resize(Context context, String originalImagePath, int maxWidth, int maxHeight, int quality) throws IOException {
Bitmap original = BitmapFactory.decodeFile(originalImagePath);

Expand All @@ -45,16 +100,20 @@ File resize(Context context, String originalImagePath, int maxWidth, int maxHeig
float ratioMax = (float) maxWidth / (float) maxHeight;

int finalWidth = maxWidth;
int finalHeight = maxHeight;

if (ratioMax > 1) {
finalWidth = (int) ((float) maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float) maxWidth / ratioBitmap);
}

Bitmap resized = Bitmap.createScaledBitmap(original, finalWidth, finalHeight, true);
resized = Bitmap.createBitmap(resized, 0, 0, finalWidth, finalHeight, rotationMatrix, true);
Bitmap resized = resizeBitmapWithRenderScript(RenderScript.create(context), original, finalWidth);

Bitmap rotatedBitmap = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotationMatrix, true);

if (rotatedBitmap != resized) {
resized.recycle();
}

resized = rotatedBitmap;

File imageDirectory = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void clean(final Promise promise) {
return;
}

permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
permissionsCheck(activity, promise, Collections.singletonList(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ? Manifest.permission.WRITE_EXTERNAL_STORAGE : Manifest.permission.READ_MEDIA_IMAGES), new Callable<Void>() {
@Override
public Void call() {
try {
Expand Down Expand Up @@ -193,7 +193,7 @@ public void cleanSingle(final String pathToDelete, final Promise promise) {
return;
}

permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
permissionsCheck(activity, promise, Collections.singletonList(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ? Manifest.permission.WRITE_EXTERNAL_STORAGE : Manifest.permission.READ_MEDIA_IMAGES), new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
Expand Down Expand Up @@ -283,7 +283,7 @@ public void openCamera(final ReadableMap options, final Promise promise) {
setConfiguration(options);
resultCollector.setup(promise, false);

permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.CAMERA, Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ? Manifest.permission.WRITE_EXTERNAL_STORAGE : Manifest.permission.READ_MEDIA_IMAGES), new Callable<Void>() {
@Override
public Void call() {
initiateCamera(activity);
Expand Down Expand Up @@ -373,7 +373,7 @@ public void openPicker(final ReadableMap options, final Promise promise) {
setConfiguration(options);
resultCollector.setup(promise, multiple);

permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
permissionsCheck(activity, promise, Collections.singletonList(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ? Manifest.permission.WRITE_EXTERNAL_STORAGE : Manifest.permission.READ_MEDIA_IMAGES), new Callable<Void>() {
@Override
public Void call() {
initiatePicker(activity);
Expand All @@ -395,7 +395,7 @@ public void openCropper(final ReadableMap options, final Promise promise) {
resultCollector.setup(promise, false);

final Uri uri = Uri.parse(options.getString("path"));
permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
permissionsCheck(activity, promise, Collections.singletonList(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ? Manifest.permission.WRITE_EXTERNAL_STORAGE : Manifest.permission.READ_MEDIA_IMAGES), new Callable<Void>() {
@Override
public Void call() {
startCropping(activity, uri);
Expand Down
54 changes: 28 additions & 26 deletions ios/src/ImageCropPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ - (void) processSingleImagePick:(UIImage*)image withExif:(NSDictionary*) exif wi
- (void)dismissCropper:(UIViewController *)controller selectionDone:(BOOL)selectionDone completion:(void (^)(void))completion {
switch (self.currentSelectionMode) {
case CROPPING:
[controller dismissViewControllerAnimated:YES completion:completion];
[controller dismissViewControllerAnimated:NO completion:completion];
break;
case PICKER:
if (selectionDone) {
Expand Down Expand Up @@ -864,32 +864,34 @@ + (NSDictionary *)cgRectToDictionary:(CGRect)rect {

#pragma mark - TOCCropViewController Implementation
- (void)cropImage:(UIImage *)image {
TOCropViewController *cropVC;
if ([[[self options] objectForKey:@"cropperCircleOverlay"] boolValue]) {
cropVC = [[TOCropViewController alloc] initWithCroppingStyle:TOCropViewCroppingStyleCircular image:image];
} else {
cropVC = [[TOCropViewController alloc] initWithImage:image];
CGFloat widthRatio = [[self.options objectForKey:@"width"] floatValue];
CGFloat heightRatio = [[self.options objectForKey:@"height"] floatValue];
if (widthRatio > 0 && heightRatio > 0){
CGSize aspectRatio = CGSizeMake(widthRatio, heightRatio);
cropVC.customAspectRatio = aspectRatio;

}
cropVC.aspectRatioLockEnabled = ![[self.options objectForKey:@"freeStyleCropEnabled"] boolValue];
cropVC.resetAspectRatioEnabled = !cropVC.aspectRatioLockEnabled;
}

cropVC.title = [[self options] objectForKey:@"cropperToolbarTitle"];
cropVC.delegate = self;

cropVC.doneButtonTitle = [self.options objectForKey:@"cropperChooseText"];
cropVC.cancelButtonTitle = [self.options objectForKey:@"cropperCancelText"];

cropVC.modalPresentationStyle = UIModalPresentationFullScreen;

dispatch_async(dispatch_get_main_queue(), ^{
[[self getRootVC] presentViewController:cropVC animated:FALSE completion:nil];
TOCropViewController *cropVC;
if ([[[self options] objectForKey:@"cropperCircleOverlay"] boolValue]) {
cropVC = [[TOCropViewController alloc] initWithCroppingStyle:TOCropViewCroppingStyleCircular image:image];
} else {
cropVC = [[TOCropViewController alloc] initWithImage:image];
CGFloat widthRatio = [[self.options objectForKey:@"width"] floatValue];
CGFloat heightRatio = [[self.options objectForKey:@"height"] floatValue];
if (widthRatio > 0 && heightRatio > 0){
CGSize aspectRatio = CGSizeMake(widthRatio, heightRatio);
cropVC.customAspectRatio = aspectRatio;

}
cropVC.aspectRatioLockEnabled = ![[self.options objectForKey:@"freeStyleCropEnabled"] boolValue];
cropVC.resetAspectRatioEnabled = !cropVC.aspectRatioLockEnabled;
}

cropVC.title = [[self options] objectForKey:@"cropperToolbarTitle"];
cropVC.delegate = self;

cropVC.doneButtonTitle = [self.options objectForKey:@"cropperChooseText"];
cropVC.cancelButtonTitle = [self.options objectForKey:@"cropperCancelText"];

cropVC.modalPresentationStyle = UIModalPresentationFullScreen;

dispatch_async(dispatch_get_main_queue(), ^{
[[self getRootVC] presentViewController:cropVC animated:FALSE completion:nil];
});
});
}
#pragma mark - TOCropViewController Delegate
Expand Down