Tutorials


As I’m learning and working on Google Android, I’ll post my notes as tutorials for others to learn also.

[Android: The Basics]

1. Grab image from URL

2. Create a simple PhoneBook

3. Create a simple application grid

4. Create an Application Launcher (using a ListView)

5. Playing with Toast message notification

6. SQLite on Android – Part I

7. SQLite on Android – Part II

8. SQLite on Android – Part III (Final)

9. Load SQLite Database on Emulator

10. Create and use custom Content Provider

11. Insert image to database

12. Create & use emulated SD Card

13. A Quick Study on MP3 file structure

14. Parsing MP3 Basic Information

15. Query MP3 Information using Android MediaMetadataRetriever

16. Create and use Compound Control

17. Create an application auto receiving new message (SMS)

18. Bitmap and memory leak! – Part I

19. Bitmap and memory leak! – Part II

20. Bitmap and memory leak! – Part III

21. Auto-link for TextView

22. Using WebView as a Image Zoom View control

23. Controlling the Airplane Mode

24. Store and use files in Assets

25. New Cache for Better Memory Performance

26. Create a Service that does a schedule task

27. Guide to create, build and sign Android applications (using command)

28. Build ViewPagerIndicator with Ant

29. Create application with Swipe gesture to navigate page like Google Play

[ Android: Image Processing ]

1. Image Processing – Highlight Image on the fly

2. Image Processing – Invert Image on the fly

3. Image Processing – Grayscale Image on the fly

4. Image Processing – Gamma Correction on the fly

5. Image Processing – Filter Image Color

6. Image Processing – Photography Sepia-toning Effect

7. Image Processing – Decreasing Color Depth

8. Image Processing – Contrast Image on the fly

9. Image Processing – Rotate Image on the fly

10. Image Processing – Image Brightness

11. Image Processing – Convolution Matrix from Concepts to Implementation

12. Image Processing – Gaussian Blur effect

13. Image Processing – Sharpening Image

14. Image Processing – Mean Removal effect

15. Image Processing – Smooth effect

16. Image Processing – Emboss effect

17. Image Processing – Engraving effect

18. Image Processing – Boost up color intensity

19. Image Processing – Rounded Corner Image

20. Image Processing – Watermarking on the fly

21. Image Processing – Image Flipping / Mirroring

22. Image Processing – Pixel Color Replacement

23. Image Processing – Tint Color (Yet Another)

24. Image Processing – Flea / Noise Effect

25. Image Processing – Black Filter (Increasing the Darkness)

26. Image Processing – Snow Effect

27. Image Processing – Shading Filter

28. Image Processing – Saturation Filter

29. Image Processing – Hue Filter

30. Image Processing – Image Reflection Effect

31. Image Processing – Draw Text on a Curve

[ Series – Android XML Adventure]

1. What is the “Thing” called XML?

2. Parsing XML Data with SAX-Parser

3. Parsing XML Data with DOM

4. Parsing XML Data with XmlPullParser

5. Create & Write XML Data

6. Compare: XML Parsers

7. Parse XML using XPath

8. Parse HTML using HtmlCleaner

9. Parse HTML using JSoup

Hope you enjoy it.

Cheers,

