android - How to bluring and debluring bitmap on seekbar change -
i developing image processing , want apply blur effect using seekbar. using code below blur bitmap , works want deblur bitmap can not achieve...plz me out 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 = r; < 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" image once it's blurred. way can achieve keep copy of original , restore when necessary. only way deal blurring blur original image. need reorganise code in following fashion:
private bitmap origbitmap; ... bitmap bmp = //get 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 blurred bitmap wherever need } 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 = r; < 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 note:
in beginning declaring private member variable of type
bitmap
, hold original bitmap, before blurring has been applied.whenever seekbar moves, apply blur original bitmap, not current state of bitmap.
in
blurfast
method, need create new bitmap blurred pixels rather updating 1 passed (notice last 3 lines in code).lastly, keep in mind if bitmaps large, may run out of memory, keep close eye on memory usage.
i haven't tested code can't guarantee need, should started.
Comments
Post a Comment