|
1 | 1 | package id.zelory.compressor;
|
2 | 2 |
|
| 3 | +import android.content.Context; |
3 | 4 | import android.graphics.Bitmap;
|
4 | 5 | import android.graphics.BitmapFactory;
|
5 | 6 | import android.graphics.Matrix;
|
6 | 7 | import android.media.ExifInterface;
|
| 8 | +import android.net.Uri; |
7 | 9 |
|
8 | 10 | import java.io.File;
|
| 11 | +import java.io.FileNotFoundException; |
9 | 12 | import java.io.FileOutputStream;
|
10 | 13 | import java.io.IOException;
|
| 14 | +import java.io.InputStream; |
11 | 15 |
|
12 | 16 | /**
|
13 | 17 | * Created on : June 18, 2016
|
@@ -41,6 +45,46 @@ static File compressImage(File imageFile, int reqWidth, int reqHeight, Bitmap.Co
|
41 | 45 | return new File(destinationPath);
|
42 | 46 | }
|
43 | 47 |
|
| 48 | + static File compressImage(Context context, Uri imageUri, int reqWidth, int reqHeight, Bitmap.CompressFormat compressFormat, int quality, String destinationPath) throws IOException { |
| 49 | + FileOutputStream fileOutputStream = null; |
| 50 | + File file = new File(destinationPath).getParentFile(); |
| 51 | + if (!file.exists()) { |
| 52 | + file.mkdirs(); |
| 53 | + } |
| 54 | + try { |
| 55 | + fileOutputStream = new FileOutputStream(destinationPath); |
| 56 | + // write the compressed bitmap at the destination specified by destinationPath. |
| 57 | + decodeSampledBitmapFromUri(context, imageUri, reqWidth, reqHeight).compress(compressFormat, quality, fileOutputStream); |
| 58 | + } finally { |
| 59 | + if (fileOutputStream != null) { |
| 60 | + fileOutputStream.flush(); |
| 61 | + fileOutputStream.close(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return new File(destinationPath); |
| 66 | + } |
| 67 | + |
| 68 | + static Bitmap decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException { |
| 69 | + BitmapFactory.Options options = new BitmapFactory.Options(); |
| 70 | + options.inJustDecodeBounds = true; |
| 71 | + InputStream inputStream = context.getContentResolver().openInputStream(imageUri); |
| 72 | + BitmapFactory.decodeStream(inputStream, null, options); |
| 73 | + |
| 74 | + // Calculate inSampleSize |
| 75 | + options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); |
| 76 | + |
| 77 | + // Decode bitmap with inSampleSize set |
| 78 | + options.inJustDecodeBounds = false; |
| 79 | + |
| 80 | + Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream, null, options); |
| 81 | + |
| 82 | + //check the rotation of the image and display it properly |
| 83 | + Matrix matrix = new Matrix(); |
| 84 | + scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); |
| 85 | + return scaledBitmap; |
| 86 | + } |
| 87 | + |
44 | 88 | static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqHeight) throws IOException {
|
45 | 89 | // First decode with inJustDecodeBounds=true to check dimensions
|
46 | 90 | BitmapFactory.Options options = new BitmapFactory.Options();
|
|
0 commit comments