I have a class that uses a color matrix to add image effects on an ImageView. I have several color matrices for contrast,exposure,temperature and saturation. Ex:
public static Bitmap changeContrast(Bitmap bmp, float contrast)
{
ColorMatrix cm = new ColorMatrix(new float[]
{
contrast, 0, 0, 0, 0,
0, contrast, 0, 0, 0,
0, 0, contrast, 0, 0,
0, 0, 0, 1, 0
});
return getBitmapFromColorMatrix(cm, bmp);
}
Now my problem is, the sharpening of the image. There seems to be no color matrix for these. Please help.
I tried sharpening the image using the code from here but it takes too long to process and I do not know what valid values should be passed to the weight parameter of the sharpenImage(Bitmap src, double weight) method.
It may be too late but i'd just wanted to share a fast way to sharpen an image.
public static Bitmap doSharpen(Bitmap original, float[] radius) {
Bitmap bitmap = Bitmap.createBitmap(
original.getWidth(), original.getHeight(),
Bitmap.Config.ARGB_8888);
RenderScript rs = RenderScript.create(yourContext);
Allocation allocIn = Allocation.createFromBitmap(rs, original);
Allocation allocOut = Allocation.createFromBitmap(rs, bitmap);
ScriptIntrinsicConvolve3x3 convolution
= ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
convolution.setInput(allocIn);
convolution.setCoefficients(radius);
convolution.forEach(allocOut);
allocOut.copyTo(bitmap);
rs.destroy();
return bitmap;
}
And here are 3 different sharpen types that i created:
// low
private static void loadBitmapSharp() {
float[] sharp = { -0.60f, -0.60f, -0.60f, -0.60f, 5.81f, -0.60f,
-0.60f, -0.60f, -0.60f };
//you call the method above and just paste the bitmap you want to apply it and the float of above
yourbitmap = doSharpen(getbitmap, sharp));
}
// medium
private static void loadBitmapSharp1() {
float[] sharp = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f, -1.0f,
0.0f
};
//you call the method above and just paste the bitmap you want to apply it and the float of above
yourbitmap = doSharpen(getbitmap, sharp));
}
// high
private static void loadBitmapSharp2() {
float[] sharp = { -0.15f, -0.15f, -0.15f, -0.15f, 2.2f, -0.15f, -0.15f,
-0.15f, -0.15f
};
//you call the method above and just paste the bitmap you want to apply it and the float of above
yourbitmap = doSharpen(getbitmap, sharp));
}
You can also apply them direct to a bitmap without a void, its fast simple and there are good results!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With