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.
 
 
 

52 lines
1.3 KiB

package com.sothr.imagetools.hash.`type`
import grizzled.slf4j.Logging
/**
* Speedy AHash Perceptual Hashing
* Uses the average value of the pixels as a baseline to determine the structure of the image
*
* Created by dev on 1/22/14.
*/
object AHash extends PerceptualHasher with Logging {
def getHash(imageData: Array[Array[Int]]): Long = {
trace("Generating AHash")
val width = imageData.length
val height = imageData(0).length
trace(s"Image data size: ${width}x$height")
/*
Average Pixel Calculation
*/
var total = 0
for (row <- 0 until height) {
for (col <- 0 until width) {
total += imageData(row)(col)
}
}
val mean = total / (height * width)
/*
For each pixel, if it is at or above the average, store it as a one, else store it as a zero
*/
var hash = 0L
for (row <- 0 until height by 2) {
for (col <- 0 until width by 1) {
hash <<= 1
val pixel = imageData(row)(col)
if (pixel >= mean) hash |= 1 else hash |= 0
}
if ((row + 1) < width) {
val nextRow = row + 1
for (col <- (width - 1) to 0 by -1) {
hash <<= 1
val pixel = imageData(nextRow)(col)
if (pixel >= mean) hash |= 1 else hash |= 0
}
}
}
debug(s"Computed AHash: $hash from ${width * height} pixels")
hash
}
}