You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by GitBox <gi...@apache.org> on 2018/07/25 05:16:47 UTC

[GitHub] therajanmaurya closed pull request #11: feat: Image compression before upload

therajanmaurya closed pull request #11: feat: Image compression before upload
URL: https://github.com/apache/fineract-cn-mobile/pull/11
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/app/build.gradle b/app/build.gradle
index 884ac3a..713fc16 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -117,6 +117,9 @@ dependencies {
     //Sticky header RecyclerView
     implementation 'org.zakariya.stickyheaders:stickyheaders:0.7.6'
 
+    // Compressor for Image Compression
+    implementation 'id.zelory:compressor:2.1.0'
+
     //Material Stepper UI library
     implementation 'com.stepstone.stepper:material-stepper:3.3.0'
 
diff --git a/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardBottomSheet.java b/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardBottomSheet.java
index 86180fa..2779125 100644
--- a/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardBottomSheet.java
+++ b/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardBottomSheet.java
@@ -8,7 +8,7 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.pm.PackageManager;
-import android.graphics.Bitmap;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.provider.MediaStore;
@@ -16,6 +16,7 @@
 import android.support.design.widget.BottomSheetBehavior;
 import android.support.design.widget.BottomSheetDialog;
 import android.support.design.widget.TextInputLayout;
+import android.support.v4.content.FileProvider;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.Log;
@@ -32,7 +33,6 @@
 import org.apache.fineract.utils.ValidationUtil;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 
 import javax.inject.Inject;
@@ -40,10 +40,11 @@
 import butterknife.BindView;
 import butterknife.ButterKnife;
 import butterknife.OnClick;
+import id.zelory.compressor.Compressor;
 
 /**
  * @author Rajan Maurya
- *         On 01/08/17.
+ * On 01/08/17.
  */
 public class UploadIdentificationCardBottomSheet extends FineractBaseBottomSheetDialogFragment
         implements UploadIdentificationCardContract.View, TextWatcher {
@@ -73,10 +74,12 @@
     @Inject
     UploadIdentificationCardPresenter uploadIdentificationCardPresenter;
 
+    private Compressor imageCompressor;
+
     View rootView;
 
     private BottomSheetBehavior behavior;
-    private File cachePath;
+    private File cameraImage, compressedImage;
     private String customerIdentifier;
     private String identificationNumber;
 
@@ -92,6 +95,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
         behavior = BottomSheetBehavior.from((View) rootView.getParent());
         ((FineractBaseActivity) getActivity()).getActivityComponent().inject(this);
         uploadIdentificationCardPresenter.attachView(this);
+        imageCompressor = new Compressor(getActivity());
         ButterKnife.bind(this, rootView);
 
         showUserInterface();
@@ -112,7 +116,7 @@ public void onUploadIdentificationCard() {
 
             uploadIdentificationCardPresenter.uploadIdentificationCardScan(customerIdentifier,
                     identificationNumber, etIdentifier.getText().toString().trim(),
-                    etDescription.getText().toString().trim(), cachePath);
+                    etDescription.getText().toString().trim(), compressedImage);
         }
     }
 
@@ -138,8 +142,22 @@ public void checkCameraPermission() {
 
     @Override
     public void openCamera() {
+
         Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
+
         if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
+
+            cameraImage = new File(getActivity().getExternalCacheDir() +
+                    File.separator + "scan.png");
+
+            if (cameraImage.exists()) {
+                cameraImage.delete();
+            }
+
+            Uri photoURI = FileProvider.getUriForFile(getActivity(),
+                    "org.apache.fineract.fileprovider", cameraImage);
+
+            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
             startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
         }
     }
@@ -147,10 +165,12 @@ public void openCamera() {
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
-            Bundle extras = data.getExtras();
-            Bitmap imageBitmap = (Bitmap) extras.get("data");
             etSelectFile.setText(getString(R.string.scan_file));
-            saveImageInCache(imageBitmap);
+            try {
+                compressedImage = imageCompressor.compressToFile(cameraImage);
+            } catch (IOException e) {
+                Log.d(UploadIdentificationCardBottomSheet.class.getSimpleName(), e.toString());
+            }
         }
     }
 
@@ -167,22 +187,6 @@ public void requestPermission() {
                 ConstantKeys.PERMISSIONS_CAMERA_STATUS);
     }
 
-    @Override
-    public void saveImageInCache(Bitmap bitmap) {
-        try {
-            File outputDir = getActivity().getCacheDir();
-            File outputFile = File.createTempFile("scan", "png", outputDir);
-            cachePath = outputFile;
-
-            // overwrites this image every time
-            FileOutputStream stream = new FileOutputStream(outputFile);
-            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
-            stream.close();
-        } catch (IOException e) {
-            Log.d(LOG_TAG, e.getLocalizedMessage());
-        }
-    }
-
     @Override
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
             @NonNull int[] grantResults) {
diff --git a/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardContract.java b/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardContract.java
index 03e0c3a..823fc58 100644
--- a/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardContract.java
+++ b/app/src/main/java/org/apache/fineract/ui/online/identification/uploadidentificationscan/UploadIdentificationCardContract.java
@@ -1,7 +1,5 @@
 package org.apache.fineract.ui.online.identification.uploadidentificationscan;
 
-import android.graphics.Bitmap;
-
 import org.apache.fineract.ui.base.MvpView;
 
 import java.io.File;
@@ -22,8 +20,6 @@
 
         void requestPermission();
 
-        void saveImageInCache(Bitmap bitmap);
-
         void showScanUploadedSuccessfully();
 
         void showProgressDialog();
diff --git a/app/src/main/res/xml/filepaths.xml b/app/src/main/res/xml/filepaths.xml
index a0bcef4..9388ff0 100644
--- a/app/src/main/res/xml/filepaths.xml
+++ b/app/src/main/res/xml/filepaths.xml
@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <paths xmlns:android="http://schemas.android.com/apk/res/android">
-    <cache-path name="shared_images" path="images/"/>
+    <external-cache-path name="cache" path="/" />
 </paths>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services