0

我有一个在位图上放置鱼眼失真效果的应用程序。要创建失真,我必须遍历整个位图,检查给定像素是否落入圆形边界。如果确实如此,那么我会操纵该像素。这个过程是劳动密集型的,需要长达 50 秒。我正在考虑不同的方法来做到这一点,所以我不必循环整个位图来应用效果。我的一个想法是先绘制位图并显示它。然后创建第二个仅具有效果的位图叠加。然后我可以将第二个位图覆盖在第一个位图上。我只是想想想我可以应用这种效果的方法,而无需循环通过尽可能多的像素来加快速度。我会发布失真类。谢谢。

.

class Filters{
    private float xscale;
    private float yscale;
    private float xshift;
    private float yshift;
    private int [] s;
    private int [] scalar;
    private int [] s1;
    private int [] s2;
    private int [] s3;
    private int [] s4;
    private String TAG = "Filters";
    long getRadXStart = 0;
    long getRadXEnd = 0;
    long startSample = 0;
    long endSample = 0;
    public Filters(){

        Log.e(TAG, "***********inside filter constructor");
        s = new int[4];
        scalar = new int[4];
        s1 = new int[4];
        s2 = new int[4];
        s3 = new int[4];
        s4 = new int[4];
    }

    public Bitmap barrel (Bitmap input, float k,float cenx, float ceny){
        //Log.e(TAG, "***********INSIDE BARREL METHOD ");
        Debug.startMethodTracing("barrel");

        //float centerX=input.getWidth()/2; //center of distortion
        //float centerY=input.getHeight()/2;
        float centerX=cenx;
        float centerY=ceny;

        int width = input.getWidth(); //image bounds
        int height = input.getHeight();

        Bitmap dst = Bitmap.createBitmap(width, height,input.getConfig() ); //output pic
       // Log.e(TAG, "***********dst bitmap created ");
          xshift = calc_shift(0,centerX-1,centerX,k);

          float newcenterX = width-centerX;
          float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);

          yshift = calc_shift(0,centerY-1,centerY,k);

          float newcenterY = height-centerY;
          float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);

          xscale = (width-xshift-xshift_2)/width;
        //  Log.e(TAG, "***********xscale ="+xscale);
          yscale = (height-yshift-yshift_2)/height;
        //  Log.e(TAG, "***********yscale ="+yscale);
        //  Log.e(TAG, "***********filter.barrel() about to loop through bm");
          /*for(int j=0;j<dst.getHeight();j++){
              for(int i=0;i<dst.getWidth();i++){
                float x = getRadialX((float)i,(float)j,centerX,centerY,k);
                float y = getRadialY((float)i,(float)j,centerX,centerY,k);
                sampleImage(input,x,y);
                int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
    //            System.out.print(i+" "+j+" \\");

                dst.setPixel(i, j, color);

              }
            }*/

          int origPixel;
          long startLoop = System.currentTimeMillis();
          for(int j=0;j<dst.getHeight();j++){
              for(int i=0;i<dst.getWidth();i++){
                 origPixel= input.getPixel(i,j);
                 getRadXStart = System.currentTimeMillis();
                float x = getRadialX((float)j,(float)i,centerX,centerY,k);
                getRadXEnd= System.currentTimeMillis();

                float y = getRadialY((float)j,(float)i,centerX,centerY,k);

                sampleImage(input,x,y);

                int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
    //            System.out.print(i+" "+j+" \\");

                //if( Math.sqrt( Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) ) ) <= 150 ){
                if(  Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) )  <= 22500 ){
                dst.setPixel(i, j, color);
                }else{
                    dst.setPixel(i,j,origPixel);
                }
              }
            }
          long endLoop = System.currentTimeMillis();
          long loopDuration = endLoop - startLoop;
          long radXDuration = getRadXEnd - getRadXStart;
          long sampleDur = endSample - startSample;

          Log.e(TAG, "sample method took "+sampleDur+"ms");
          Log.e(TAG, "getRadialX took "+radXDuration+"ms");
          Log.e(TAG, "loop took "+loopDuration+"ms");

        //  Log.e(TAG, "***********filter.barrel()  looped through bm about to return dst bm");
          Debug.stopMethodTracing();
        return dst;

    }

    void sampleImage(Bitmap arr, float idx0, float idx1)
    {
         startSample = System.currentTimeMillis();
       // s = new int [4];
      if(idx0<0 || idx1<0 || idx0>(arr.getHeight()-1) || idx1>(arr.getWidth()-1)){
        s[0]=0;
        s[1]=0;
        s[2]=0;
        s[3]=0;
        return;
      }

      float idx0_fl=(float) Math.floor(idx0);
      float idx0_cl=(float) Math.ceil(idx0);
      float idx1_fl=(float) Math.floor(idx1);
      float idx1_cl=(float) Math.ceil(idx1);

     /* float idx0_fl=idx0;
      float idx0_cl=idx0;
      float idx1_fl=idx1;
      float idx1_cl=idx1;*/

     /* int [] s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
      int [] s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
      int [] s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
      int [] s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);*/

       s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
       s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
       s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
       s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);

      float x = idx0 - idx0_fl;
      float y = idx1 - idx1_fl;

      s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
      s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
      s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
      s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));

      endSample = System.currentTimeMillis();
    }

    int [] getARGB(Bitmap buf,int x, int y){

        int rgb = buf.getPixel(y, x); // Returns by default ARGB.
       // int [] scalar = new int[4];
        scalar[0] = (rgb >>> 24) & 0xFF;
        scalar[1] = (rgb >>> 16) & 0xFF;
        scalar[2] = (rgb >>> 8) & 0xFF;
        scalar[3] = (rgb >>> 0) & 0xFF;
        return scalar;
    }

    float getRadialX(float x,float y,float cx,float cy,float k){

      x = (x*xscale+xshift);
      y = (y*yscale+yshift);
      float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      return res;
    }

    float getRadialY(float x,float y,float cx,float cy,float k){

      x = (x*xscale+xshift);
      y = (y*yscale+yshift);
      float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
      return res;
    }

    float thresh = 1;

    float calc_shift(float x1,float x2,float cx,float k){

      float x3 = (float)(x1+(x2-x1)*0.5);
      float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
      float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));

      if(res1>-thresh && res1 < thresh)
        return x1;
      if(res3<0){
        return calc_shift(x3,x2,cx,k);
      }
      else{
        return calc_shift(x1,x3,cx,k);
      }
    }



}// end of filters class

