Home > Tutorials > Image Processing – Image Flipping / Mirroring

Image Processing – Image Flipping / Mirroring


This technique is basically same as rotation, however, it transforms rotation with a reverse direction.

– For vertical flipping: [ x = x, y = y * -1 ]

– For horizontal flipping: [ x = x * -1, y = y ]

By using matrix transformation, we’ll get nice results:

Original Image

Original Image

Vertical Flipping

Vertical Flipping

Horizontal Flipping

Horizontal Flipping

Here the implementation:

	// type definition
	public static final int FLIP_VERTICAL = 1;
	public static final int FLIP_HORIZONTAL = 2;

	public static Bitmap flip(Bitmap src, int type) {
		// create new matrix for transformation
		Matrix matrix = new Matrix();
		// if vertical
		if(type == FLIP_VERTICAL) {
			// y = y * -1
			matrix.preScale(1.0f, -1.0f);
		}
		// if horizonal
		else if(type == FLIP_HORIZONTAL) {
			// x = x * -1
			matrix.preScale(-1.0f, 1.0f);
		// unknown type
		} else {
			return null;
		}

		// return transformed image
		return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
	}

Hope you like it!

Cheers,
Pete Houston

  1. seema
    May 18, 2016 at 6:56 pm

    Please tell me if the same can be done for videos. If so kindly help me

  2. Gogomx3
    December 28, 2011 at 9:28 am

    Thanks

  3. Junior
    June 28, 2011 at 3:32 am

    hey, what is your email? I want to send you my app so you can see how it looks like after your tutorials! Thanks

    • Umair Zahid
      August 31, 2012 at 7:50 pm

      Hay Junior. Can you share you app with me? I am really interested to see how these code snippets actually work.

  1. No trackbacks yet.

Leave a comment