How to bluring and debluring bitmap on seekbar change

核能气质少年 提交于 2021-01-29 03:31:26

问题


i am developing image processing and i want to apply blur effect using seekbar. i am using code below to blur bitmap and it works but now i want to deblur bitmap which i can not achieve...plz help me out to this problem

private Bitmap blurfast(Bitmap bmp, int radius) {
      int w = bmp.getWidth();
      int h = bmp.getHeight();
      int[] pix = new int[w * h];
      bmp.getPixels(pix, 0, w, 0, 0, w, h);

      for(int r = radius; r >= 1; r /= 2) 
      {
          for(int i = r; i < h - r; i++)
          {
              for(int j = r; j < w - r; j++)
              {
                  int tl = pix[(i - r) * w + j - r];
                  int tr = pix[(i - r) * w + j + r];
                  int tc = pix[(i - r) * w + j];
                  int bl = pix[(i + r) * w + j - r];
                  int br = pix[(i + r) * w + j + r];
                  int bc = pix[(i + r) * w + j];
                  int cl = pix[i * w + j - r];
                  int cr = pix[i * w + j + r];

            pix[(i * w) + j] = 0xFF000000 |
            (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) + 
(br & 0xFF) + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF |
            (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00) 
+  (br & 0xFF00) + (bc & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00 |
            (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) + 
(bl & 0xFF0000) + (br & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000) + 
(cr & 0xFF0000)) >> 3) & 0xFF0000;
              }
          }
      }
      bmp.setPixels(pix, 0, w, 0, 0, w, h);
      return bmp;
    }

回答1:


You can't "deblur" the image once it's blurred. The only way you can achieve it is to keep a copy of the original and restore it when necessary. The only way to deal with blurring is to always blur from the original image. You would need to reorganise your code in the following fashion:

private Bitmap origBitmap;
...

Bitmap bmp = //get your starting bitmap here;
origBitmap = Bitmap.createBitmap(bmp);

...

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Bitmap blurred = blurfast(origBitmap, progress);
        //now draw your blurred bitmap into wherever you need it to be
    }

    public void onStartTrackingTouch(SeekBar seekBar) {}
    public void onStopTrackingTouch(SeekBar seekBar) {}
});
...

private Bitmap blurfast(Bitmap bmp, int radius) {
      int w = bmp.getWidth();
      int h = bmp.getHeight();
      int[] pix = new int[w * h];
      bmp.getPixels(pix, 0, w, 0, 0, w, h);

      for(int r = radius; r >= 1; r /= 2) 
      {
          for(int i = r; i < h - r; i++)
          {
              for(int j = r; j < w - r; j++)
              {
                  int tl = pix[(i - r) * w + j - r];
                  int tr = pix[(i - r) * w + j + r];
                  int tc = pix[(i - r) * w + j];
                  int bl = pix[(i + r) * w + j - r];
                  int br = pix[(i + r) * w + j + r];
                  int bc = pix[(i + r) * w + j];
                  int cl = pix[i * w + j - r];
                  int cr = pix[i * w + j + r];

            pix[(i * w) + j] = 0xFF000000 |
            (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) + 
(br & 0xFF) + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF |
            (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00) 
+  (br & 0xFF00) + (bc & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00 |
            (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) + 
(bl & 0xFF0000) + (br & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000) + 
(cr & 0xFF0000)) >> 3) & 0xFF0000;
              }
          }
      }
      Bitmap blurred = Bitmap.createBitmap(w, h, bmp.getConfig);
      blurred.setPixels(pix, 0, w, 0, 0, w, h);
      return blurred;
    }

A few things to note:

  1. In the beginning you are declaring a private member variable of type Bitmap, which will hold your original bitmap, before any blurring has been applied.

  2. Whenever your seekbar moves, you apply your blur to the original bitmap, not the current state of your bitmap.

  3. In the blurfast method, you need to create a new bitmap with the blurred pixels rather than updating the one passed into it (notice the last three lines in my code).

  4. Lastly, keep in mind that if your bitmaps are large, you may run out of memory, so keep a close eye on memory usage.

I haven't tested this code so can't guarantee that this is exactly what you need, but this should get you started.



来源:https://stackoverflow.com/questions/16077797/how-to-bluring-and-debluring-bitmap-on-seekbar-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!