Pete Houston

  1. pakistanaz
    July 30, 2023 at 4:21 pm

    Great Post. Thank for this information. katmoviehd Apk

  2. Sabuj
    December 5, 2020 at 10:21 pm

    Thanks Sir

  3. January 27, 2020 at 1:07 pm

    The post office is always out of tge way for me. Bleh! I need to check it too, ah!

  4. Muhammad
    July 28, 2018 at 3:08 pm

    i want to use warp camera effects but i am not able to do this. please any one help me.

  5. Saurav
    December 13, 2017 at 1:58 pm

    Can any one help me i want to crop face from the image but not in oval or rectangle .

  6. September 11, 2017 at 9:30 am

    Amazing! Іtѕ genuinely amazing post, Ι
    have got much cleaг idea on tһe topic ⲟf
    from this paragraph.

  7. Ahtisham
    June 24, 2017 at 5:28 pm

    good

  8. Dhaval
    February 27, 2017 at 1:22 am

    pls add tutorial of using frames with images

  9. Alex Brand
    December 28, 2016 at 6:32 pm

    Nice image processing tutorial highly recommend.

  10. December 15, 2016 at 12:35 pm

    Useful tutorials.. Is there any tutorial to draw same size of circles on imageview with x,y coordinates?

  11. October 7, 2016 at 2:26 am

    Big fan of yours….salute…nice tutorial

  12. Jay Thummar
    September 30, 2016 at 2:00 pm

    Give me some solution about HDR integration in to custom camera and in some device camera capture very blurry image so give me some solution about it

    • Shujaat Ali Bhatti
      October 30, 2018 at 3:44 am

      bro you need to apply manual Focus for this 🙂 its really late but hope so it will Gonna Help

      See This Method Its Working For Me ///
      public boolean startManualFocus(View view, MotionEvent motionEvent) {
      Log.d(TAG, “startManualFocus: called”);

      if (mManualFocusEngaged) {
      Log.d(TAG, “startManualFocus: Manual focus already engaged”);
      return true;
      }

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

      CameraManager manager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
      CameraCharacteristics characteristics = null;
      try {
      characteristics = manager.getCameraCharacteristics(mCameraId);

      final Rect sensorArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

      final int y = (int) ((motionEvent.getX() / (float) view.getWidth()) * (float) sensorArraySize.height());
      final int x = (int) ((motionEvent.getY() / (float) view.getHeight()) * (float) sensorArraySize.width());
      final int halfTouchWidth = 150;
      final int halfTouchHeight = 150;
      MeteringRectangle focusAreaTouch = new MeteringRectangle(
      Math.max(x – halfTouchWidth, 0),
      Math.max(y – halfTouchHeight, 0),
      halfTouchWidth * 2,
      halfTouchHeight * 2,
      MeteringRectangle.METERING_WEIGHT_MAX – 1);

      CameraCaptureSession.CaptureCallback captureCallbackHandler = new CameraCaptureSession.CaptureCallback() {
      @Override
      public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
      super.onCaptureCompleted(session, request, result);
      mManualFocusEngaged = false;

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      if (request.getTag() == “FOCUS_TAG”) {
      //the focus trigger is complete –
      //resume repeating (preview surface will get frames), clear AF trigger
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);

      // reset to get ready to capture a picture
      try {
      mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
      } catch (CameraAccessException e) {
      e.printStackTrace();
      }
      }
      }
      }

      @Override
      public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure failure) {
      super.onCaptureFailed(session, request, failure);
      Log.e(TAG, “startManualFocus: Manual AF failure: ” + failure);
      mManualFocusEngaged = false;
      }
      };

      if (mCaptureSession != null) {
      //first stop the existing repeating request
      mCaptureSession.stopRepeating();

      //cancel any existing AF trigger (repeated touches, etc.)
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
      mCaptureSession.capture(mPreviewRequestBuilder.build(), captureCallbackHandler, mBackgroundHandler);

      //Now add a new AF trigger with focus region
      if (isMeteringAreaAFSupported()) {
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusAreaTouch});
      }
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);

      mPreviewRequestBuilder.setTag(“FOCUS_TAG”); //we’ll capture this later for resuming the preview

      //then we ask for a single request (not repeating!)
      mCaptureSession.capture(mPreviewRequestBuilder.build(), captureCallbackHandler, mBackgroundHandler);
      mManualFocusEngaged = true;

      }

      } catch (CameraAccessException e) {
      e.printStackTrace();
      return true;
      }
      return true;
      }

      return false;

      }

      // Now Call This Method in onTouch like Below

      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {

      switch (motionEvent.getAction()) {

      case MotionEvent.ACTION_DOWN: {
      startX = motionEvent.getX();
      startY = motionEvent.getY();
      break;
      }

      case MotionEvent.ACTION_UP: {
      float endX = motionEvent.getX();
      float endY = motionEvent.getY();
      if (isAClick(startX, endX, startY, endY)) {
      if (view.getId() == R.id.texture && view.getId() != R.id.stillshot) {
      Log.d(TAG, “onTouch: MANUAL FOCUS.”);
      startManualFocus(view, motionEvent);
      }
      }
      break;
      }

      case MotionEvent.ACTION_MOVE: {

      break;
      }
      }

      if (mIsImageAvailable) {
      Log.d(TAG, “onTouch: sending touch event to DrawableImageView”);
      return mStillshotImageView.touchEvent(motionEvent);
      } else {
      Log.d(TAG, “onTouch: ZOOM.”);
      return mTextureView.onTouch(motionEvent);
      }
      // if (mIsImageAvailable && mIsDrawingEnabled){
      // return mStillshotImageView.touchEvent(motionEvent);
      // }
      // return true;
      }

  13. suresh
    August 31, 2016 at 7:25 pm
  14. vivek
    March 11, 2016 at 2:27 am

    Hi , Am new to android. Creating an app which require multiple images to be convert in a video or say slide show with effect and sound. Please us know how do that ? Thanks

  15. NIkhil
    January 12, 2016 at 1:35 pm

    Hi ,

    i am making a drawing app. I am able to implement drawing on a white canvas and save it in memory by making customView. Now i want to load the saved image into the canvas to redraw on that. I am setting the image but it is always showing white canvas. Please help.

  16. nisa
    November 6, 2015 at 12:52 am

    Hey can you give a tutorial on histogram equalization?

  17. October 13, 2015 at 4:53 pm

    Nice set of tutorials man….i like it …. 😛

  18. Nabeel
    August 12, 2015 at 4:36 pm

    Hi.
    I want to put an image over another image. Can you please help me out how can i do that

  19. sagar
    April 24, 2015 at 7:06 pm

    Hii Image Processing Tutorial is best for beginner.
    I am developing an app on Gesture detection (swipe down,swipe up, left,right). But i want use custom gesture, is that any help..

  20. mousa
    April 15, 2015 at 7:39 pm

    Very good tutorial
    Thanks Thanks Thanks

  21. Brownsoo
    December 22, 2014 at 4:25 pm

    Good~!!

  22. iqra
    December 11, 2014 at 10:35 pm

    Hi…can any one tell me the android implementation for color base image segmentation please???

  23. Venkata Tarun
    November 12, 2014 at 1:13 pm

    hiii… can you post sketch effect for a image.

  24. abid
    November 8, 2014 at 4:11 am

    can any body help me in fashion advisor project ?? statue wear dresses

  25. August 11, 2014 at 6:29 pm

    Hi

    Nice tutorial,
    Can help me out in implementing more effects like Tue, Aqua, Ventura, Laguna, Soft Focus, etc. and adding frame effect etc.

    Thanks in advance

  26. Yograj Shinde
    July 23, 2014 at 6:34 pm

    Awesome tutorials buddy … thx

  27. Yongsu Jeon
    July 21, 2014 at 4:31 pm

    i’m very much obliged to you

  28. Ankur Sharma
    July 16, 2014 at 1:17 pm

    hey it doesnt work with png extension . it works for jpg. can u please help me in this regard. thanks in advance

    • Ankur Sharma
      July 16, 2014 at 2:09 pm

      Also for kitkat and jellybean versions. it doesnt show any transformations after editing . please help

  29. May 28, 2014 at 7:29 pm

    Nice tutorial ….. 🙂

  30. September 2, 2013 at 7:08 pm

    Hi, I got I needed for the Image Detailing…
    Can you help me I want to apply the HDR Color Filter.

    • Jay Thummar
      September 30, 2016 at 1:55 pm

      did you get the solution ???

  31. August 30, 2013 at 6:09 pm

    Hi, I have learned so much thing from the Image Processing Tuts:
    and that was quite intensive. thanks man

    I am not able to find increase/decrease the Image details. and Also apply the temperature filter. plz help.

  32. August 16, 2013 at 7:46 am

    That’s a whole lot of tutorial you’ve compiled there. Will take some time to check them. Kudos!

  33. August 11, 2013 at 11:20 am

    Hi, I need to do image processing to remove a certain pattern from a bitmap. Could you please help me with this?

  34. Tharaka
    August 11, 2013 at 11:20 am

    Hi, I need to do image processing to remove a certain pattern from a bitmap. Could you please help me with this?

  35. June 15, 2013 at 9:51 pm

    There is something wrong with the code related to Posterize.. Can you please post the correct code

    • June 17, 2013 at 3:35 pm

      Hi,

      Please tell me how can I give posterization effect to the bitmap, using different shades of grey

  36. shweta
    May 31, 2013 at 6:23 pm

    its a best tutorial…………..

  37. May 31, 2013 at 4:13 pm

    I am a junior developer android apps. I have learned a lot from you.

  38. Narendra Prajapati
    December 29, 2012 at 6:34 pm

    hey i need tutorial for add text in image .

  39. Priyesh Bhargava
    December 24, 2012 at 7:57 pm

    Your link provide best stuff, can you provide me some help about adding different brush in android like ms paint.

  40. Paul
    November 13, 2012 at 9:55 pm

    Thanks for such a great series of articles!

  41. August 27, 2012 at 9:48 am

    Hello, do you have any method to deskew an image?

  42. April 3, 2012 at 9:58 am

    good tutorial 😀 thanks

  43. April 1, 2012 at 5:11 am

    Thanks for this comprehensive list of tutorials. I found very useful especially the Image Processing category.
    I also started collect all my tutorials on a single page: http://androidresearch.wordpress.com/android-tutorials/

  44. November 4, 2011 at 3:41 pm

    This tutorial was very helpfull for me,i want to know more effects for image like vinatage,old,polaroid filters. can any one please give some idea how to do. please help me.

  45. September 21, 2011 at 3:27 am

    Melih Gümüşçay :

    Smiley in the code is 2 character : “8)”
    Smiley in the code is 8

  46. September 21, 2011 at 3:25 am

    And this code is converted version of convolution:

    public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix) {

    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[SIZE][SIZE];

    int[] pix = new int[width * height];
    src.getPixels(pix, 0, width, 0, 0, width, height);

    int index;
    for (int y = 0; y < height-2; y++)
    for (int x = 0; x < width-2; x++)
    {
    // get pixel matrix
    for(int i = 0; i < SIZE; ++i) {
    for(int j = 0; j < SIZE; ++j) {
    index = (y+j) * width + (x+i);
    pixels[i][j] = pix[index];
    }
    }

    // get alpha of center pixel
    A = Color.alpha(pixels[1][1]);

    // init color sum
    sumR = sumG = sumB = 0;

    // get sum of RGB on matrix
    for(int i = 0; i < SIZE; ++i) {
    for(int j = 0; j < SIZE; ++j) {
    sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
    sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
    sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
    }
    }

    // get final Red
    R = (int)(sumR / matrix.Factor + matrix.Offset);
    if(R 255) { R = 255; }

    // get final Green
    G = (int)(sumG / matrix.Factor + matrix.Offset);
    if(G 255) { G = 255; }

    // get final Blue
    B = (int)(sumB / matrix.Factor + matrix.Offset);
    if(B 255) { B = 255; }

    // apply new pixel
    pix[y * width + x] = 0xff000000 | (R << 16) | (G << 8) | B;
    //result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
    }
    result.setPixels(pix, 0, width, 0, 0, width, height);
    pix = null;
    // final image
    return result;
    }

  47. September 16, 2011 at 9:53 pm

    Smiley in the code is 8

  48. September 16, 2011 at 9:50 pm

    Hi,
    As i commented before i made an improvement using array instead of getpixel function its performance change drastically. Let me share it with you:

    package com.montagraph;

    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.Bitmap.Config;
    import android.graphics.PorterDuff.Mode;

    //thanks to https://xjaphx.wordpress.com
    public class PhotoEffects {
    Bitmap mBitmap;
    Bitmap mOriginalBitmap;
    Bitmap.Config mBitmapConfig;
    int mWidth;
    int mHeight;

    public PhotoEffects(Bitmap src)
    {
    mOriginalBitmap = mBitmap = src;
    mBitmapConfig = src.getConfig();
    mWidth = src.getWidth();
    mHeight = src.getHeight();
    }
    public Bitmap rotate(float degree) {
    // create new matrix
    Matrix matrix = new Matrix();
    // setup rotation degree
    matrix.postRotate(degree);

    // return new bitmap rotated using matrix
    return Bitmap.createBitmap(mBitmap, 0, 0, mWidth, mHeight, matrix, true);
    }
    public Bitmap mirror() {//flipHorizontal
    // create new matrix for transformation
    Matrix matrix = new Matrix();
    // x = x * -1
    matrix.preScale(-1.0f, 1.0f);
    // unknown type
    // return transformed image
    return Bitmap.createBitmap(mBitmap, 0, 0, mWidth, mHeight, matrix, true);
    }
    public Bitmap addWatermark(String watermark, Point location, int color, int alpha, int size, boolean underline) {
    Bitmap bmOut = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);

    Canvas canvas = new Canvas(bmOut);
    canvas.drawBitmap(mBitmap, 0, 0, null);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(color);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawText(watermark, location.x, location.y, paint);

    mBitmap = bmOut;
    return bmOut;
    }
    public Bitmap roundCorner(float round) {
    // create bitmap output
    Bitmap bmOut = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
    // set canvas for painting
    Canvas canvas = new Canvas(bmOut);
    canvas.drawARGB(0, 0, 0, 0);

    // config paint
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);

    // config rectangle for embedding
    final Rect rect = new Rect(0, 0, mWidth, mHeight);
    final RectF rectF = new RectF(rect);

    // draw rect to canvas
    canvas.drawRoundRect(rectF, round, round, paint);

    // create Xfer mode
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    // draw source image to canvas
    canvas.drawBitmap(mBitmap, rect, rect, paint);

    // return final image
    mBitmap = bmOut;
    return bmOut;
    }
    public Bitmap blur() {
    double[][] GaussianBlurConfig = new double[][] {
    { -1, 0, -1 },
    { 0, 4, 0 },
    { -1, 0, -1 }
    };
    ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
    convMatrix.applyConfig(GaussianBlurConfig);
    convMatrix.Factor = 16;
    convMatrix.Offset = 0;
    return ConvolutionMatrix.computeConvolution3x3(mBitmap, convMatrix);
    }
    public Bitmap hue(double hue) {
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    float[] hsv = new float[3];

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x < mWidth; x++)
    {
    int index = y * mWidth + x;
    Color.colorToHSV( pix[index], hsv );
    hsv[0] = (float)((hsv[0] + 360 * hue) % 360);
    pix[index] = (Color.HSVToColor( hsv ) & 0x00ffffff) | (pix[index] & 0xff000000);
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    /*
    // create bitmap output
    Bitmap bmOut = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);

    float[] hsv = new float[3];
    for( int x = 0; x < mWidth; x++ ) {
    for( int y = 0; y < mHeight; y++ ) {
    int c = source.getPixel( x, y );
    Color.colorToHSV( c, hsv );
    hsv[0] = (float) ((hsv[0] + 360 * hue) % 360);
    c = (Color.HSVToColor( hsv ) & 0x00ffffff) | (c & 0xff000000);
    bmOut.setPixel( x, y, c );
    }
    }

    mBitmap = bmOut;
    return bmOut;
    */
    }
    public Bitmap brightness(int value)
    {
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R = r + value;
    R = (R 255) ? 255 : R);
    G = g + value;
    G = (G 255) ? 255 : G);
    B = b + value;
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap sharpen(double weight) {
    double[][] SharpConfig = new double[][] {
    { 0 , -2 , 0 },
    { -2, weight, -2 },
    { 0 , -2 , 0 }
    };
    ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
    convMatrix.applyConfig(SharpConfig);
    convMatrix.Factor = weight – 8;
    return ConvolutionMatrix.computeConvolution3x3(mBitmap, convMatrix);
    }
    public Bitmap invert()
    {
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R = 255 – r;
    R = (R 255) ? 255 : R);
    G = 255 – g;
    G = (G 255) ? 255 : G);
    B = 255 – b;
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap greyscale() {
    // constant factors
    final double GS_RED = 0.299;
    final double GS_GREEN = 0.587;
    final double GS_BLUE = 0.114;

    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R = G = B = (int)(GS_RED * r + GS_GREEN * g + GS_BLUE * b);
    R = (R 255) ? 255 : R);
    G = (G 255) ? 255 : G);
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap sepia(int depth, double red, double green, double blue) {
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R = G = B = (int)(GS_RED * r + GS_GREEN * g + GS_BLUE * b);
    R = (R 255) ? 255 : R);
    G = (G 255) ? 255 : G);
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap contrast(double value) {
    // get contrast value
    double contrast = Math.pow((100 + value) / 100, 2);
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R = (int)(((((r / 255.0) – 0.5) * contrast) + 0.5) * 255.0);
    R = (R 255) ? 255 : R);
    G = (int)(((((g / 255.0) – 0.5) * contrast) + 0.5) * 255.0);
    G = (G 255) ? 255 : G);
    B = (int)(((((b / 255.0) – 0.5) * contrast) + 0.5) * 255.0);
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap posterize(int bitOffset) {//decreaseColorDepth
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R = ((r + (bitOffset / 2)) – ((r + (bitOffset / 2)) % bitOffset) – 1);
    R = (R 255) ? 255 : R);
    G = ((g + (bitOffset / 2)) – ((g + (bitOffset / 2)) % bitOffset) – 1);
    G = (G 255) ? 255 : G);
    B = ((b + (bitOffset / 2)) – ((b + (bitOffset / 2)) % bitOffset) – 1);
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap engrave() {
    ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
    convMatrix.setAll(0);
    convMatrix.Matrix[0][0] = -2;
    convMatrix.Matrix[1][1] = 2;
    convMatrix.Factor = 1;
    convMatrix.Offset = 95;
    return ConvolutionMatrix.computeConvolution3x3(mBitmap, convMatrix);
    }
    public Bitmap colorize(int type, float percent) {
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    int R, G, B;

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    R=r;
    G=g;
    B=b;
    if(type == 1) {
    R = (int)(r * (1 + percent));
    }
    else if(type == 2) {
    G = (int)(g * (1 + percent));
    }
    else if(type == 3) {
    B = (int)(b * (1 + percent));
    }
    R = (R 255) ? 255 : R);
    G = (G 255) ? 255 : G);
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    public Bitmap tint(int deg) {
    int[] pix = new int[mWidth * mHeight];
    mBitmap.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
    double angle = (3.14159d * (double)deg) / 180.0d;
    int S = (int)(256.0d * Math.sin(angle));
    int C = (int)(256.0d * Math.cos(angle));

    for (int y = 0; y < mHeight; y++)
    for (int x = 0; x > 16) & 0xff;
    int g = (pix[index] >> 8) & 0xff;
    int b = pix[index] & 0xff;
    RY = ( 70 * r – 59 * g – 11 * b) / 100;
    GY = (-30 * r + 41 * g – 11 * b) / 100;
    BY = (-30 * r – 59 * g + 89 * b) / 100;
    Y = ( 30 * r + 59 * g + 11 * b) / 100;
    RYY = (S * BY + C * RY) / 256;
    BYY = (C * BY – S * RY) / 256;
    GYY = (-51 * RYY – 19 * BYY) / 100;
    R = Y + RYY;
    R = (R 255) ? 255 : R);
    G = Y + GYY;
    G = (G 255) ? 255 : G);
    B = Y + BYY;
    B = (B 255) ? 255 : B);
    pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
    }

    Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, mBitmapConfig);
    bm.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    mBitmap = bm;
    pix = null;
    return mBitmap;
    }
    }

    • Aman Ullah Usmani
      June 16, 2012 at 9:25 pm

      Hi,
      I am not able to get few line of code. Can you please guide me their exact usage and syntax?

      for (int x = 0; x > 16) // & 0xff; //
      int g = (pix[index] >> 8) // & 0xff; //
      int b = pix[index] & 0xff;
      R = ((r + (bitOffset / 2)) – ((r + (bitOffset / 2)) % bitOffset) – 1);
      R = // (R 255) ? 255 : R); //
      G = ((g + (bitOffset / 2)) – ((g + (bitOffset / 2)) % bitOffset) – 1);
      G = (G 255) ? 255 : G); //
      B = ((b + (bitOffset / 2)) – ((b + (bitOffset / 2)) % bitOffset) – 1);
      B = (B 255) ? 255 : B); //
      pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;

      this is what i copped from above and those with // i am not able to understand. kindly help me out as getpixel and set by loop is giving me worst time … Thanks

    • June 17, 2013 at 3:54 pm

      Can you please tell me how can I posterize the bitmap.. I am trying to follow your code here but it is not compiling.. seems like some of the symbols are missing

  49. September 3, 2011 at 10:01 pm

    Hi as a suggestion instead of using getPixel function in a loop you can use getPixels to capture all pixels into an array then process them it would improve the performance

    • September 4, 2011 at 12:16 pm

      That’s true! It’s supposed to be that way 🙂

  50. July 12, 2011 at 5:11 pm

    Nice Image Processing tutorial ^^

  1. No trackbacks yet.

Leave a comment