Here is a clear answer with code according to @XverhelstX. I created the code to get a clear sketch from the photo. Take the image from the source and convert it to input bitmap. Now call the method below and pass Bitmap to it.
public Bitmap Changetosketch(Bitmap bmp){ Bitmap Copy,Invert,Result; Copy =bmp; Copy = toGrayscale(Copy); Invert = createInvertedBitmap(Copy); Invert = Blur.blur(MainActivity.this, Invert); Result = ColorDodgeBlend(Invert, Copy); return Result; }
Here we got 3 methods GrayScale, Inverted and Color-DodgeBlend.
public static Bitmap toGrayscale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; } public static Bitmap createInvertedBitmap(Bitmap src) { ColorMatrix colorMatrix_Inverted = new ColorMatrix(new float[] { -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0}); ColorFilter ColorFilter_Sepia = new ColorMatrixColorFilter( colorMatrix_Inverted); Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColorFilter(ColorFilter_Sepia); canvas.drawBitmap(src, 0, 0, paint); return bitmap; } public Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) { Bitmap base = source.copy(Bitmap.Config.ARGB_8888, true); Bitmap blend = layer.copy(Bitmap.Config.ARGB_8888, false); IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight()); base.copyPixelsToBuffer(buffBase); buffBase.rewind(); IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight()); blend.copyPixelsToBuffer(buffBlend); buffBlend.rewind(); IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight()); buffOut.rewind(); while (buffOut.position() < buffOut.limit()) { int filterInt = buffBlend.get(); int srcInt = buffBase.get(); int redValueFilter = Color.red(filterInt); int greenValueFilter = Color.green(filterInt); int blueValueFilter = Color.blue(filterInt); int redValueSrc = Color.red(srcInt); int greenValueSrc = Color.green(srcInt); int blueValueSrc = Color.blue(srcInt); int redValueFinal = colordodge(redValueFilter, redValueSrc); int greenValueFinal = colordodge(greenValueFilter, greenValueSrc); int blueValueFinal = colordodge(blueValueFilter, blueValueSrc); int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal); buffOut.put(pixel); } buffOut.rewind(); base.copyPixelsFromBuffer(buffOut); blend.recycle(); return base; } private int colordodge(int in1, int in2) { float image = (float)in2; float mask = (float)in1; return ((int) ((image == 255) ? image:Math.min(255, (((long)mask << 8 ) / (255 - image))))); }
It should be noted that in my code I draw a bitmap using Renderscript.
Here is the Blur class.
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.support.v8.renderscript.Allocation; import android.support.v8.renderscript.Element; import android.support.v8.renderscript.RenderScript; import android.support.v8.renderscript.ScriptIntrinsicBlur; import android.view.View; public class Blur { private static final float BITMAP_SCALE = 0.4f; private static final float BLUR_RADIUS = 4.5f; public static Bitmap blur(View v) { return blur(v.getContext(), getScreenshot(v)); } public static Bitmap blur(Context ctx, Bitmap image) { Bitmap photo = image.copy(Bitmap.Config.ARGB_8888, true); try { final RenderScript rs = RenderScript.create( ctx ); final Allocation input = Allocation.createFromBitmap(rs, photo, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius( BLUR_RADIUS ); script.setInput( input ); script.forEach( output ); output.copyTo( photo ); }catch (Exception e){ e.printStackTrace(); } return photo; } private static Bitmap getScreenshot(View v) { Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.draw(c); return b; } }
After setting all this up, just pass the original bitmap to the first method from the onCreate ie method:
Done.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ResultBitmap = ChangetoSketch(InputBitmap); ImageView.setImageBitmap(ResultBitmap); } });
If this helps, please vote.