converting image into pencil sketch effect in android - android

how to convert photo's and images (bitmaps) to pencil sketch-like pictures(As shown below)? I can't find any good sources on how to do it.
I want to make an android app that can programmatically "convert" JPEG photo's to pencil sketch images.
Can you please help me in this.

First read the image
g = Convert-To-Gray-Scale(s)
i = Invert-Colors(g)
b = Apply-Gaussian-Blur(i)
result = Color-Dodge-Blend-Merge(b,g)
https://github.com/finebyte/android-jhlabs/issues/2
Also see https://github.com/geraintluff/canvas-sketch

`public static Bitmap toGrayscale(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixel;
int width = src.getWidth();
int height = src.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get one pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
public static Bitmap createInvertedBitmap(Bitmap src) {
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixelColor;
int height = src.getHeight();
int width = src.getWidth();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelColor = src.getPixel(x, y);
A = Color.alpha(pixelColor);
R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
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)))));
}`

Related

Image flip and image filter android

I am trying to image flip and image filter But I have a problem with that When I put a filter on the image then when I flip the image the filter is removed
public void Flipimg(View v1){
img1.setImageBitmap(fliph(scaledBitmap));
}
public Bitmap fliph(Bitmap bms){
Matrix mat = new Matrix();
mat.setScale(-1, 1);
Bitmap bit1=Bitmap.createBitmap( bms, 0, 0, bms.getWidth(), bms.getHeight(), mat, true);
return bit;
public void Filterimg(View v2){
img1.setImageBitmap(applyGreyscaleEffect(scaledBitmap));
}
public Bitmap applyGreyscaleEffect(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixel;
int width = src.getWidth();
int height = src.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));}
}
return bmOut;
looking images THIS image flip
THIS image apply filter
If you'd like to apply effects sequentially, get current bitmap on the ImageView via BitmapDrawable.
public void Flipimg(View v1)
{
//img1.setImageBitmap(fliph(scaledBitmap));
Bitmap currentBitmap = ((BitmapDrawable) img1.getDrawable()).getBitmap();
img1.setImageBitmap(fliph(currentBitmap));
}
public void Filterimg(View v2)
{
//img1.setImageBitmap(applyGreyscaleEffect(scaledBitmap));
Bitmap currentBitmap = ((BitmapDrawable) img1.getDrawable()).getBitmap();
img1.setImageBitmap(applyGreyscaleEffect(currentBitmap));
}

How to increase brightness of the button?

Hi I have one application in that when am try to touch any button that one should be more brighter compared to remaining all the button.
I have tried one code it is working fine .but,it is taking too much for displaying.
Can any one tell me any other way I can do it?
if (themeClick.equals("6")) {/*cool*/
mTick1.setVisibility(View.VISIBLE);
mTick2.setVisibility(View.VISIBLE);
mTick3.setVisibility(View.VISIBLE);
mTick4.setVisibility(View.VISIBLE);
mTick5.setVisibility(View.VISIBLE);
mTick6.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.cool),-80));
mTick1.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.classic),-0));
mTick2.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.casual1),-0));
mTick3.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.natural),-0));
mTick4.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.smart),-0));
mTick5.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.meeting),-0));
mTick6.setVisibility(View.VISIBLE);
}
public Bitmap SetBrightness(Bitmap src, int value) {
// original image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// increase/decrease each channel
R += value;
if(R > 255) { R = 255; }
else if(R < 0) { R = 0; }
G += value;
if(G > 255) { G = 255; }
else if(G < 0) { G = 0; }
B += value;
if(B > 255) { B = 255; }
else if(B < 0) { B = 0; }
// apply new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}

Modifying bitmap is very slow...Is there any other way for this?

I am here trying to convert my current bitmap in black and white bitmap by writing following method.But it takes very long time to convert because of its for loop of every pixels.Am i wrong here any where? or is there any other way to do this?..Help will be appreciated.Thank you.
My Code:
public Bitmap convert(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
int A, R, G, B;
final int width = src.getWidth();
final int height = src.getHeight();
int pixels[] = new int[width * height];
int k = 0;
Bitmap bitmap = Bitmap.createBitmap(width, height, src.getConfig());
src.getPixels(pixels, 0, width, 0, 0, width, height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = pixels[x + y * width];
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
pixels[x + y * width] = Color.argb(
A, R, G, B);
}
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
You can easily convert rgb image to grayscale using this method without looping through all pixels
public Bitmap toGrayscale(Bitmap bmp)
{
int originalwidth, originalheight;
originalheight = bmp.getHeight();
originalwidth = bmp.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(originalwidth, originalheight, Bitmap.Config.ARGB_8888);
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(bmp, 0, 0, paint);
return bmpGrayscale;
}

How to programatically apply effects on imageview image in android?

i want to apply various effects on image view image.this image is already sent from main activity.i have the code for effects but don't know the method how to apply it on button click.kindly guide me.
public class PhotoEffect extends Activity implements OnClickListener {
ImageView camphoto;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photoeffect);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bundle extras=getIntent().getExtras();
//b=(Bitmap)extras.get("img");
byte[] byteArray=extras.getByteArray("img");
bmp=BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
camphoto=(ImageView)findViewById(R.id.camimage);
camphoto.setImageBitmap(bmp);
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
#Override
public void onClick(View v)
{
Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
switch (v.getId())
{
case R.id.btnsepia:
//Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
camphoto.setImageBitmap(createSepiaToningEffect(bitmap, 2, .5, .6, .59));
break;
case R.id.btnhighlight:
camphoto.setImageBitmap(doHighlightImage(bitmap));
break;
}
}
You must define the createSepiaToningEffect function like this returning a Bitmap after processing.
public static Bitmap createSepiaToningEffect(Bitmap src, int depth, double red, double green, double blue) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
final double GS_RED = 0.3;
final double GS_GREEN = 0.59;
final double GS_BLUE = 0.11;
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
// apply intensity level for sepid-toning on each channel
R += (depth * red);
if(R > 255) { R = 255; }
G += (depth * green);
if(G > 255) { G = 255; }
B += (depth * blue);
if(B > 255) { B = 255; }
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}

How adjust brightness of bitmap in gradient format in android?

I have seen so many links and posts of to adjust birightness of bitmap in android, but i want to adjust brightness with gradient, like adjust brightness of only center of the bitmap. Anybody have any idea how to do this ?
You can generate a bitmap and draw a gradient:
How to draw a smooth/dithered gradient on a canvas in Android
Then use each pixel from gradient bitmap and get the brightness and pass it to the function that adjust brightness.
look here Formula to determine brightness of RGB color
public Bitmap SetBrightness(Bitmap src, Bitmap gradient) {
// original image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
int gradientPixel = gradient.getPixel(x, y);
int value = getLuminance(gradientPixel);
// increase/decrease each channel
R += value;
if (R > 255) {
R = 255;
}
else if (R < 0) {
R = 0;
}
G += value;
if (G > 255) {
G = 255;
}
else if (G < 0) {
G = 0;
}
B += value;
if (B > 255) {
B = 255;
}
else if (B < 0) {
B = 0;
}
// apply new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
public int getLuminance(int pixel) {
int A, R, G, B;
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
return (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
}

Categories

Resources