【问题标题】:how to change the color of certain pixels in bitmap android如何更改位图android中某些像素的颜色
【发布时间】:2011-08-20 10:45:09
【问题描述】:

我有一个想要更改某些像素的位图。我已经将位图中的数据放入了一个数组中,但是如何在该数组中设置像素颜色?

谢谢

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
            myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

            for(int i =0; i<500;i++){
                //Log.e(TAG, "pixel"+i +pixels[i]);

【问题讨论】:

    标签: android image-processing bitmap


    【解决方案1】:

    要设置pixels 数组中像素的颜色,请从Android 的Color 类的静态方法中获取值并将它们分配到您的数组中。完成后,使用setPixels 将像素复制回位图。

    例如,将位图的前五行变为蓝色:

    import android.graphics.Color;
    
    int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
    myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
    for (int i=0; i<myBitmap.getWidth()*5; i++)
        pixels[i] = Color.BLUE;
    myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
    

    您还可以一次在 Bitmap 对象中设置一个像素的颜色,而无需使用 setPixel() 方法设置像素缓冲区:

    myBitmap.setPixel(x, y, Color.rgb(45, 127, 0));
    

    【讨论】:

    • 如何获取所有像素
    最近更新 更多