.

[更新] 嗨,我还没有看过所有的视频,因为我在加密狗上只有这么多的数据限额,所以要等到上班再看。我已将代码修改为以下代码。这将像素数据存储在一个 int 数组中,因此无需调用 dst.setPixel。它仍然很慢(在 3.2MP 相机上为 14 秒),根本不像您的代码那样需要几秒钟。您能否分享该代码或告诉我这是否不是您的意思。谢谢马特。

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

          int p = 0;
          int i = 0;
          for(int j=0;j<dst.getHeight();j++){
              for( i=0;i<dst.getWidth();i++,p++){
                 origPixel= input.getPixel(i,j);

                float x = getRadialX((float)j,(float)i,centerX,centerY,k);


                float y = getRadialY((float)j,(float)i,centerX,centerY,k);

                sampleImage(input,x,y);

                 color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
    //            System.out.print(i+" "+j+" \\");

                //if( Math.sqrt( Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) ) ) <= 150 ){
                if(  Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) )  <= 22500 ){
                //dst.setPixel(i, j, color);
                    arr[p]=color;
                    Log.e(TAG, "***********arr = " +arr[i]+" i = "+i);
                }else{
                    //dst.setPixel(i,j,origPixel);
                    arr[p]=origPixel;

                }
              }
            }



        //  Log.e(TAG, "***********filter.barrel()  looped through bm about to return dst bm");
          Debug.stopMethodTracing();
         Bitmap dst2 = Bitmap.createBitmap(arr,width,height,input.getConfig());
        return dst2;

    }
4

1 回答 1

1

我敢打赌,如果您在内部循环中消除了对 dst.setPixel 的调用,您会大大减少执行时间。不要在循环内对位图进行操作,而是在循环期间将值填充到整数数组中,并在传入数组的末尾调用 setPixels。

我有可以在几秒钟内循环遍历整个 2MP 图像的图像处理代码。

在较旧的 Android api(我相信早于 2.3,但它甚至可能包括 2.3)上,实际的图像数据并不驻留在托管堆中,因此可能需要进行一些昂贵的操作来查找您正在覆盖的位的实际位置在对 setPixel 的调用中。我的信息来源是关于 Android 内存管理的 Google I/O 2011 视频。如果您在 Android 中进行此类工作,则必须注意:

http://www.youtube.com/watch?v=_CruQY55HOk

于 2011-06-21T17:24:00.010 回答