镜像捕获的照片

时间:2019-01-29 05:44:59

标签: image xamarin xamarin.android

我单击图片并将其传递给意图。但是我在imageview上显示的图像是所捕获图像的镜像。我尝试实现rotateflip方法,但出现错误,指出位图不包含rotateflip的定义。

varmetrics = 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;
    }

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用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);
    }

bInput是您要翻转的输入位图。