Android根据Colorbar调整Bitmap颜色

原理:读取colorbar图片。将颜色变化存取到与colorbar高度相同的数组中mColorbar,只需读取第一列颜色值即可。


获取一张需要改变颜色值的bitmap. 将bitmap每个点的颜色值转为灰度值,再根据以下公式获取该灰度值在colorbar的颜色数组colorarray中对应的颜色值


代码如下

1读取colorbar

private int[] mColorbar;


   Bitmap mColorbitmap;

    privatevoid readColorbar() {

                   byte[]bardata;

                   try{

                            InputStreamis = mContext.getResources().openRawResource(R.drawable.colorbar);

                 ByteArrayOutputStream os = newByteArrayOutputStream(is.available());


                 byte[] buffer = new byte[4096];

                 int bytesRead;

                 while ((bytesRead = is.read(buffer)) !=-1) {

                     os.write(buffer, 0, bytesRead);

                 }

                 is.close();

                 bardata = os.toByteArray();


                            BitmapFactory.Optionsoptions = new BitmapFactory.Options();       

                            options.inPreferredConfig= Bitmap.Config.RGB_565;       

                            mColorbitmap= BitmapFactory.decodeByteArray(bardata, 0, bardata.length, options);


                            mColorbar = new

int [mColorbitmap.getHeight()];


                            for(int i = 0; i < mColorbitmap.getHeight(); i++) {

                                               int color =

mColorbitmap.getPixel(0, i);

                                               mColorbar[i]= color;


                                               intr = Color.red(color);

                                               intg = Color.green(color);

                                               intb = Color.blue(color);

                                               inta = Color.alpha(color);

                                               //Log.d(TAG,"readColorbar [" +i +"] r=" + r + "g=" + g+"b=" +b);

                            }

             } catch (IOException e) {

                            Log.e(TAG,"load colorbar exception");

                   }


}


2.根据灰度值获取颜色


Bitmap mBitmap = bitmap.copy(Bitmap.Config.RGB_565,true);          

                   for(int i = 0; i < bitmap.getHeight(); i++) {

           for (int j = 0; j < bitmap.getWidth(); j++) {

                                     intcolor = bitmap.getPixel(j, i);

                /*

                int r = Color.red(color);

                                     intg = Color.green(color);

                                     intb = Color.blue(color);

                                     inta = Color.alpha(color);

                                     */


                                     intr = (color&0x00FF0000)>>16; 

                int g =(color&0x0000FF00)>>8; 

                int b =(color&0x000000FF); 

                int grey =(int)((float)r*0.3+(float)g*0.59+(float)b*0.11); 

               //grey =alpha|(grey<<16)|(grey<<8)|grey;

               

//灰度值除以255,获得当前灰度比重。再乘以colorbar高度。

                int

colorIndex = (grey* mColorbitmap.getHeight())/ 255 ;

                mBitmap.setPixel(j, i,

mColorbar[colorIndex]);


                                     //Log.d(TAG,"onPicFrameChanged [j,i] grey = " + grey + "colorindex = "+ colorIndex);

                                //Log.d(TAG, "onPicFrameChanged[" + j +"," + i +"] r=" + r + "g=" + g+"b=" +b);


                            }

                   }


你可能感兴趣的:(Android根据Colorbar调整Bitmap颜色)