【问题标题】:How to increase pixels size of a Bitmap如何增加位图的像素大小
【发布时间】:2020-01-01 08:12:54
【问题描述】:

我想知道是否可以逐个像素地修改位图像素。我不是要求更改位图大小/比例。我正在寻找的是我想增加位图的所有或某些特定像素的大小。

我试过了

Thread(Runnable {
         val newBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width * 4,
                 bitmap.height * 4, true);

         for (x in 0 until bitmap.getWidth()) {
             for (y in 0 until bitmap.getHeight()) {

                 val pixel = bitmap.getPixel(x, y);
                 val redValue = Color.red(pixel)
                 val blueValue = Color.blue(pixel)
                 val greenValue = Color.green(pixel)

                 newBitmap.setPixel(x * 4, y * 4, Color.rgb(redValue, greenValue, blueValue))

             }
         }

         runOnUiThread({ imageView.setImageBitmap(newBitmap) })

     }).start()

但它对位图没有影响。任何形式的帮助都将受到高度赞赏。

【问题讨论】:

  • increase size of all or some specific pixels 是什么意思?像素大小始终为 1 像素,不可修改。可能您想用与您选择的像素相同的颜色为周围的像素着色?
  • 我想让像素看起来更大,使其具有像素化/马赛克效果。
  • 可能您想用与您选择的像素相同的颜色为周围的像素着色。这是一个很好的建议。有什么想法吗?
  • 为周围的所有像素着色,为 x+1 y+1、x+1 y、x y+1 等坐标执行 setPixel

标签: android image-processing bitmap


【解决方案1】:

如果您只想对位图的一部分进行像素化,那么您可以为要编辑的部分创建一个Bitmap,对新位图执行更改,然后将更改后的位图覆盖在原始位图之上.

因此,例如,您可以通过执行以下操作来像素化 Bitmap 的一部分,

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    Bitmap originalBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.drawabledroid)).getBitmap();
    // Pixelate just the top half
    int x = 0, y = 0, width = originalBitmap.getWidth(), height = originalBitmap.getHeight()/2;
    //Or pixelate a rectangle in the middle
    //int x = originalBitmap.getWidth()/2 - 50, y = originalBitmap.getHeight()/2 - 50, width = 100, height = 100;

    //Get bitmap with region you want to pixelate
    Bitmap original = Bitmap.createBitmap(originalBitmap, x,y,width,height);

    int normalWidth = original.getWidth(), normalHeight = original.getHeight();
    int smallWidth = normalWidth/15, smallHeight = normalHeight/15;

    Bitmap small = getResizedBitmap(original, smallWidth, smallHeight);
    Bitmap pixelated = getResizedBitmap(small, normalWidth, normalHeight);

    //Overlay the pixelated bitmap on top of the original
    Bitmap overlayed = overlayBitmaps(originalBitmap, pixelated,x,y, width, height);
}

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;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    return Bitmap.createBitmap(
            bm, 0, 0, width, height, matrix, false);
}

public static Bitmap overlayBitmaps(Bitmap bmp1, Bitmap bmp2, int x, int y, int width, int height) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    //If you know the background in transparent or any specific color
    //Then you can color the region before overlaying the pixelated bitmap
    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(Color.TRANSPARENT);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    mPaint.setAntiAlias(true);
    canvas.drawRect(x,y, x+width,y+height, mPaint);
    //Overlay the pixelated bitmap
    canvas.drawBitmap(bmp2, x, y, null);
    return bmOverlay;
}

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多