-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapLoader.java
More file actions
153 lines (132 loc) · 6.15 KB
/
BitmapLoader.java
File metadata and controls
153 lines (132 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.lorenzostanco.utils;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
/**
* Carica in memoria immagini da file e risorse nel modo più efficiente possibile.
*/
@SuppressWarnings("unused") public final class BitmapLoader {
public final static boolean LOG = true;
public final static String TAG = "BITMAP-LOADER";
/**
* Carica un'immagine da file alla massima dimensione possibile (senza subsample).
*/
public static Bitmap load(final String path) {
return load(path, 0, 0);
}
/**
* Carica un'immagine dalle risorse alla massima dimensione possibile (senza subsample).
*/
public static Bitmap load(final Resources res, final int resId) {
return load(res, resId, 0, 0);
}
/**
* Carica un'immagine da file. Se le dimensioni del contenitore non sono date,
* l'immagine verà caricata alla massima dimensione possibile (senza subsample).
* @param destW Larghezza del contenitore dell'immagine, 0 se non si conosce
* @param destH Altezza del contenitore dell'immagine, 0 se non si conosce
*/
public static Bitmap load(final String path, final int destW, final int destH) {
final int[] size = getSize(path);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inSampleSize = calculateSampleSize(size[0], size[1], destW, destH);
if (LOG) Log.i(TAG, String.format("Decoding from file, original size is (%d %d), output is downsampled by %d to fit (%d %d)", size[0], size[1], options.inSampleSize, destW, destH));
return BitmapFactory.decodeFile(path, options);
}
/**
* Carica un'immagine dalle risorse. Se le dimensioni del contenitore non sono date,
* l'immagine verà caricata alla massima dimensione possibile (senza subsample).
* @param destW Larghezza del contenitore dell'immagine, 0 se non si conosce
* @param destH Altezza del contenitore dell'immagine, 0 se non si conosce
*/
public static Bitmap load(final Resources res, final int resId, final int destW, final int destH) {
final int[] size = getSize(res, resId);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inSampleSize = calculateSampleSize(size[0], size[1], destW, destH);
if (LOG) Log.i(TAG, String.format("Decoding from resources, original size is (%d %d), output is downsampled by %d to fit (%d %d)", size[0], size[1], options.inSampleSize, destW, destH));
return BitmapFactory.decodeResource(res, resId, options);
}
/**
* Carica un'immagine dagli assets. Se le dimensioni del contenitore non sono date,
* l'immagine verà caricata alla massima dimensione possibile (senza subsample).
* @param path Path all'interno degli assets
* @param destW Larghezza del contenitore dell'immagine, 0 se non si conosce
* @param destH Altezza del contenitore dell'immagine, 0 se non si conosce
*/
public static Bitmap load(final AssetManager assets, final String path, final int destW, final int destH) {
final int[] size = getSize(assets, path);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inSampleSize = calculateSampleSize(size[0], size[1], destW, destH);
if (LOG) Log.i(TAG, String.format("Decoding from assets, original size is (%d %d), output is downsampled by %d to fit (%d %d)", size[0], size[1], options.inSampleSize, destW, destH));
try {
final InputStream is = assets.open(path);
final Bitmap ret = BitmapFactory.decodeStream(is, null, options);
is.close();
return ret;
} catch (IOException x) {
return null;
}
}
/**
* Ritorna le dimensioni (larghezza e altezza) di un'immagine da file
*/
public static int[] getSize(final String path) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
return new int[] { options.outWidth, options.outHeight };
}
/**
* Ritorna le dimensioni (larghezza e altezza) di un'immagine dalle risorse
*/
public static int[] getSize(final Resources res, final int resId) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
return new int[] { options.outWidth, options.outHeight };
}
/**
* Ritorna le dimensioni (larghezza e altezza) di un'immagine dagli assets
*/
public static int[] getSize(final AssetManager assets, final String path) {
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
final InputStream is = assets.open(path);
BitmapFactory.decodeStream(is, null, options);
is.close();
return new int[] { options.outWidth, options.outHeight };
} catch (IOException x) {
return new int[] { 0, 0 };
}
}
/**
* Calcola il migliore sample size (in potenze di 2) per caricare una immagine grande
* (di dimensioni sourceWH) in un contenitore piccolo (di dimensioni destWH). Una delle
* dimensioni del contenitore può essere passata 0, verrà calcolata in base al caso peggiore.
*/
private static int calculateSampleSize(final int sourceW, final int sourceH, int destW, int destH) {
int inSampleSize = 1;
// Se una delle dimensioni di destinazione non è data, calcolala
if (destW <= 0 && destH <= 0) return inSampleSize;
if (destW <= 0) destW = Math.round(destH * ((float)sourceW / (float)sourceH));
if (destH <= 0) destH = Math.round(destW / ((float)sourceW / (float)sourceH));
// Se l'immagine sorgente è più grande del contenitore
if (sourceW > destW || sourceH > destH) {
// Calculate ratios of height and width to requested height and width
final int ratioW = (int)Math.floor((float)sourceW / (float)destW);
final int ratioH = (int)Math.floor((float)sourceH / (float)destH);
// Choose the smallest ratio as inSampleSize value, this will guarantee a final
// image with both dimensions larger than or equal to the requested height and width.
inSampleSize = Math.min(ratioW, ratioH);
}
return Math.max(inSampleSize, 1);
}
}