|
3 | 3 | import android.app.WallpaperManager;
|
4 | 4 | import android.content.Context;
|
5 | 5 | import android.graphics.Bitmap;
|
6 |
| -import android.graphics.BitmapRegionDecoder; |
| 6 | +import android.graphics.BitmapFactory; |
7 | 7 | import android.graphics.Color;
|
8 |
| -import android.graphics.Rect; |
9 | 8 | import android.net.Uri;
|
| 9 | + |
10 | 10 | import androidx.annotation.IntDef;
|
11 | 11 | import androidx.annotation.Nullable;
|
12 | 12 |
|
|
24 | 24 | import java.lang.annotation.Retention;
|
25 | 25 | import java.lang.annotation.RetentionPolicy;
|
26 | 26 |
|
| 27 | +import androidx.exifinterface.media.ExifInterface; |
27 | 28 | import fr.free.nrw.commons.R;
|
28 | 29 | import fr.free.nrw.commons.location.LatLng;
|
29 | 30 | import timber.log.Timber;
|
@@ -87,33 +88,26 @@ public class ImageUtils {
|
87 | 88 | }
|
88 | 89 |
|
89 | 90 | /**
|
90 |
| - * @param bitmapRegionDecoder BitmapRegionDecoder for the image we wish to process |
91 |
| - * @return IMAGE_OK if image is neither dark nor blurry or if the input bitmapRegionDecoder provided is null |
| 91 | + * @return IMAGE_OK if image is not too dark |
92 | 92 | * IMAGE_DARK if image is too dark
|
93 | 93 | */
|
94 |
| - static @Result |
95 |
| - int checkIfImageIsTooDark(BitmapRegionDecoder bitmapRegionDecoder) { |
96 |
| - if (bitmapRegionDecoder == null) { |
97 |
| - Timber.e("Expected bitmapRegionDecoder was null"); |
98 |
| - return IMAGE_OK; |
99 |
| - } |
100 |
| - |
101 |
| - int loadImageHeight = bitmapRegionDecoder.getHeight(); |
102 |
| - int loadImageWidth = bitmapRegionDecoder.getWidth(); |
103 |
| - |
104 |
| - int checkImageTopPosition = 0; |
105 |
| - int checkImageLeftPosition = 0; |
106 |
| - |
107 |
| - Timber.v("left: " + checkImageLeftPosition + " right: " + loadImageWidth + " top: " + checkImageTopPosition + " bottom: " + loadImageHeight); |
108 |
| - |
109 |
| - Rect rect = new Rect(checkImageLeftPosition,checkImageTopPosition, loadImageWidth, loadImageHeight); |
| 94 | + static @Result int checkIfImageIsTooDark(String imagePath) { |
| 95 | + long millis = System.currentTimeMillis(); |
| 96 | + try { |
| 97 | + Bitmap bmp = new ExifInterface(imagePath).getThumbnailBitmap(); |
| 98 | + if (bmp == null) { |
| 99 | + bmp = BitmapFactory.decodeFile(imagePath); |
| 100 | + } |
110 | 101 |
|
111 |
| - Bitmap processBitmap = bitmapRegionDecoder.decodeRegion(rect,null); |
| 102 | + if (checkIfImageIsDark(bmp)) { |
| 103 | + return IMAGE_DARK; |
| 104 | + } |
112 | 105 |
|
113 |
| - if (checkIfImageIsDark(processBitmap)) { |
114 |
| - return IMAGE_DARK; |
| 106 | + } catch (Exception e) { |
| 107 | + Timber.d(e, "Error while checking image darkness."); |
| 108 | + } finally { |
| 109 | + Timber.d("Checking image darkness took " + (System.currentTimeMillis() - millis) + " ms."); |
115 | 110 | }
|
116 |
| - |
117 | 111 | return IMAGE_OK;
|
118 | 112 | }
|
119 | 113 |
|
@@ -147,7 +141,6 @@ private static boolean checkIfImageIsDark(Bitmap bitmap) {
|
147 | 141 | int bitmapHeight = bitmap.getHeight();
|
148 | 142 |
|
149 | 143 | int allPixelsCount = bitmapWidth * bitmapHeight;
|
150 |
| - Timber.d("total %s", Integer.toString(allPixelsCount)); |
151 | 144 | int numberOfBrightPixels = 0;
|
152 | 145 | int numberOfMediumBrightnessPixels = 0;
|
153 | 146 | double brightPixelThreshold = 0.025 * allPixelsCount;
|
|
0 commit comments