Advertisement
myLoveOnlyForYou

suck my blur nigga

May 24th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.45 KB | None | 0 0
  1. // DAMN BLUR
  2.     @SuppressLint("ClickableViewAccessibility")
  3.     fun blur(mainImage: ImageView, coordinates: TextView) {
  4.         mainImage.setOnTouchListener(View.OnTouchListener { _, event ->
  5.             // click coordinates
  6.             var rawX = event.x
  7.             var rawY = event.y
  8.  
  9.             var oldBitmap = (mainImage.drawable as BitmapDrawable).bitmap
  10.             var height = oldBitmap.height
  11.             var width = oldBitmap.width
  12.             // get bitmap coordinates
  13.             val x = (rawX.toDouble() * (width.toDouble() / mainImage.width.toDouble())).toInt()
  14.             val y = (rawY.toDouble() * (height.toDouble() / mainImage.height.toDouble())).toInt()
  15.  
  16. //            coordinates.text = "$x | $y"
  17.             var oldBittmapPixelsArray = IntArray(width * height) // empty pixels array
  18.             var newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) // new bitmap
  19.             var newBitmapPixelsArray = oldBittmapPixelsArray // the same for new bitmap
  20.             oldBitmap.getPixels(oldBittmapPixelsArray, 0, width, 0, 0, width, height) // filling pixels
  21.             var count = 0
  22.             val array = oldBittmapPixelsArray //массив - донор
  23.             val matrix = Array(height) { IntArray(width) } //будущая матрица
  24.             for (i in matrix.indices) {
  25.                 for (j in 0 until matrix[i].size) {
  26.                     matrix[i][j] = array[count++] //перенос элементов из донора в матрицу
  27.                 }
  28.             }
  29.             var redSum = 0
  30.             var greenSum = 0
  31.             var blueSum = 0
  32.  
  33.             var eps = (height * width) / (5000) // ЭТО ОКРЕСТНОСТЬ БЛЮРА (тип радиус), КОРОЧЕ НАДО ПРИДУМАТЬ, как ее менять в зависимости от разрешения
  34.  
  35.             for (i in y - eps..y + eps){
  36.                 for (j in x - eps..x + eps){
  37.                     if (i in 0..(height - 1) && j >= 0 && j < width){
  38.                         var red = 0
  39.                         var green = 0
  40.                         var blue = 0
  41.                         red = matrix[i][j] and 0x00ff0000 shr 16
  42.                         green = matrix[i][j] and 0x0000ff00 shr 8
  43.                         blue = matrix[i][j] and 0x000000ff shr 0
  44.                         redSum += red
  45.                         greenSum += green
  46.                         blueSum += blue
  47.                     }
  48.                 }
  49.             }
  50.             redSum /= ((1 + 2 * eps) * (1 + 2 * eps))
  51.             greenSum /= ((1 + 2 * eps) * (1 + 2 * eps))
  52.             blueSum /= ((1 + 2 * eps) * (1 + 2 * eps))
  53.             for (i in y - eps..y + eps){
  54.                 for (j in x - eps..x + eps){
  55.                     if (i in 0..(height - 1) && j >= 0 && j < width){
  56.                         matrix[i][j] = ((0xff000000) or (redSum.toLong() shl 16) or (greenSum.toLong() shl 8) or (blueSum.toLong() shl 0)).toInt()
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             for (row in 0 until height){
  62.                 for (column in 0 until width) {
  63.                     newBitmapPixelsArray[(row * width) + column] = matrix[row][column] // from matrix to new empty pixels array
  64.                 }
  65.             }
  66.             newBitmap.setPixels(newBitmapPixelsArray, 0, width, 0, 0, width, height)
  67.             mainImage.setImageBitmap(newBitmap)
  68.  
  69.             return@OnTouchListener true
  70.         })
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement