位图的颜色变化速度快 [英] Change colour of a bitmap fastly

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

问题描述

我是想实现一个真正简单的动作,即改变正常的位图的颜色。
不幸的是,出现了一些错误。在我来说,我想灰色钢的位图变成更红钢。因此,我写了一些code,它获取的每个像素的颜色int和提高了每一个红色值。现在,两件事情发生:
首先,它确实需要一个很长很长的时间给所有的像素转换,即使我使用的AsyncTask。
其次,当它完成一个周期,整个位图那种旋转和乘法像下面的图片中。
有没有什么办法可以顺利实现我的目标是什么?事情是,我经常看到在其他应用这一行动没有遇到问题,所以必须要达到我的目标的一种方式。

感谢您!

PS:请不要在评论被激怒,他们只是试图寻找另一种方式

钢形象 https://drive.google。 COM /文件/ D / 0B72QIg-baxzjakJsQkFRZFVFOFU /编辑?USP =共享

 公共无效adjustColor(BMP位图)
{
    / *的for(int i = 0; I< bmp.getHeight() - 1;我++){
        对于(INT J = 0; J< bmp.getWidth() - 1; J ++){
            如果(bmp.getPixel(J,I)!= Color.TRANSPARENT){
            如果(Color.green(bmp.getPixel(J,I))≤; = 175&放大器;&放大器; Color.green(bmp.getPixel(J,I))> = 65){
                红色= Color.red(bmp.getPixel(J,I));
                绿色= Color.green(bmp.getPixel(J,I));
                蓝= Color.blue(bmp.getPixel(J,I));
                如果(ⅰ== bmp.getHeight() - 1){
                    红色= Color.red(bmp.getPixel(J,bmp.getHeight()));
                    绿色= Color.green(bmp.getPixel(J,bmp.getHeight()));
                    蓝= Color.blue(bmp.getPixel(J,bmp.getHeight()));}
                如果((红+ mOfen.heatQ / 10)< = 205)红色=红色+ mOfen.heatQ / 10;
                bmp.setPixel(J,I,Color.rgb(红,绿,蓝));
            }}
        }
    } * /    为(中间体H = 0; H&下; bmp.getWidth()* bmp.getHeight(); 1 H ++){
        INT X = H-bmp.getWidth()*(((int)的H / bmp.getWidth()+ 1)-1);
        INT Y = H / bmp.getWidth();
        Log.d(ROH,Y+ Integer.toString(Y));
        Log.d(ROH,高度+ Integer.toString(bmp.getHeight()));
        如果(Color.green(bmp.getPixel(X,Y))≤; = 175&放大器;&放大器; Color.green(bmp.getPixel(X,Y))GT; = 65){
            bmp.setPixel(X,Y,Color.WHITE);
                红色= Color.red(allpixels并[h]);
                绿色= Color.green(allpixels [H]);
                蓝= Color.blue(allpixels [H]);
    / *为(INT N = 1; N< = 10; N ++){
                    /*Log.d(\"roh,左+ Float.toString(mOfen.rRoh [N-1]。左));
                    Log.d(ROH,右+ Float.toString(mOfen.rRoh [N-1] .right));
                    Log.d(ROH中,n+ Integer.toString(正));
                    Log.d(ROH,X + RohX+ Float.toString(X + RohX));
                    Log.d(ROH,顶+ Float.toString(mOfen.rRoh [N-1] .TOP));
                    Log.d(ROH,底部+ Float.toString(mOfen.rRoh [N-1] .bottom));
                    Log.d(ROH中,n+ Integer.toString(正));
                    Log.d(ROH,Y + RohY+ Float.toString(Y + RohY)); * /
    / *如果(mOfen.rRoh [N-1]。左&所述; X + RohX&放大器;&放大器; Y + RohY> mOfen.rRoh [N-1]赛拓朴&放大器;&放大器;
                        mOfen.rRoh [N-1] .right> X + RohX&放大器;&安培; Y + RohY< mOfen.rRoh [N-1] .bottom){
                        / *如果((mOfen.heat [N-1])&下; = 245&放大器;及(mOfen.heat [N-1])>红色)红色= mOfen.heat [N-1]; * /如果(红色< 255)红++;
    / * Log.d(红,红+ Integer.toString(红色));
                        Log.d(红,Float.toString(X + mOfen.rRoh [N-1]。左));
                        Log.d(红,Float.toString((mOfen.rRoh [N-1]。左)));
                        }} * /
                allpixels并[h] = Color.rgb(红,绿,蓝);
        }
    }
    copyArrayIntoBitmap(BMP);
    //bmp.setPixels(allpixels,0,bmp.getWidth(),0,0,bmp.getWidth(),bmp.getHeight());
}公共无效copyBitmapIntoArray(BMP位图)
{
    allpixels =新INT [bmp.getWidth()* bmp.getHeight()];    诠释计数= 0;
    的for(int i = 0; I< bmp.getHeight() - 1;我++){
        对于(INT J = 0; J< bmp.getWidth() - 1; J ++){
            allpixels [计数] = bmp.getPixel(J,I);
            算上++;
        }
    }
}公共无效copyArrayIntoBitmap(BMP位图)
{
    诠释计数= 0;
    的for(int i = 0; I< bmp.getHeight() - 1;我++){
        对于(INT J = 0; J< bmp.getWidth() - 1; J ++){
            bmp.setPixel(J,I,allpixels [计数]);
            算上++;
        }
    }
}


解决方案

所以你的code和你的问题有被说不同的操作。看起来你正在做的不仅仅是全球更新所有的像素更红一点多一点。我这样说是因为这两个code在你的问题,评论和注释,那么在他们一个条件部分,它确定如何更红做一个像素。我也认为这是为什么一个简单的 LightingColorFilter 将无法正常工作,因为你需要选择哪些像素受到影响。

若干问题我立刻发现:

我打赌你正在运行到大量的I / O问题,在此code问候所有的内存分配和内存复制。宽x高是你的像素数量,效果显着。但是,这也是做一个操作相当大量的entites的。所以,你到与getPixel()与setPixel()反复已经非常非常慢干这一行的每个像素都是电话只是不切实际的。您应该使用散装<一个href=\"http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int%5B%5D,%20int,%20int,%20int,%20int,%20int,%20int%29\"相对=nofollow> 的getPixels() 和<一个href=\"http://developer.android.com/reference/android/graphics/Bitmap.html#setPixels%28int%5B%5D,%20int,%20int,%20int,%20int,%20int,%20int%29\"相对=nofollow> 的setPixels() 如果你要做这种方式。只是仅此要加快大块的过程。

现在,如果你想有一个向前兼容的性能提升(比如一个扩展与更好的硬件更好),你可以看看的 RenderScript 这允许你做在每个像素级别的工作,但在多个CPU的和GPU的。这有点像你的形象缓冲区一个的map-reduce框架。你将不得不写一些C,但如果你发现这个组件中使用的很多,那么这将可能助阵颇有几分是pretty活泼的(特别是较大的图像)。

I was trying to realize an actually simple action, namely to change the color of a normal bitmap. Unfortunately, there appeared a few errors. In my case I want a bitmap of grey steel to turn into redder steel. Therefore, I wrote some code, which gets the color int of each pixel and raises the red value for each one. Now two things happen: Firstly, it really takes a long, long time to convert all the pixels, even if I use an AsyncTask. Secondly, when it finishes one cycle, the whole bitmap kind of rotates and multiplies like in the picture below. Is there any way to smoothly realize my aim? The thing is that I often see this action in other apps without having problems, so the must be a way to reach my goal.

Thank you!

PS: Please do not be irritated by the comments, they are just tries to find another way!

"steel image" https://drive.google.com/file/d/0B72QIg-baxzjakJsQkFRZFVFOFU/edit?usp=sharing

public void adjustColor(Bitmap bmp)
{       
    /*for(int i=0; i<bmp.getHeight()-1; i++) {          
        for(int j=0; j<bmp.getWidth()-1; j++) { 
            if(bmp.getPixel(j, i) != Color.TRANSPARENT) {
            if(Color.green(bmp.getPixel(j, i)) <= 175 && Color.green(bmp.getPixel(j, i)) >= 65) {
                red = Color.red(bmp.getPixel(j, i));
                green = Color.green(bmp.getPixel(j, i));
                blue = Color.blue(bmp.getPixel(j, i));
                if (i == bmp.getHeight()-1) {
                    red = Color.red(bmp.getPixel(j, bmp.getHeight()));
                    green = Color.green(bmp.getPixel(j, bmp.getHeight()));
                    blue = Color.blue(bmp.getPixel(j, bmp.getHeight()));} 
                if ((red + mOfen.heatQ/10) <= 205) red = red + mOfen.heatQ/10;
                bmp.setPixel(j, i, Color.rgb(red, green, blue)); 
            }}
        }
    }*/

    for(int h=0; h<bmp.getWidth()*bmp.getHeight(); h++) {
        int x = h-bmp.getWidth()*(((int)h/bmp.getWidth()+1)-1);
        int y = h/bmp.getWidth();
        Log.d("roh", "y " + Integer.toString(y));
        Log.d("roh", "height " + Integer.toString(bmp.getHeight()));
        if(Color.green(bmp.getPixel(x, y)) <= 175 && Color.green(bmp.getPixel(x, y)) >= 65) {
            bmp.setPixel(x, y, Color.WHITE); 
                red = Color.red( allpixels[h] );
                green = Color.green( allpixels[h] );
                blue = Color.blue( allpixels[h] );
    /*          for(int n=1; n<=10; n++) {              
                    /*Log.d("roh", "left " + Float.toString(mOfen.rRoh[n-1].left));
                    Log.d("roh", "right " + Float.toString(mOfen.rRoh[n-1].right)); 
                    Log.d("roh", "n " + Integer.toString(n));
                    Log.d("roh", "x+RohX " + Float.toString(x+RohX)); 
                    Log.d("roh", "top " + Float.toString(mOfen.rRoh[n-1].top));
                    Log.d("roh", "bottom " + Float.toString(mOfen.rRoh[n-1].bottom)); 
                    Log.d("roh", "n " + Integer.toString(n));
                    Log.d("roh", "y+RohY " + Float.toString(y+RohY)); */
    /*              if( mOfen.rRoh[n-1].left < x+RohX && y+RohY > mOfen.rRoh[n-1].top &&
                        mOfen.rRoh[n-1].right > x+RohX &&  y+RohY < mOfen.rRoh[n-1].bottom) {
                        /*if ((mOfen.heat[n-1]) <= 245 && (mOfen.heat[n-1]) > red ) red = mOfen.heat[n-1]; */ if(red<255) red++; 
    /*                  Log.d("red", "red" + Integer.toString(red)); 
                        Log.d("red", Float.toString(x+mOfen.rRoh[n-1].left)); 
                        Log.d("red", Float.toString((mOfen.rRoh[n-1].left))); 
                        }}  */
                allpixels[h] = Color.rgb(red, green, blue); 
        }
    }


    copyArrayIntoBitmap(bmp);
    //bmp.setPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight()); 
}

public void copyBitmapIntoArray(Bitmap bmp) 
{
    allpixels = new int[bmp.getWidth()*bmp.getHeight()];

    int count = 0;
    for(int i=0; i<bmp.getHeight()-1; i++) {            
        for(int j=0; j<bmp.getWidth()-1; j++) { 
            allpixels[count] = bmp.getPixel(j, i);
            count++;
        }
    } 
}

public void copyArrayIntoBitmap(Bitmap bmp) 
{
    int count = 0;
    for(int i=0; i<bmp.getHeight()-1; i++) {            
        for(int j=0; j<bmp.getWidth()-1; j++) { 
            bmp.setPixel(j, i, allpixels[count]);
            count++;
        }
    } 
} 

解决方案

So your code and your question have different operations being said. Looks like you are doing a little more than just globally updating all pixels to be a bit more red. I say this because both code in your question, commented and uncommented, have a conditional part in them that determines how much more red to make a pixel. I also assume this is why a simple LightingColorFilter will not work, since you need to choose which pixels are affected.

Some issues I spotted immediately:

I am betting you are running into a lot of I/O issues with regards to all the memory allocation and memory copying in this code. WxH is your number of pixels, obviously. But that is also a fairly large amount of entites to do an operation on. So your calls to getPixel() and setPixel() iteratively are very very slow already and doing this for each pixel is just impractical. You should use bulk getPixels() and setPixels() if you are going to do it this way. Just this alone should speed up a large chunk of your process.

Now if you want a forward compatible performance boost (like one that scales better with better hardware), you could look into RenderScript This allows you to do work on a per pixel level, but across multiple CPU's and GPU's. It's sort of like a map-reduce framework for your image buffers. You will have to write some C, but if you find this component to be used a lot then this will probably help out quite a bit and be pretty snappy (especially for larger images).

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

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