Skip to content

Commit

Permalink
Merge pull request #2 from kredibel-id/compress
Browse files Browse the repository at this point in the history
[Update] compress, stable. Walhamdulillah.
  • Loading branch information
hangga authored Jun 1, 2022
2 parents b211fbe + c669658 commit 83ffbe0
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 20 deletions.
24 changes: 11 additions & 13 deletions app/src/main/java/id/co/kredibel/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import io.kredibel.picker.Picker;
import io.kredibel.picker.PickerListener;
import io.kredibel.picker.PickerObserver;

import java.io.File;

Expand All @@ -24,24 +26,20 @@ protected void onCreate(Bundle savedInstanceState) {
ImageView imgResult = findViewById(R.id.imgResult);
TextView txtPath = findViewById(R.id.txtPath);

findViewById(R.id.btnCamera).setOnClickListener(view -> picker.pickCamera(new PickerListener() {
PickerListener pickerListener = new PickerListener() {
@Override
public void onPicked(Uri uri, File file, Bitmap bitmap) {
imgResult.setImageURI(uri);
txtPath.setText(file.getAbsolutePath()+"(" + (file.exists()) + ")");
Log.d("CEK", "CEK-EXIST: Bitmap|Camera: " + (bitmap != null));
Log.d("CEK", "CEK-EXIST: File|Camera: " + (file.exists()) + "|" + file.getAbsolutePath());
//Log.d("CEK", "CEK-EXIST: Bitmap|Camera: " + (bitmap != null));
//Log.d("CEK", "CEK-EXIST: File|Camera: " + (file.exists()) + "|" + file.getAbsolutePath());
}
}));
};

findViewById(R.id.btnGalery).setOnClickListener(view -> picker.pickGallery(new PickerListener() {
@Override
public void onPicked(Uri uri, File file, Bitmap bitmap) {
imgResult.setImageURI(uri);
txtPath.setText(file.getAbsolutePath()+"(" + (file.exists()) + ")");
Log.d("CEK", "CEK-EXIST: Bitmap|Gallery: " + (bitmap != null));
Log.d("CEK", "CEK-EXIST: File|Gallery: " + (file.exists())+ "|" + file.getAbsolutePath());
}
}));
findViewById(R.id.btnChoose).setOnClickListener(view -> picker.pickImage(pickerListener));

findViewById(R.id.btnCamera).setOnClickListener(view -> picker.pickCamera(pickerListener));

findViewById(R.id.btnGalery).setOnClickListener(view -> picker.pickGallery(pickerListener));
}
}
13 changes: 11 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/btnGalery"
android:layout_toLeftOf="@+id/vCenter"
android:layout_toLeftOf="@+id/btnChoose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Galery"
Expand All @@ -24,13 +24,22 @@

<Button
android:id="@+id/btnCamera"
android:layout_toRightOf="@+id/vCenter"
android:layout_toRightOf="@+id/btnChoose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Camera"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/btnChoose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Choose"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>

<TextView
android:id="@+id/txtPath"
android:layout_below="@+id/btnCamera"
Expand Down
5 changes: 1 addition & 4 deletions picker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

group = 'io.kredibel' // <-- group name
archivesBaseName = "picker" // <-- artifact name
version = "0.0.4-beta2"
version = "0.0.4-beta5"

publishing {

Expand Down Expand Up @@ -59,11 +59,8 @@ android {
}

dependencies {

//implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
//implementation 'androidx.exifinterface:exifinterface:1.3.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
121 changes: 121 additions & 0 deletions picker/src/main/java/io/kredibel/picker/FileCompressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package io.kredibel.picker;

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;

public class FileCompressor {
//max width and height values of the compressed image is taken as 612x816
private final Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
private final String destinationDirectoryPath;

public FileCompressor(Context context) {
destinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
}

/*public static Bitmap getBitmapData(Context context, Intent data) {
Uri photoUri = null;
if (data != null && data.getData() != null) {
photoUri = data.getData();
}
String path = photoUri.getPath();
try {
InputStream imageStream = context.getContentResolver().openInputStream(photoUri);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
imageStream.close();
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}*/

static File compressImage(File imageFile, Bitmap.CompressFormat compressFormat, int quality, String destinationPath) throws IOException {
FileOutputStream fileOutputStream = null;
File file = new File(destinationPath).getParentFile();
if (!Objects.requireNonNull(file).exists()) {
file.mkdirs();
}
try {
fileOutputStream = new FileOutputStream(destinationPath);
// write the compressed bitmap at the destination specified by destinationPath.
decodeSampledBitmapFromFile(imageFile).compress(compressFormat, quality, fileOutputStream);
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
return new File(destinationPath);
}

static Bitmap decodeSampledBitmapFromFile(File imageFile) throws IOException {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap scaledBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
//check the rotation of the image and display it properly
ExifInterface exif;
exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
return scaledBitmap;
}

private static int calculateInSampleSize(BitmapFactory.Options options) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > 816 || width > 612) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= 816 && (halfWidth / inSampleSize) >= 612) {
inSampleSize *= 2;
}
}
return inSampleSize;
}

/*public FileCompressor setDestinationDirectoryPath(String destinationDirectoryPath) {
this.destinationDirectoryPath = destinationDirectoryPath;
return this;
}*/

public File compressToFile(File imageFile) throws IOException {
return compressToFile(imageFile, imageFile.getName());
}

public File compressToFile(File imageFile, String compressedFileName) throws IOException {
int quality = 50;
return compressImage(imageFile, compressFormat, quality, destinationDirectoryPath + File.separator + compressedFileName);
}

/*public Bitmap compressToBitmap(File imageFile) throws IOException {
return decodeSampledBitmapFromFile(imageFile);
}*/
}
4 changes: 4 additions & 0 deletions picker/src/main/java/io/kredibel/picker/Picker.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public void pickCamera(PickerListener pickerListener) {
public void pickGallery(PickerListener pickerListener) {
observer.pickGallery(pickerListener);
}

public void pickImage(PickerListener pickerListener){
observer.pickImage(pickerListener);
}
}
27 changes: 26 additions & 1 deletion picker/src/main/java/io/kredibel/picker/PickerObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.activity.result.ActivityResultRegistry;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void onActivityResult(ActivityResult result) {
};
private ActivityResultLauncher<String> imageLauncher;
private ActivityResultLauncher<Intent> cameraLauncher;

public PickerObserver(@NonNull AppCompatActivity activity) {
this.activity = activity;
this.registry = activity.getActivityResultRegistry();
Expand Down Expand Up @@ -90,7 +92,14 @@ File bitmapToFile(Bitmap bitmap) {
e.printStackTrace();
}
}
return file;
File compresedFile;
FileCompressor fileCompressor = new FileCompressor(activity);
try {
compresedFile = fileCompressor.compressToFile(file);
} catch (IOException e) {
compresedFile = file;
}
return compresedFile;
}

@Override
Expand Down Expand Up @@ -124,4 +133,20 @@ public void pickCamera(PickerListener pickerListener) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraLauncher.launch(intent);
}

public void pickImage(PickerListener pickerListener) {
final CharSequence[] options = {"From Camera", "From Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Pick Image!");
builder.setItems(options, (dialog, item) -> {
if (item == 0) {
pickCamera(pickerListener);
} else if (item == 1) {
pickGallery(pickerListener);
} else {
dialog.dismiss();
}
});
builder.show();
}
}

0 comments on commit 83ffbe0

Please sign in to comment.