From 56012287b04e072f931839710b16428e012d5982 Mon Sep 17 00:00:00 2001 From: Nalin Angrish Date: Wed, 14 Oct 2020 18:18:12 +0530 Subject: [PATCH] Added Bitmap compression for faster and effective loading. Due to this compression, the application can handle more images than before and can run smoothly on low-end devices too --- .idea/misc.xml | 7 +++ app/build.gradle | 5 +- .../nalinstudios/iscan/EditViewActivity.java | 49 ++++++++++--------- .../nalinstudios/iscan/internal/Statics.java | 2 +- .../iscan/scanlibrary/ResultFragment.java | 5 +- .../iscan/scanlibrary/ScanFragment.java | 5 +- .../nalinstudios/iscan/scanlibrary/Utils.java | 15 ++++-- 7 files changed, 51 insertions(+), 37 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index ef45395..b74cba2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -14,4 +14,11 @@ + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index e1d5f23..df25cd5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.nalinstudios.iscan" minSdkVersion 19 targetSdkVersion 28 - versionCode 150 - versionName "1.5.0" + versionCode 151 + versionName "1.5.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled = true } @@ -46,4 +46,5 @@ dependencies { implementation('itext:itext:4.2.1') { exclude group: 'jfree', module: 'jfreechart' } + implementation 'id.zelory:compressor:2.0.0' } diff --git a/app/src/main/java/com/nalinstudios/iscan/EditViewActivity.java b/app/src/main/java/com/nalinstudios/iscan/EditViewActivity.java index 5d5e791..a066918 100644 --- a/app/src/main/java/com/nalinstudios/iscan/EditViewActivity.java +++ b/app/src/main/java/com/nalinstudios/iscan/EditViewActivity.java @@ -59,30 +59,35 @@ protected void main(){ String sessionDir = getApplication().getSharedPreferences("IScan", MODE_PRIVATE).getString("sessionName", "hello"); dir = new File(getFilesDir(), sessionDir); - FragmentManager manager = getFragmentManager(); + final FragmentManager manager = getFragmentManager(); - for (int i = 0; i < dir.listFiles().length; i++) { - FragmentTransaction transaction = manager.beginTransaction(); - - File imageFile = dir.listFiles()[i]; - Bundle args = new Bundle(); - args.putParcelable(ScanConstants.SCANNED_RESULT, Uri.fromFile(imageFile)); - args.putString(ScanConstants.SCAN_FILE, imageFile.getAbsolutePath()); - ResultFragment result = new ResultFragment(); - result.setArguments(args); - - - LinearLayout l = new LinearLayout(this); - l.setId(View.generateViewId()); - transaction.add(l.getId(), result, "result-"+i); - LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, LinearLayout.LayoutParams.MATCH_PARENT); - l.setLayoutParams(param); - ((LinearLayout)findViewById(R.id.viewList)).addView(l,i); - findViewById(R.id.viewList).invalidate(); - fragList.add(result); - transaction.commit(); + new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < dir.listFiles().length; i++) { + FragmentTransaction transaction = manager.beginTransaction(); + + File imageFile = dir.listFiles()[i]; + Bundle args = new Bundle(); + args.putParcelable(ScanConstants.SCANNED_RESULT, Uri.fromFile(imageFile)); + args.putString(ScanConstants.SCAN_FILE, imageFile.getAbsolutePath()); + ResultFragment result = new ResultFragment(); + result.setArguments(args); + + + LinearLayout l = new LinearLayout(EditViewActivity.this); + l.setId(View.generateViewId()); + transaction.add(l.getId(), result, "result-"+i); + LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, LinearLayout.LayoutParams.MATCH_PARENT); + l.setLayoutParams(param); + ((LinearLayout)findViewById(R.id.viewList)).addView(l,i); + findViewById(R.id.viewList).invalidate(); + fragList.add(result); + transaction.commit(); - } + } + } + }).start(); Log.println(Log.ASSERT, "count", ((LinearLayout)findViewById(R.id.viewList)).getChildCount()+""); } diff --git a/app/src/main/java/com/nalinstudios/iscan/internal/Statics.java b/app/src/main/java/com/nalinstudios/iscan/internal/Statics.java index b171684..cccc873 100644 --- a/app/src/main/java/com/nalinstudios/iscan/internal/Statics.java +++ b/app/src/main/java/com/nalinstudios/iscan/internal/Statics.java @@ -78,7 +78,7 @@ public static void createPdf(Application app, String name) throws IOException, D float ph = rat*ih; Bitmap page = Bitmap.createScaledBitmap(image,(int)pw,(int)ph,true); // scale the bitmap so that the page width is standard (of an A4 size) - doc.setPageSize(new Rectangle(page.getWidth(), page.getHeight())); + doc.setPageSize(new Rectangle(pw, ph)); doc.newPage(); Image img = Image.getInstance(toByteArray(page)); doc.add(img); diff --git a/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ResultFragment.java b/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ResultFragment.java index 2bccb27..eea2c0f 100644 --- a/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ResultFragment.java +++ b/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ResultFragment.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.util.Objects; /** @@ -94,9 +93,7 @@ private void init() { */ private void getBitmap() { Uri uri = getUri(); - try{ - onReceiveBitmap(Utils.getBitmap(getActivity(), uri)); - }catch (IOException e){e.printStackTrace();} + onReceiveBitmap(Utils.getBitmap(getActivity(), uri)); } diff --git a/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ScanFragment.java b/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ScanFragment.java index c9ddec2..b1f5f73 100644 --- a/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ScanFragment.java +++ b/app/src/main/java/com/nalinstudios/iscan/scanlibrary/ScanFragment.java @@ -24,7 +24,6 @@ import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; @@ -92,9 +91,7 @@ public void run() { */ private void getBitmap() { Uri uri = getUri(); - try{ - onReceiveBitmap(Utils.getBitmap(getActivity(), uri)); - }catch (IOException e){e.printStackTrace();} + onReceiveBitmap(Utils.getBitmap(getActivity(), uri)); } diff --git a/app/src/main/java/com/nalinstudios/iscan/scanlibrary/Utils.java b/app/src/main/java/com/nalinstudios/iscan/scanlibrary/Utils.java index be2297d..2bf3480 100644 --- a/app/src/main/java/com/nalinstudios/iscan/scanlibrary/Utils.java +++ b/app/src/main/java/com/nalinstudios/iscan/scanlibrary/Utils.java @@ -3,9 +3,11 @@ import android.content.Context; import android.graphics.Bitmap; import android.net.Uri; -import android.provider.MediaStore; +import android.util.Log; -import java.io.IOException; +import java.io.File; + +import id.zelory.compressor.Compressor; /** * The class containing helper method(s) @@ -31,8 +33,13 @@ static Uri getUri(Context context, Bitmap bitmap) { * @param uri the URI to obtain image from. * @return the obtained bitmap */ - public static Bitmap getBitmap(Context ctx, final Uri uri) throws IOException { - return MediaStore.Images.Media.getBitmap(ctx.getContentResolver(), uri); + public static Bitmap getBitmap(Context ctx, final Uri uri){ + Bitmap img = new Compressor(ctx) + .setQuality(70) + .compressToBitmap(new File(uri.getPath())); + Log.println(Log.ASSERT, "memory used: ", (img.getAllocationByteCount()/1024)+" kb"); + return img; } + }