Feat[icon]: automatically resize cached icons to 256x256 and compress as JPEG

This commit is contained in:
artdeell 2023-08-19 11:14:36 +03:00 committed by ArtDev
parent ecbbbfcc4d
commit be0d98e09a

View File

@ -1,10 +1,15 @@
package net.kdt.pojavlaunch.modloaders.modpacks.imagecache; package net.kdt.pojavlaunch.modloaders.modpacks.imagecache;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import net.kdt.pojavlaunch.utils.DownloadUtils; import net.kdt.pojavlaunch.utils.DownloadUtils;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
class DownloadImageTask implements Runnable { class DownloadImageTask implements Runnable {
private static final float BITMAP_FINAL_DIMENSION = 256f;
private final ReadFromDiskTask mParentTask; private final ReadFromDiskTask mParentTask;
private int mRetryCount; private int mRetryCount;
DownloadImageTask(ReadFromDiskTask parentTask) { DownloadImageTask(ReadFromDiskTask parentTask) {
@ -28,6 +33,25 @@ class DownloadImageTask implements Runnable {
try { try {
IconCacheJanitor.waitForJanitorToFinish(); IconCacheJanitor.waitForJanitorToFinish();
DownloadUtils.downloadFile(mParentTask.imageUrl, mParentTask.cacheFile); DownloadUtils.downloadFile(mParentTask.imageUrl, mParentTask.cacheFile);
Bitmap bitmap = BitmapFactory.decodeFile(mParentTask.cacheFile.getAbsolutePath());
if(bitmap == null) return false;
int bitmapWidth = bitmap.getWidth(), bitmapHeight = bitmap.getHeight();
if(bitmapWidth <= BITMAP_FINAL_DIMENSION && bitmapHeight <= BITMAP_FINAL_DIMENSION) {
bitmap.recycle();
return true;
}
float imageRescaleRatio = Math.min(BITMAP_FINAL_DIMENSION/bitmapWidth, BITMAP_FINAL_DIMENSION/bitmapHeight);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap,
(int)(bitmapWidth * imageRescaleRatio),
(int)(bitmapHeight * imageRescaleRatio),
true);
bitmap.recycle();
if(resizedBitmap == bitmap) return true;
try (FileOutputStream fileOutputStream = new FileOutputStream(mParentTask.cacheFile)) {
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
} finally {
resizedBitmap.recycle();
}
return true; return true;
}catch (IOException e) { }catch (IOException e) {
e.printStackTrace(); e.printStackTrace();