代码需要1分钟才能处理

时间:2016-09-21 18:25:16

标签: android

此代码遍历图片的所有像素,改变每个像素的rgb。当我运行代码时,大约需要1-2分钟才能完成。我认为这是因为循环完成了整个画面的590万次。我怎么能像>那样更快地完成这个过程10秒或接近此。

感谢任何帮助。感谢

int r,g,b,colour;   
int [] allpixels = new int [picture.getHeight() * picture.getWidth()];
picture.getPixels(allpixels, 0, picture.getWidth(), 0, 0, picture.getWidth(), picture.getHeight());

                    for(int i = 0; i < allpixels.length; i++)
                    {
                        colour = allpixels[i];
                        r = Color.red(colour);
                        b = Color.blue(colour);
                        g = Color.green(colour);

                        if(r < FIRSTSECTION){
                            r = FIRST;
                        }else if(r >= SECONDSECTION && r < THIRDSECTION){
                            r = SECOND;
                        }else if(r >= THIRDSECTION && r < FOURTHSECTION){
                            r = THIRD;
                        }else if(r >= FOURTHSECTION && r < FIFTHSECTION){
                            r = FORTH;
                        }

                        if(b < FIRSTSECTION){
                            b = FIRST;
                        }else if(b >= SECONDSECTION && b < THIRDSECTION){
                            b = SECOND;
                        }else if(b >= THIRDSECTION && b < FOURTHSECTION){
                            b = THIRD;
                        }else if(b >= FOURTHSECTION && b < FIFTHSECTION){
                            b = FORTH;
                        }

                        if(g < FIRSTSECTION){
                            g = FIRST;
                        }else if(g >= SECONDSECTION && g < THIRDSECTION){
                            g = SECOND;
                        }else if(g >= THIRDSECTION && g < FOURTHSECTION){
                            g = THIRD;
                        }else if(g >= FOURTHSECTION && g < FIFTHSECTION){
                            g = FORTH;
                        }

                        allpixels[i] = Color.argb(1, r, g, b);

                    }

                    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
                    bitmap.setPixels(allpixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
                    drawableBitmap = new BitmapDrawable(getResources(), bitmap);

1 个答案:

答案 0 :(得分:1)

我会考虑使用opengl来做这样的事情。这可以使用基本着色器完成,并且比在Java中运行速度快得多。有关opengl es入门的信息,请参阅此链接。 https://developer.android.com/training/graphics/opengl/index.html

这也是一个非常全面的教程,虽然可能会略微过时http://www.learnopengles.com/android-lesson-one-getting-started/