Sunday 21 April 2013

Photo Sketch Effect for android




Please use below class and pass the bitmap to this function and get return bitmap with Sketch effect.You can also customize the value and get the customization of sketch effect.


public class Sketch {
      //  type = 3(Negative), 4(Pencil Sketch), 5(Color Pencil Sketch)
      public static final Bitmap changeToSketch(Bitmap bitmap) {
         return changeToSketch(bitmap, 5,110);
      }    
     
      public static final Bitmap changeToSketch(Bitmap src,int type,int threshold) {
          int width = src.getWidth();
        int height = src.getHeight();
        Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

        int A, R, G, B;
        int sumR, sumG, sumB;
        int[][] pixels = new int[3][3];
        for(int y = 0; y < height - 2; ++y) {
            for(int x = 0; x < width - 2; ++x) {
           //      get pixel matrix
                      for(int i = 0; i < 3; ++i) {
                    for(int j = 0; j < 3; ++j) {
                        pixels[i][j] = src.getPixel(x + i, y + j);
                    }
                }
            // get alpha of center pixel
                A = Color.alpha(pixels[1][1]);
                // init color sum
                sumR = sumG = sumB = 0;
                sumR = (type*Color.red(pixels[1][1])) - Color.red(pixels[0][0]) - Color.red(pixels[0][2]) - Color.red(pixels[2][0]) - Color.red(pixels[2][2]);
                sumG = (type*Color.green(pixels[1][1])) - Color.green(pixels[0][0]) - Color.green(pixels[0][2]) - Color.green(pixels[2][0]) - Color.green(pixels[2][2]);
                sumB = (type*Color.blue(pixels[1][1])) - Color.blue(pixels[0][0]) - Color.blue(pixels[0][2]) - Color.blue(pixels[2][0]) - Color.blue(pixels[2][2]);
                // get final Red
                R = (int)(sumR  + threshold);
                if(R < 0) { R = 0; }
                else if(R > 255) { R = 255; }
                // get final Green
                G = (int)(sumG  + threshold);
                if(G < 0) { G = 0; }
                else if(G > 255) { G = 255; }
                // get final Blue
                B = (int)(sumB  + threshold);
                if(B < 0) { B = 0; }
                else if(B > 255) { B = 255; }

                result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
            }
        }
        return result;
      }
             

}

By: NovaRadix Technology.





3 comments:

  1. You can view all effect on this photo effect application.

    https://play.google.com/store/apps/details?id=com.nova.photo.college.effect

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Call web services easily

get source code from here, Download