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

Uri Support #91

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions compressor/src/main/java/id/zelory/compressor/Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;

import io.reactivex.Flowable;
import io.reactivex.annotations.Nullable;

/**
* Created on : June 18, 2016
Expand All @@ -22,9 +25,11 @@ public class Compressor {
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
private int quality = 80;
private String destinationDirectoryPath;
private WeakReference<Context> contextWeakReference;

public Compressor(Context context) {
destinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
contextWeakReference = new WeakReference<Context>(context);
}

public Compressor setMaxWidth(int maxWidth) {
Expand Down Expand Up @@ -61,6 +66,14 @@ public File compressToFile(File imageFile, String compressedFileName) throws IOE
destinationDirectoryPath + File.separator + compressedFileName);
}

public @Nullable File compressToUri(Uri imageUri, String compressedFileName) throws IOException {
if (contextWeakReference == null || contextWeakReference.get() == null) {
return null;
}
return ImageUtil.compressImage(contextWeakReference.get(),imageUri, maxWidth, maxHeight, compressFormat, quality,
destinationDirectoryPath + File.separator + compressedFileName);
}

public Bitmap compressToBitmap(File imageFile) throws IOException {
return ImageUtil.decodeSampledBitmapFromFile(imageFile, maxWidth, maxHeight);
}
Expand Down
44 changes: 44 additions & 0 deletions compressor/src/main/java/id/zelory/compressor/ImageUtil.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package id.zelory.compressor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Created on : June 18, 2016
Expand Down Expand Up @@ -41,6 +45,46 @@ static File compressImage(File imageFile, int reqWidth, int reqHeight, Bitmap.Co
return new File(destinationPath);
}

static File compressImage(Context context, Uri imageUri, int reqWidth, int reqHeight, Bitmap.CompressFormat compressFormat, int quality, String destinationPath) throws IOException {
FileOutputStream fileOutputStream = null;
File file = new File(destinationPath).getParentFile();
if (!file.exists()) {
file.mkdirs();
}
try {
fileOutputStream = new FileOutputStream(destinationPath);
// write the compressed bitmap at the destination specified by destinationPath.
decodeSampledBitmapFromUri(context, imageUri, reqWidth, reqHeight).compress(compressFormat, quality, fileOutputStream);
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}

return new File(destinationPath);
}

static Bitmap decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream inputStream = context.getContentResolver().openInputStream(imageUri);
BitmapFactory.decodeStream(inputStream, null, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the calculate sample size method?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks 😃


// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;

Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream, null, options);

//check the rotation of the image and display it properly
Matrix matrix = new Matrix();
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
return scaledBitmap;
}

static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqHeight) throws IOException {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
Expand Down