Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Added Bitmap compression for faster and effective loading. Due to thi…
Browse files Browse the repository at this point in the history
…s compression, the application can handle more images than before and can run smoothly on low-end devices too
  • Loading branch information
Nalin-Angrish committed Oct 14, 2020
1 parent b1efc7d commit 5601228
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -46,4 +46,5 @@ dependencies {
implementation('itext:itext:4.2.1') {
exclude group: 'jfree', module: 'jfreechart'
}
implementation 'id.zelory:compressor:2.0.0'
}
49 changes: 27 additions & 22 deletions app/src/main/java/com/nalinstudios/iscan/EditViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()+"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

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

/**
Expand Down Expand Up @@ -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));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}


Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/nalinstudios/iscan/scanlibrary/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

}

0 comments on commit 5601228

Please sign in to comment.