Skip to content

Commit

Permalink
- Compressor.java : Added method for compressing and copying the imag…
Browse files Browse the repository at this point in the history
…e using the Uri.

- ImageUti.java : Method to fetch and compress the image using the Uri.
  • Loading branch information
Mohammed Rampurawala committed Jan 4, 2018
1 parent d698334 commit bf2f8ae
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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);

// 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

0 comments on commit bf2f8ae

Please sign in to comment.