反转位图的颜色 [英] Invert bitmap colors

查看:158
本文介绍了反转位图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我有一个图表程序,它的设计是黑色的,但图表(我从服务器作为图像获取),轻(它实际上只使用了5种颜色:红色,绿色,白色,黑色和灰色)。

I have the following problem. I have a charting program, and it's design is black, but the charts (that I get from the server as images) are light (it actually uses only 5 colors: red, green, white, black and gray).

要适应的设计反转做得很好,唯一的问题是,红色和绿色也倒(绿 - >粉红色,红色 - >绿色)。

To fit with the design inversion does a good job, the only problem is that red and green are inverted also (green -> pink, red -> green).

有没有办法颠倒一切,除了那些2种颜色,还是有办法反转后重绘这些颜色? 而如何昂贵的是那些操作(因为我得到了海图更新pretty的常)?

Is there a way to invert everything except those 2 colors, or a way to repaint those colors after inversion? And how costly are those operations (since I get the chart updates pretty often)?

在此先感谢:)

更新

我试图替换颜色与setPixel方法在一个循环

I tried replacing colors with setPixel method in a loop

for(int x = 0 ;x < chart.getWidth();x++) {
        for(int y = 0;y < chart.getHeight();y++) {
            final int replacement = getColorReplacement(chart.getPixel(x, y));
            if(replacement != 0) {
                chart.setPixel(x, y, replacement);
            }
        }
    }

Unfortunetely,该方法花费的时间太长(〜650ms),是否有一个更快的方法来做到这一点,并会的setPixels()方法的更快?

Unfortunetely, the method takes too long (~650ms), is there a faster way to do it, and will setPixels() method work faster?

推荐答案

处理位图的速度要快得多,如果你拨打的getPixels图像数据复制到一个int数组只有一次,不叫循环中的任何功能。只需操纵数组,然后调用的setPixels底。

Manipulating a bitmap is much faster if you copy the image data into an int array by calling getPixels only once, and don't call any function inside the loop. Just manipulate the array, then call setPixels at the end.

这样的东西:

int length = bitmap.getWidth()*bitmap.getHeight();
int[] array = new int[length];
bitmap.getPixels(array,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
for (int i=0;i<length;i++){
// If the bitmap is in ARGB_8888 format
  if (array[i] == 0xff000000){
    array[i] = 0xffffffff;
  } else if ...
  }
}
bitmap.setPixels(array,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());

这篇关于反转位图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