mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
make blurring draft compilable
This commit is contained in:
@@ -1,79 +1,80 @@
|
||||
package org.thoughtcrime.securesms.scribbles;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.RectF;
|
||||
import android.os.Build;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.RectF;
|
||||
// import android.os.Build;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import com.google.firebase.ml.vision.FirebaseVision;
|
||||
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
|
||||
import com.google.firebase.ml.vision.face.FirebaseVisionFace;
|
||||
import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetector;
|
||||
import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions;
|
||||
// import com.annimon.stream.Stream;
|
||||
// import com.google.firebase.ml.vision.FirebaseVision;
|
||||
// import com.google.firebase.ml.vision.common.FirebaseVisionImage;
|
||||
// import com.google.firebase.ml.vision.face.FirebaseVisionFace;
|
||||
// import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetector;
|
||||
// import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions;
|
||||
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
// import java.io.IOException;
|
||||
// import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
// import java.util.concurrent.CountDownLatch;
|
||||
// import java.util.concurrent.TimeUnit;
|
||||
|
||||
class FirebaseFaceDetector implements FaceDetector {
|
||||
|
||||
private static final String TAG = Log.tag(FirebaseFaceDetector.class);
|
||||
// private static final String TAG = FirebaseFaceDetector.class.getSimpleName();
|
||||
|
||||
private static final long MAX_SIZE = 1000 * 1000;
|
||||
// private static final long MAX_SIZE = 1000 * 1000;
|
||||
|
||||
@Override
|
||||
public List<RectF> detect(Bitmap source) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
int performanceMode = getPerformanceMode(source);
|
||||
Log.d(TAG, "Using performance mode " + performanceMode + " (API " + Build.VERSION.SDK_INT + ", " + source.getWidth() + "x" + source.getHeight() + ")");
|
||||
|
||||
FirebaseVisionFaceDetectorOptions options = new FirebaseVisionFaceDetectorOptions.Builder()
|
||||
.setPerformanceMode(performanceMode)
|
||||
.setMinFaceSize(0.05f)
|
||||
.setContourMode(FirebaseVisionFaceDetectorOptions.NO_CONTOURS)
|
||||
.setLandmarkMode(FirebaseVisionFaceDetectorOptions.NO_LANDMARKS)
|
||||
.setClassificationMode(FirebaseVisionFaceDetectorOptions.NO_CLASSIFICATIONS)
|
||||
.build();
|
||||
|
||||
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(source);
|
||||
List<RectF> output = new ArrayList<>();
|
||||
|
||||
try (FirebaseVisionFaceDetector detector = FirebaseVision.getInstance().getVisionFaceDetector(options)) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
detector.detectInImage(image)
|
||||
.addOnSuccessListener(firebaseVisionFaces -> {
|
||||
output.addAll(Stream.of(firebaseVisionFaces)
|
||||
.map(FirebaseVisionFace::getBoundingBox)
|
||||
.map(r -> new RectF(r.left, r.top, r.right, r.bottom))
|
||||
.toList());
|
||||
latch.countDown();
|
||||
})
|
||||
.addOnFailureListener(e -> latch.countDown());
|
||||
|
||||
latch.await(15, TimeUnit.SECONDS);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Failed to close!", e);
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, e);
|
||||
// long startTime = System.currentTimeMillis();
|
||||
//
|
||||
// int performanceMode = getPerformanceMode(source);
|
||||
// Log.d(TAG, "Using performance mode " + performanceMode + " (API " + Build.VERSION.SDK_INT + ", " + source.getWidth() + "x" + source.getHeight() + ")");
|
||||
//
|
||||
// FirebaseVisionFaceDetectorOptions options = new FirebaseVisionFaceDetectorOptions.Builder()
|
||||
// .setPerformanceMode(performanceMode)
|
||||
// .setMinFaceSize(0.05f)
|
||||
// .setContourMode(FirebaseVisionFaceDetectorOptions.NO_CONTOURS)
|
||||
// .setLandmarkMode(FirebaseVisionFaceDetectorOptions.NO_LANDMARKS)
|
||||
// .setClassificationMode(FirebaseVisionFaceDetectorOptions.NO_CLASSIFICATIONS)
|
||||
// .build();
|
||||
//
|
||||
//
|
||||
// FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(source);
|
||||
// List<RectF> output = new ArrayList<>();
|
||||
//
|
||||
// try (FirebaseVisionFaceDetector detector = FirebaseVision.getInstance().getVisionFaceDetector(options)) {
|
||||
// CountDownLatch latch = new CountDownLatch(1);
|
||||
//
|
||||
// detector.detectInImage(image)
|
||||
// .addOnSuccessListener(firebaseVisionFaces -> {
|
||||
// output.addAll(Stream.of(firebaseVisionFaces)
|
||||
// .map(FirebaseVisionFace::getBoundingBox)
|
||||
// .map(r -> new RectF(r.left, r.top, r.right, r.bottom))
|
||||
// .toList());
|
||||
// latch.countDown();
|
||||
// })
|
||||
// .addOnFailureListener(e -> latch.countDown());
|
||||
//
|
||||
// latch.await(15, TimeUnit.SECONDS);
|
||||
// } catch (IOException e) {
|
||||
// Log.w(TAG, "Failed to close!", e);
|
||||
// } catch (InterruptedException e) {
|
||||
// Log.w(TAG, e);
|
||||
// }
|
||||
//
|
||||
// Log.d(TAG, "Finished in " + (System.currentTimeMillis() - startTime) + " ms");
|
||||
//
|
||||
// return output;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
Log.d(TAG, "Finished in " + (System.currentTimeMillis() - startTime) + " ms");
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
private static int getPerformanceMode(Bitmap source) {
|
||||
if (Build.VERSION.SDK_INT < 28) {
|
||||
return FirebaseVisionFaceDetectorOptions.FAST;
|
||||
}
|
||||
|
||||
return source.getWidth() * source.getHeight() < MAX_SIZE ? FirebaseVisionFaceDetectorOptions.ACCURATE
|
||||
: FirebaseVisionFaceDetectorOptions.FAST;
|
||||
}
|
||||
//
|
||||
// private static int getPerformanceMode(Bitmap source) {
|
||||
// if (Build.VERSION.SDK_INT < 28) {
|
||||
// return FirebaseVisionFaceDetectorOptions.FAST;
|
||||
// }
|
||||
//
|
||||
// return source.getWidth() * source.getHeight() < MAX_SIZE ? FirebaseVisionFaceDetectorOptions.ACCURATE
|
||||
// : FirebaseVisionFaceDetectorOptions.FAST;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ public class ScribbleActivity extends PassphraseRequiredActionBarActivity {
|
||||
imageEditorFragment = initFragment(R.id.scribble_container, ImageEditorFragment.newInstance(getIntent().getData()));
|
||||
}
|
||||
|
||||
@Override
|
||||
/* @Override
|
||||
public void onBackPressed() {
|
||||
if (!imageEditorFragment.onBackPressed()) {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.thoughtcrime.securesms.scribbles;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Matrix;
|
||||
@@ -10,14 +11,17 @@ import android.graphics.PorterDuffXfermode;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.renderscript.Allocation;
|
||||
import android.renderscript.Element;
|
||||
import android.renderscript.RenderScript;
|
||||
import android.renderscript.ScriptIntrinsicBlur;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
import com.bumptech.glide.request.target.CustomTarget;
|
||||
@@ -114,13 +118,16 @@ final class UriGlideRenderer implements Renderer {
|
||||
|
||||
rendererContext.restore();
|
||||
|
||||
renderBlurOverlay(rendererContext);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
renderBlurOverlay(rendererContext);
|
||||
}
|
||||
} else if (rendererContext.isBlockingLoad()) {
|
||||
// If failed to load, we draw a black out, in case image was sticker positioned to cover private info.
|
||||
rendererContext.canvas.drawRect(Bounds.FULL_BOUNDS, paint);
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(17)
|
||||
private void renderBlurOverlay(RendererContext rendererContext) {
|
||||
boolean renderMask = false;
|
||||
|
||||
@@ -234,6 +241,7 @@ final class UriGlideRenderer implements Renderer {
|
||||
return matrix;
|
||||
}
|
||||
|
||||
@RequiresApi(17)
|
||||
private static @NonNull Bitmap blur(Bitmap bitmap, Context context) {
|
||||
Point previewSize = scaleKeepingAspectRatio(new Point(bitmap.getWidth(), bitmap.getHeight()), PREVIEW_DIMENSION_LIMIT);
|
||||
Point blurSize = scaleKeepingAspectRatio(new Point(previewSize.x / 2, previewSize.y / 2 ), MAX_BLUR_DIMENSION);
|
||||
|
||||
@@ -144,6 +144,31 @@ public class BitmapUtil {
|
||||
}
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
public static Bitmap createScaledBitmap(Bitmap bitmap, int maxWidth, int maxHeight) {
|
||||
if (bitmap.getWidth() <= maxWidth && bitmap.getHeight() <= maxHeight) {
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
if (maxWidth <= 0 || maxHeight <= 0) {
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
int newWidth = maxWidth;
|
||||
int newHeight = maxHeight;
|
||||
|
||||
float widthRatio = bitmap.getWidth() / (float) maxWidth;
|
||||
float heightRatio = bitmap.getHeight() / (float) maxHeight;
|
||||
|
||||
if (widthRatio > heightRatio) {
|
||||
newHeight = (int) (bitmap.getHeight() / widthRatio);
|
||||
} else {
|
||||
newWidth = (int) (bitmap.getWidth() / heightRatio);
|
||||
}
|
||||
|
||||
return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
|
||||
}
|
||||
|
||||
private static BitmapFactory.Options getImageDimensions(InputStream inputStream)
|
||||
throws BitmapDecodingException
|
||||
{
|
||||
@@ -503,4 +528,5 @@ public class BitmapUtil {
|
||||
drawable.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user