通过位图循环到数组

时间:2011-06-22 08:59:54

标签: java android image-processing bitmap

是否可以循环遍历位图并将每个颜色值设置为数组?目前只有顶行的数组被写入dst位图。例如

Bitmap dst = Bitmap.createBitmap(width, height,input.getConfig() ); //output pic


          int origPixel = 0;
          int []arr = new int[input.getWidth()*input.getHeight()];
          int color = 0;

          for(int j=0;j<dst.getHeight();j++){
              for(int i=0;i<dst.getWidth();i++){

           origPixel= input.getPixel(i,j);

           color = ........do something special with that pixel transform it whatever



           if(  Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) )  <= 22500 ){

                arr[i]=color;

            }else{

                arr[i]=origPixel;
            }
  }

}

    Bitmap dst2 = Bitmap.createBitmap(arr,width,height,input.getConfig());
    return dst2;

1 个答案:

答案 0 :(得分:2)

你需要更新arr [k],其中k在第​​一次循环之前初始化,在第二次循环中递增,看到修改后的代码是你代码的一大块:

int k =0;
 for(int j=0;j<dst.getHeight();j++){
              for(int i=0;i<dst.getWidth();i++, k++){

           origPixel= input.getPixel(i,j);

           color = ........do something special with that pixel transform it whatever



           if(  Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) )  <= 22500 ){

                arr[k]=color;

            }else{

                arr[k]=origPixel;
            }

你要覆盖数组中的值。