如何在搜索栏更改上模糊和模糊化位图 [英] How to bluring and debluring bitmap on seekbar change

查看:43
本文介绍了如何在搜索栏更改上模糊和模糊化位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发图像处理,我想使用seekbar应用模糊效果.我正在使用下面的代码来模糊位图,并且它可以工作,但是现在我想对我无法实现的位图进行模糊处理...请帮我解决这个问题

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;
    }

推荐答案

一旦图像模糊,就无法去模糊".达到此目的的唯一方法是保留原始副本并在必要时将其还原.的处理模糊的方法是始终使原始图像模糊.您需要按照以下方式重新组织代码:

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;
    }

一些注意事项:

  1. 一开始,您要声明一个类型为 Bitmap 的私有成员变量,该变量将在应用任何模糊效果之前保存原始位图.

  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.

只要搜索栏移动,就将模糊应用于原始位图,而不是位图的当前状态.

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

blurfast 方法中,您需要使用模糊的像素创建一个新的位图,而不是更新传递给它的位图(请注意我代码的最后三行).

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).

最后,请记住,如果位图很大,则可能会耗尽内存,因此请密切注意内存使用情况.

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.

这篇关于如何在搜索栏更改上模糊和模糊化位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