Skip to content

Commit

Permalink
Some refactoring & couple fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
shliama committed Jun 21, 2016
1 parent ace3146 commit 89c47ff
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 54 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:2.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 2 additions & 1 deletion ucrop/src/main/java/com/yalantis/ucrop/callback/BitmapLoadCallback.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public interface BitmapLoadCallback {

void onBitmapLoaded(@NonNull Bitmap bitmap, @NonNull String imageInputPath, @NonNull String imageOutputPath);
void onBitmapLoaded(@NonNull Bitmap bitmap, @NonNull String imageInputPath, @Nullable String imageOutputPath);

void onFailure(@NonNull Exception bitmapWorkerException);

Expand Down
10 changes: 5 additions & 5 deletions ucrop/src/main/java/com/yalantis/ucrop/task/BitmapCropTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ public BitmapCropTask(@Nullable Bitmap viewBitmap,
@Override
@Nullable
protected Throwable doInBackground(Void... params) {
if (mViewBitmap == null || mViewBitmap.isRecycled()) {
return new NullPointerException("ViewBitmap is null or already recycled");
}
if (mCurrentImageRect.isEmpty()) {
if (mViewBitmap == null) {
return new NullPointerException("ViewBitmap is null");
} else if (mViewBitmap.isRecycled()) {
return new NullPointerException("ViewBitmap is recycled");
} else if (mCurrentImageRect.isEmpty()) {
return new NullPointerException("CurrentImageRect is empty");
}

float resizeScale = resize();

try {
crop(resizeScale);
mViewBitmap.recycle();
mViewBitmap = null;
} catch (Throwable throwable) {
return throwable;
Expand Down
28 changes: 19 additions & 9 deletions ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public BitmapWorkerResult(@Nullable Bitmap bitmapResult, @Nullable Exception bit
}

public BitmapLoadTask(@NonNull Context context,
@Nullable Uri inputUri, @Nullable Uri outputUri,
@NonNull Uri inputUri, @Nullable Uri outputUri,
int requiredWidth, int requiredHeight,
BitmapLoadCallback loadCallback) {
mContext = context;
Expand All @@ -76,8 +76,8 @@ public BitmapLoadTask(@NonNull Context context,
@Override
@NonNull
protected BitmapWorkerResult doInBackground(Void... params) {
if (mInputUri == null || mOutputUri == null) {
return new BitmapWorkerResult(null, new NullPointerException("Uri cannot be null"));
if (mInputUri == null) {
return new BitmapWorkerResult(null, new NullPointerException("Input Uri cannot be null"));
}

try {
Expand All @@ -97,14 +97,14 @@ protected BitmapWorkerResult doInBackground(Void... params) {
if (parcelFileDescriptor != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
} else {
return new BitmapWorkerResult(null, new NullPointerException("ParcelFileDescriptor was null for given Uri"));
return new BitmapWorkerResult(null, new NullPointerException("ParcelFileDescriptor was null for given Uri: [" + mInputUri + "]"));
}

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
if (options.outWidth == -1 || options.outHeight == -1) {
return new BitmapWorkerResult(null, new IllegalArgumentException("Bounds for bitmap could not be retrieved from Uri"));
return new BitmapWorkerResult(null, new IllegalArgumentException("Bounds for bitmap could not be retrieved from the Uri: [" + mInputUri + "]"));
}

options.inSampleSize = BitmapLoadUtils.calculateInSampleSize(options, mRequiredWidth, mRequiredHeight);
Expand All @@ -124,7 +124,7 @@ protected BitmapWorkerResult doInBackground(Void... params) {
}

if (decodeSampledBitmap == null) {
return new BitmapWorkerResult(null, new IllegalArgumentException("Bitmap could not be decoded from Uri"));
return new BitmapWorkerResult(null, new IllegalArgumentException("Bitmap could not be decoded from the Uri: [" + mInputUri + "]"));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Expand Down Expand Up @@ -177,8 +177,13 @@ private void processInputUri() throws NullPointerException, IOException {
}
}

private void copyFile(@NonNull Uri inputUri, @NonNull Uri outputUri) throws NullPointerException, IOException {
private void copyFile(@NonNull Uri inputUri, @Nullable Uri outputUri) throws NullPointerException, IOException {
Log.d(TAG, "copyFile");

if (outputUri == null) {
throw new NullPointerException("Output Uri is null - cannot copy image");
}

InputStream inputStream = null;
OutputStream outputStream = null;
try {
Expand All @@ -203,8 +208,13 @@ private void copyFile(@NonNull Uri inputUri, @NonNull Uri outputUri) throws Null
}
}

private void downloadFile(@NonNull Uri inputUri, @NonNull Uri outputUri) throws NullPointerException, IOException {
private void downloadFile(@NonNull Uri inputUri, @Nullable Uri outputUri) throws NullPointerException, IOException {
Log.d(TAG, "downloadFile");

if (outputUri == null) {
throw new NullPointerException("Output Uri is null - cannot download image");
}

OkHttpClient client = new OkHttpClient();

BufferedSource source = null;
Expand Down Expand Up @@ -241,7 +251,7 @@ private void downloadFile(@NonNull Uri inputUri, @NonNull Uri outputUri) throws
@Override
protected void onPostExecute(@NonNull BitmapWorkerResult result) {
if (result.mBitmapWorkerException == null) {
mBitmapLoadCallback.onBitmapLoaded(result.mBitmapResult, mInputUri.getPath(), mOutputUri.getPath());
mBitmapLoadCallback.onBitmapLoaded(result.mBitmapResult, mInputUri.getPath(), (mOutputUri == null) ? null : mOutputUri.getPath());
} else {
mBitmapLoadCallback.onFailure(result.mBitmapWorkerException);
}
Expand Down
33 changes: 30 additions & 3 deletions ucrop/src/main/java/com/yalantis/ucrop/util/BitmapLoadUtils.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.yalantis.ucrop.callback.BitmapLoadCallback;
import com.yalantis.ucrop.task.BitmapLoadTask;
Expand All @@ -25,7 +29,7 @@ public class BitmapLoadUtils {
private static final String TAG = "BitmapLoadUtils";

public static void decodeBitmapInBackground(@NonNull Context context,
@Nullable Uri uri, @Nullable Uri outputUri,
@NonNull Uri uri, @Nullable Uri outputUri,
int requiredWidth, int requiredHeight,
BitmapLoadCallback loadCallback) {

Expand All @@ -35,8 +39,7 @@ public static void decodeBitmapInBackground(@NonNull Context context,
public static Bitmap transformBitmap(@NonNull Bitmap bitmap, @NonNull Matrix transformMatrix) {
try {
Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transformMatrix, true);
if (bitmap != converted) {
bitmap.recycle();
if (!bitmap.sameAs(converted)) {
bitmap = converted;
}
} catch (OutOfMemoryError error) {
Expand Down Expand Up @@ -112,6 +115,30 @@ public static int exifToTranslation(int exifOrientation) {
return translation;
}

/**
* This method calculates maximum size of both width and height of bitmap.
* It is twice the device screen diagonal for default implementation.
*
* @return - max bitmap size in pixels.
*/
@SuppressWarnings({"SuspiciousNameCombination", "deprecation"})
public static int calculateMaxBitmapSize(@NonNull Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Point size = new Point();
int width, height;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
width = size.x;
height = size.y;
} else {
width = display.getWidth();
height = display.getHeight();
}
return (int) Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) * 2;
}

@SuppressWarnings("ConstantConditions")
public static void close(@Nullable Closeable c) {
if (c != null && c instanceof Closeable) { // java.lang.IncompatibleClassChangeError: interface not implemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class FastBitmapDrawable extends Drawable {
Expand All @@ -39,8 +38,7 @@ public FastBitmapDrawable(Bitmap b) {
@Override
public void draw(Canvas canvas) {
if (mBitmap != null && !mBitmap.isRecycled()) {
final Rect r = getBounds();
canvas.drawBitmap(mBitmap, null, r, mPaint);
canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
}
}

Expand Down
34 changes: 3 additions & 31 deletions ucrop/src/main/java/com/yalantis/ucrop/view/TransformImageView.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

import com.yalantis.ucrop.callback.BitmapLoadCallback;
Expand Down Expand Up @@ -109,7 +105,7 @@ public void setMaxBitmapSize(int maxBitmapSize) {

public int getMaxBitmapSize() {
if (mMaxBitmapSize <= 0) {
mMaxBitmapSize = calculateMaxBitmapSize();
mMaxBitmapSize = BitmapLoadUtils.calculateMaxBitmapSize(getContext());
}
return mMaxBitmapSize;
}
Expand All @@ -133,14 +129,14 @@ public String getImageOutputPath() {
* @param imageUri - image Uri
* @throws Exception - can throw exception if having problems with decoding Uri or OOM.
*/
public void setImageUri(@NonNull Uri imageUri, @NonNull Uri outputUri) throws Exception {
public void setImageUri(@NonNull Uri imageUri, @Nullable Uri outputUri) throws Exception {
int maxBitmapSize = getMaxBitmapSize();

BitmapLoadUtils.decodeBitmapInBackground(getContext(), imageUri, outputUri, maxBitmapSize, maxBitmapSize,
new BitmapLoadCallback() {

@Override
public void onBitmapLoaded(@NonNull Bitmap bitmap, @NonNull String imageInputPath, @NonNull String imageOutputPath) {
public void onBitmapLoaded(@NonNull Bitmap bitmap, @NonNull String imageInputPath, @Nullable String imageOutputPath) {
mImageInputPath = imageInputPath;
mImageOutputPath = imageOutputPath;

Expand Down Expand Up @@ -255,30 +251,6 @@ protected void init() {
setScaleType(ScaleType.MATRIX);
}

/**
* This method calculates maximum size of both width and height of bitmap.
* It is twice the device screen diagonal for default implementation.
*
* @return - max bitmap size in pixels.
*/
@SuppressWarnings({"SuspiciousNameCombination", "deprecation"})
protected int calculateMaxBitmapSize() {
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Point size = new Point();
int width, height;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
width = size.x;
height = size.y;
} else {
width = display.getWidth();
height = display.getHeight();
}
return (int) Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) * 2;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Expand Down
6 changes: 5 additions & 1 deletion ucrop/src/main/jni/uCrop.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ JNIEXPORT jboolean JNICALL Java_com_yalantis_ucrop_task_BitmapCropTask_cropCImg
x0 = left, y0 = top,
x1 = left + width - 1, y1 = top + height - 1;

// TODO abs what is it good for? - absolutely nothing
const int
size_x = cimg::abs(img.width() * resizeScale), size_y = cimg::abs(img.height() * resizeScale),
size_z = -100, size_c = -100, interpolation_type = 1;
Expand All @@ -53,7 +54,7 @@ JNIEXPORT jboolean JNICALL Java_com_yalantis_ucrop_task_BitmapCropTask_cropCImg
img.resize(size_x, size_y, size_z, size_c, interpolation_type, boundary_conditions, centering_x, centering_y, centering_z, centering_c);
}


// TODO why allocate memory at all?
if (!(img.width() == width && img.height() == height && angle == 0)) {

// Create warp field.
Expand Down Expand Up @@ -86,7 +87,10 @@ JNIEXPORT jboolean JNICALL Java_com_yalantis_ucrop_task_BitmapCropTask_cropCImg
img.save(file_result_path);
}

// todo
// delete img;
~img;

env->ReleaseStringUTFChars(pathSource, file_source_path);
env->ReleaseStringUTFChars(pathResult, file_result_path);

Expand Down

0 comments on commit 89c47ff

Please sign in to comment.