Skip to content

Commit

Permalink
Different image scaling strategy
Browse files Browse the repository at this point in the history
I was getting OOM with decodeByteArray on large images, so I'm now giving
it options to scale the output bitmap.
```
options.inSampleSize = 2;
options.inDensity = 2;
options.inTargetDensity = 2;
options.inScaled = true;
```
Android is doing some math between inDensity and inTargetDensity. I don't know exactly what it's doing,
but 2 / 2 looks like it consistently scales the image in half. I've left in my code to to a second scaling pass
of the bitmap in case this doesn't work.
  • Loading branch information
petabyt committed May 5, 2024
1 parent da7bc15 commit 5d2f968
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/camlib/CamlibBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static String parseErr(int rc) {
case PTP_NO_PERM: return "Invalid permissions.";
case PTP_OPEN_FAIL: return "Couldn't connect to device.";
case WiFiComm.NOT_AVAILABLE: return "WiFi not ready yet.";
case WiFiComm.NOT_CONNECTED: return "WiFi is not connected.";
case WiFiComm.NOT_CONNECTED: return "WiFi is not connected. (yet)";
case WiFiComm.UNSUPPORTED_SDK: return "Unsupported SDK";
default: return "Unknown error";
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/dev/danielc/fujiapp/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.nio.ByteBuffer;

// https://developer.android.com/reference/android/media/MediaCodec

public class Decoder {
MediaCodecInfo getCodecInfo(String mime) {
MediaCodecList list = new MediaCodecList(MediaCodecList.ALL_CODECS);
Expand Down
20 changes: 5 additions & 15 deletions app/src/main/java/dev/danielc/fujiapp/Gallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,11 @@ public void run() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LibUI.init(this);

LayoutInflater inf = getLayoutInflater();

View tab = LibUI.tabLayout();
View gallery = inf.inflate(R.layout.gallery, (ViewGroup)tab, false);
LibUI.addTab(tab, "Gallery", gallery);
LibUI.addTab(tab, "Remote", inf.inflate(R.layout.remote, (ViewGroup)tab, false));
LibUI.addTab(tab, "Scripts", Backend.cFujiScriptsScreen(this));

ConstraintLayout cl = new ConstraintLayout(this);
cl.addView(tab);
View gallery = inf.inflate(R.layout.gallery, null);

setContentView(cl);
setContentView(gallery);

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Expand Down Expand Up @@ -190,9 +180,9 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//MenuItem menuItem = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "scripts");
//menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
//menuItem.setIcon(R.drawable.baseline_terminal_24);
MenuItem scripts = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "scripts");
scripts.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
scripts.setIcon(R.drawable.baseline_terminal_24);
return true;
}
}
Expand Down
30 changes: 20 additions & 10 deletions app/src/main/java/dev/danielc/fujiapp/Viewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,37 @@ public void run() {

Backend.cSetProgressBarObj(null, 0);

// Scale image to acceptable texture size
bitmap = BitmapFactory.decodeByteArray(fileByteData, 0, fileByteData.length);
BitmapFactory.Options options = new BitmapFactory.Options();
if (imgX > GL10.GL_MAX_TEXTURE_SIZE) {
options.inSampleSize = 2;
options.inDensity = 2;
options.inTargetDensity = 2;
options.inScaled = true;
}
if (size > 15000000) {
options.inSampleSize = 8;
options.inDensity = 8;
options.inTargetDensity = 4;
options.inScaled = true;
}

bitmap = BitmapFactory.decodeByteArray(fileByteData, 0, fileByteData.length, options);

// Resizing didn't go as expected, we need to scale the bitmap again
if (bitmap.getWidth() > GL10.GL_MAX_TEXTURE_SIZE) {
float ratio = ((float) bitmap.getHeight()) / ((float) bitmap.getWidth());
// Will result in ~11mb tex, can do 4096, but uses 40ish megs, sometimes Android complains about OOM
// Might be able to increase for newer Androids
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap,
(int)(2048),
(int)(2048 * ratio),
false
);
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, (int)(2048), (int)(2048 * ratio), false);
bitmap.recycle();
bitmap = newBitmap;
}

if (handler == null) return;
handler.post(new Runnable() {
@Override
public void run() {
popupWindow.dismiss();
ZoomageView zoomageView = findViewById(R.id.zoom_view);
zoomageView.setImageBitmap(bitmap);

threadIsDone = true;
}
});
Expand All @@ -269,6 +278,7 @@ public void run() {

@Override
protected void onDestroy() {
bitmap.recycle();
bitmap = null;
Runtime.getRuntime().gc();
handler = null;
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_settings_remote_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15,9L9,9c-0.55,0 -1,0.45 -1,1v12c0,0.55 0.45,1 1,1h6c0.55,0 1,-0.45 1,-1L16,10c0,-0.55 -0.45,-1 -1,-1zM12,15c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM7.05,6.05l1.41,1.41C9.37,6.56 10.62,6 12,6s2.63,0.56 3.54,1.46l1.41,-1.41C15.68,4.78 13.93,4 12,4s-3.68,0.78 -4.95,2.05zM12,0C8.96,0 6.21,1.23 4.22,3.22l1.41,1.41C7.26,3.01 9.51,2 12,2s4.74,1.01 6.36,2.64l1.41,-1.41C17.79,1.23 15.04,0 12,0z"/>
</vector>

0 comments on commit 5d2f968

Please sign in to comment.