Improve extremely inefficient darkness-checking logic. (#2639)

* Improve extremely inefficient darkness-checking logic.

* Use ExifInterface from AndroidX instead of android.media.

* Fix false-positive detekt check.

* Fix false-positive detekt check.
This commit is contained in:
Dmitry Brant 2019-03-19 10:53:36 -04:00 committed by Vivek Maskara
parent bc9d83a47c
commit adf23c257f
9 changed files with 38 additions and 100 deletions

View file

@ -1,22 +0,0 @@
package fr.free.nrw.commons.utils;
import android.graphics.BitmapRegionDecoder;
import java.io.FileInputStream;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class BitmapRegionDecoderWrapper {
@Inject
public BitmapRegionDecoderWrapper() {
}
public BitmapRegionDecoder newInstance(FileInputStream file, boolean isSharable) throws IOException {
return BitmapRegionDecoder.newInstance(file, isSharable);
}
}

View file

@ -3,10 +3,10 @@ package fr.free.nrw.commons.utils;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
@ -24,6 +24,7 @@ import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import androidx.exifinterface.media.ExifInterface;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import timber.log.Timber;
@ -87,33 +88,26 @@ public class ImageUtils {
}
/**
* @param bitmapRegionDecoder BitmapRegionDecoder for the image we wish to process
* @return IMAGE_OK if image is neither dark nor blurry or if the input bitmapRegionDecoder provided is null
* @return IMAGE_OK if image is not too dark
* IMAGE_DARK if image is too dark
*/
static @Result
int checkIfImageIsTooDark(BitmapRegionDecoder bitmapRegionDecoder) {
if (bitmapRegionDecoder == null) {
Timber.e("Expected bitmapRegionDecoder was null");
return IMAGE_OK;
static @Result int checkIfImageIsTooDark(String imagePath) {
long millis = System.currentTimeMillis();
try {
Bitmap bmp = new ExifInterface(imagePath).getThumbnailBitmap();
if (bmp == null) {
bmp = BitmapFactory.decodeFile(imagePath);
}
if (checkIfImageIsDark(bmp)) {
return IMAGE_DARK;
}
} catch (Exception e) {
Timber.d(e, "Error while checking image darkness.");
} finally {
Timber.d("Checking image darkness took " + (System.currentTimeMillis() - millis) + " ms.");
}
int loadImageHeight = bitmapRegionDecoder.getHeight();
int loadImageWidth = bitmapRegionDecoder.getWidth();
int checkImageTopPosition = 0;
int checkImageLeftPosition = 0;
Timber.v("left: " + checkImageLeftPosition + " right: " + loadImageWidth + " top: " + checkImageTopPosition + " bottom: " + loadImageHeight);
Rect rect = new Rect(checkImageLeftPosition,checkImageTopPosition, loadImageWidth, loadImageHeight);
Bitmap processBitmap = bitmapRegionDecoder.decodeRegion(rect,null);
if (checkIfImageIsDark(processBitmap)) {
return IMAGE_DARK;
}
return IMAGE_OK;
}
@ -147,7 +141,6 @@ public class ImageUtils {
int bitmapHeight = bitmap.getHeight();
int allPixelsCount = bitmapWidth * bitmapHeight;
Timber.d("total %s", Integer.toString(allPixelsCount));
int numberOfBrightPixels = 0;
int numberOfMediumBrightnessPixels = 0;
double brightPixelThreshold = 0.025 * allPixelsCount;

View file

@ -1,7 +1,5 @@
package fr.free.nrw.commons.utils;
import android.graphics.BitmapRegionDecoder;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -17,9 +15,8 @@ public class ImageUtilsWrapper {
}
public Single<Integer> checkIfImageIsTooDark(BitmapRegionDecoder bitmapRegionDecoder) {
int isImageDark = ImageUtils.checkIfImageIsTooDark(bitmapRegionDecoder);
return Single.just(isImageDark)
public Single<Integer> checkIfImageIsTooDark(String bitmapPath) {
return Single.just(ImageUtils.checkIfImageIsTooDark(bitmapPath))
.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.computation());
}