如何在android中像素化位图

问题描述 投票:0回答:2

我一直在搜索,但没有找到有关如何在 android 中像素化位图的问题

示例

注意我的意思不是模糊

android pixel effects
2个回答
6
投票

您可以尝试将该图像的大小调整为较小的版本(如果出于某种原因需要将其大小与原始大小相同,则可以进行备份)

{
    Bitmap original = ...;
    //Calculate proportional size, or make the method accept just a factor of scale.
    Bitmap small = getResigetResizedBitmap(original, smallWidth, smallHeight);
    Bitmap pixelated = getResigetResizedBitmap(small, normalWidth, normalHeight);
   //Recycle small, recycle original if no longer needed.
}

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    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;
}

代码来自这里


0
投票

对图像进行像素化的最简单方法是使用“最近邻”算法缩小图像,然后使用相同的算法放大图像。 对图像进行过滤以求平均值需要花费更多时间,但实际上并不会提高结果质量,毕竟您确实希望图像失真。

© www.soinside.com 2019 - 2024. All rights reserved.