2018-03-28 07:35:49 +02:00
|
|
|
package net.nullsum.audinaut.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2019-11-28 01:36:57 +01:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.ColorMatrix;
|
|
|
|
import android.graphics.ColorMatrixColorFilter;
|
|
|
|
import android.graphics.Paint;
|
2018-03-28 07:35:49 +02:00
|
|
|
import android.renderscript.Allocation;
|
|
|
|
import android.renderscript.Element;
|
|
|
|
import android.renderscript.RenderScript;
|
|
|
|
import android.renderscript.ScriptIntrinsicBlur;
|
|
|
|
|
|
|
|
class BlurBuilder {
|
|
|
|
private static final float BITMAP_SCALE = 0.4f;
|
|
|
|
private static final float BLUR_RADIUS = 25f;
|
|
|
|
|
|
|
|
public static Bitmap blur(Context context, Bitmap image) {
|
2019-03-19 02:17:48 +01:00
|
|
|
Bitmap newImage = image;
|
|
|
|
for(int i = 0; i<3; i++) {
|
|
|
|
newImage = blur_real(context, newImage);
|
|
|
|
}
|
2019-11-28 01:36:57 +01:00
|
|
|
newImage = changeBitmapContrastBrightness(newImage, 0.5f, 48);
|
2019-03-19 02:17:48 +01:00
|
|
|
return newImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap blur_real(Context context, Bitmap image) {
|
2018-03-28 07:35:49 +02:00
|
|
|
int width = Math.round(image.getWidth() * BITMAP_SCALE);
|
|
|
|
int height = Math.round(image.getHeight() * BITMAP_SCALE);
|
|
|
|
|
|
|
|
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
|
|
|
|
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
|
|
|
|
|
|
|
|
RenderScript rs = RenderScript.create(context);
|
|
|
|
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
|
|
|
|
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
|
|
|
|
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
|
|
|
|
theIntrinsic.setRadius(BLUR_RADIUS);
|
|
|
|
theIntrinsic.setInput(tmpIn);
|
|
|
|
theIntrinsic.forEach(tmpOut);
|
|
|
|
tmpOut.copyTo(outputBitmap);
|
|
|
|
|
|
|
|
return outputBitmap;
|
|
|
|
}
|
2019-11-28 01:36:57 +01:00
|
|
|
|
|
|
|
public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness)
|
|
|
|
{
|
|
|
|
ColorMatrix cm = new ColorMatrix(new float[]
|
|
|
|
{
|
|
|
|
contrast, 0, 0, 0, brightness,
|
|
|
|
0, contrast, 0, 0, brightness,
|
|
|
|
0, 0, contrast, 0, brightness,
|
|
|
|
0, 0, 0, 1, 0
|
|
|
|
});
|
|
|
|
|
|
|
|
Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
|
|
|
|
|
|
|
|
Canvas canvas = new Canvas(ret);
|
|
|
|
|
|
|
|
Paint paint = new Paint();
|
|
|
|
paint.setColorFilter(new ColorMatrixColorFilter(cm));
|
|
|
|
canvas.drawBitmap(bmp, 0, 0, paint);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-28 07:35:49 +02:00
|
|
|
}
|