【问题标题】:Image become too pixalated after converting to bitmap - Xamarin.forms转换为位图后图像变得太像素化 - Xamarin.forms
【发布时间】:2020-10-02 10:18:44
【问题描述】:

我试图在我的 Xamarin.Forms Android 应用中查找相机拍摄的图像的旋转。目的是使图像始终纵向。它工作正常。旋转后图像转换为位图。

我面临的问题是在 Vivo 设备中:图像变得过于像素化。它的大小约为 7KB。我希望减小图像尺寸,但要保持质量。

我应该做些什么改变以保持尺寸更小而不损失质量。

旋转检查并转换为位图

Android.Graphics.Bitmap loadAndResizeBitmap(string filePath)
{
    
    Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options { InJustDecodeBounds = true };
    Android.Graphics.BitmapFactory.DecodeFile(filePath, options);
    
    int REQUIRED_SIZE = 100;
    int width_tmp = options.OutWidth, height_tmp = options.OutHeight;
    int scale = 4;
    while (true)
    {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
            break;
        
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }
    
    options.InSampleSize = scale;
    options.InJustDecodeBounds = false;
    Android.Graphics.Bitmap resizedBitmap = Android.Graphics.BitmapFactory.DecodeFile(filePath, options);
    
    ExifInterface exif = null;
    try
    {
        exif = new ExifInterface(filePath);
        string orientation = exif.GetAttribute(ExifInterface.TagOrientation);
    
        Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();

        switch (orientation)
        {
            case "1": // landscape
                break;
            case "3":
                matrix.PreRotate(180);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                matrix.Dispose();
                matrix = null;
                break;
            case "4":
                matrix.PreRotate(180);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                matrix.Dispose();
                matrix = null;
                break;
            case "5":
                matrix.PreRotate(90);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                matrix.Dispose();
                matrix = null;
                break;
            case "6": // portrait
                matrix.PreRotate(90);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                matrix.Dispose();
                matrix = null;
                break;
            case "7":
                matrix.PreRotate(-90);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                matrix.Dispose();
                matrix = null;
                break;
            case "8":
                matrix.PreRotate(-90);
                resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                matrix.Dispose();
                matrix = null;
                break;               
        }
    
        return resizedBitmap;
    
    }
}

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android


    【解决方案1】:

    对于options.InSampleSize,如果设置为 > 1 的值,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。所以你得到一个 7KB 大小的图像。 https://developer.android.com/reference/android/graphics/BitmapFactory.Options#inSampleSize

    您可以尝试使用matrix.postScale 以指定比例调整矩阵的大小。 https://developer.android.com/reference/android/graphics/Matrix#preScale(float,%20float)

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
    {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
    }
    

    【讨论】:

    • 参数newHeight和newWidth是什么?
    • 并且它还显示位图不包含 getWist() 的定义。相反,它显示 getScaledwidth()
    • newHeightnewWidth是你要生成的图片的高度和宽度。
    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多