Search code examples
imagexamarinxamarin.android

mirror image the captured photo


I am clicking the picture and the passing it through intent. But the image that I am displaying on the ImageView is the mirror image of the image captured. I have tried to implement RotateFlip method but the error appears saying Bitmap doesn't contain definition of RotateFlip.

{
    var metrics = Resources.DisplayMetrics;

    int windowWidth = metrics.WidthPixels;
    int windowHeight = metrics.HeightPixels;
    int reqWidth = (int)Math.Round(windowWidth*1.0f);
    int reqHeight = (int)Math.Round(windowHeight*1.0f);
    Bitmap photo = loadBitmapLowResolution(Image, reqWidth, reqHeight);

    ExifInterface exif = new ExifInterface(Image);
    int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 90);
    var rotation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);

    orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Rotate90);
    // Rotate according to the orientation
    Matrix matrix = new Matrix();
    switch (orientation)
    {
        case (int)Orientation.Rotate90: matrix.PostRotate(90); break;
        case (int)Orientation.Rotate180: matrix.PostRotate(180); break;

        case (int)Orientation.Rotate270: matrix.PostRotate(270); break;

    }

    Bitmap photoRotated;
    if (orientation != (int)Orientation.Normal)
    {
        photoRotated = Bitmap.CreateBitmap(photo, 0, 0, photo.Width, photo.Height, matrix, true);
    }
    else
    {
        photoRotated = photo;
    }

    // Apply center crop
    int cropW = (int)(0.5 * (photoRotated.Width - photoRotated.Width/1.0f ) - 2);
    int cropH = (int)(0.5 * (photoRotated.Height - photoRotated.Height/1.0f ) - 2);
    cropW = cropW < 0 ? 0 : cropW;
    cropH = cropH < 0 ? 0 : cropH;
    //Log.Debug(TAG, "Creating a bitmap with size: " + (photoRotated.Width - 2 * cropW) + "x" + (photoRotated.Height - 2 * cropH) + " from a bitmap with size: " + photoRotated.Width + "x" + photoRotated.Height);
    Bitmap photoCrop = Bitmap.CreateBitmap(photoRotated, cropW, cropH, photoRotated.Width - 2 * cropW, photoRotated.Height- 2 * cropH);



    imageView.SetImageBitmap(photoCrop);

    // Create your application here, 
}


public static Bitmap loadBitmapLowResolution(string filePath, int reqWidth, int reqHeight)
{
    BitmapFactory.Options options = new BitmapFactory.Options();

    // First decode with inJustDecodeBounds=true to check dimensions
    options.InJustDecodeBounds = true;
    BitmapFactory.DecodeFile(filePath, options);

    // Calculate inSampleSize
    options.InSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.InJustDecodeBounds = false;

    return BitmapFactory.DecodeFile(filePath, options);
}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{

    int height = options.OutHeight;
    int width = options.OutWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth)
    {
        // Calculate ratios of height and width to requested height and width
        int heightRatio = (int)Math.Round((float)height / (float)reqHeight);
        int widthRatio = (int)Math.Round((float)width / (float)reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

Solution

  • The easiest way is to do it is using a Matrix;

    public Bitmap HorizontalFlip(Bitmap bInput)
    {
        if (bInput == null) return null;
        Matrix matrix = new Matrix();
        matrix.PreScale(-1.0f, 1.0f);
        return Bitmap.CreateBitmap(bInput, 0, 0, bInput.Width, bInput.Height, matrix, true);
    }
    

    Where bInput is the input bitmap that you wanna flip.