You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. package com.sothr.imagetools.hash
  2. import grizzled.slf4j.Logging
  3. /**
  4. * Created by dev on 1/22/14.
  5. */
  6. object DHash extends PerceptualHasher with Logging {
  7. def getHash(imageData: Array[Array[Int]]): Long = {
  8. debug("Generating DHash")
  9. val width = imageData.length
  10. val height = imageData(0).length
  11. debug(s"Image data size: ${width}x${height}")
  12. var hash = 0L
  13. for (row <- 0 until width) {
  14. //println(f"Row: $row%d")
  15. var previousPixel = imageData(row)(0)
  16. var previousLocation = (row, 0)
  17. //process each column
  18. for (col <- 0 until height) {
  19. debug(s"previousPixel: $previousPixel previousLocation: $previousLocation")
  20. //println(f"Column: $col%d")
  21. hash <<= 1
  22. val pixel = imageData(row)(col)
  23. //binary or the current bit based on whether the value
  24. //of the current pixel is greater or equal to the previous pixel
  25. if (pixel >= previousPixel) hash |= 1 else hash |= 0
  26. debug(s"hash: hash")
  27. previousPixel = pixel
  28. previousLocation = (row, col)
  29. }
  30. }
  31. hash
  32. }
  33. }