Browse Source

Added a similarImages class, updated some hashing methods

master
Drew Short 11 years ago
parent
commit
e7a971ce2e
  1. 6
      src/main/scala/com/sothr/imagetools/Engine.scala
  2. 11
      src/main/scala/com/sothr/imagetools/hash/HashService.scala
  3. 13
      src/main/scala/com/sothr/imagetools/image/Image.scala
  4. 7
      src/main/scala/com/sothr/imagetools/image/ImageService.scala
  5. 10
      src/main/scala/com/sothr/imagetools/image/SimilarImages.scala

6
src/main/scala/com/sothr/imagetools/Engine.scala

@ -1,6 +1,6 @@
package com.sothr.imagetools package com.sothr.imagetools
import com.sothr.imagetools.image.{ImageFilter, Image}
import com.sothr.imagetools.image.{SimilarImages, ImageFilter, Image}
import scala.collection.immutable import scala.collection.immutable
import scala.collection.mutable import scala.collection.mutable
import java.io.File import java.io.File
@ -28,4 +28,8 @@ class Engine extends Logging{
images.toList images.toList
} }
def getSimilarImagesForDirectory(directoryPath:String):List[SimilarImages] = {
null
}
} }

11
src/main/scala/com/sothr/imagetools/hash/HashService.scala

@ -8,6 +8,7 @@ import java.awt.image.BufferedImage
import javax.imageio.ImageIO import javax.imageio.ImageIO
import java.io.{FileInputStream, File} import java.io.{FileInputStream, File}
import org.apache.commons.codec.digest.DigestUtils import org.apache.commons.codec.digest.DigestUtils
import com.sothr.imagetools.image.Image
/** /**
* A service that exposes the ability to construct perceptive hashes from an * A service that exposes the ability to construct perceptive hashes from an
@ -17,8 +18,12 @@ import org.apache.commons.codec.digest.DigestUtils
object HashService extends Logging { object HashService extends Logging {
def getImageHashes(imagePath:String):ImageHashDTO = { def getImageHashes(imagePath:String):ImageHashDTO = {
debug(s"Creating hashes for $imagePath") debug(s"Creating hashes for $imagePath")
getImageHashes(ImageIO.read(new File(imagePath)), imagePath)
}
def getImageHashes(image:BufferedImage, imagePath:String):ImageHashDTO = {
debug(s"Creating hashes for image")
var ahash:Long = 0L var ahash:Long = 0L
var dhash:Long = 0L var dhash:Long = 0L
@ -26,7 +31,7 @@ object HashService extends Logging {
val md5:String = getMD5(imagePath) val md5:String = getMD5(imagePath)
//Get Image Data //Get Image Data
val grayImage = ImageService.convertToGray(ImageIO.read(new File(imagePath)))
val grayImage = ImageService.convertToGray(image)
if (PropertiesService.get(PropertiesEnum.UseAhash.toString) == "true") { if (PropertiesService.get(PropertiesEnum.UseAhash.toString) == "true") {
ahash = getAhash(grayImage, true) ahash = getAhash(grayImage, true)
@ -41,7 +46,7 @@ object HashService extends Logging {
val hashes = new ImageHashDTO(ahash, dhash, phash, md5) val hashes = new ImageHashDTO(ahash, dhash, phash, md5)
debug(s"Generated hashes: $hashes") debug(s"Generated hashes: $hashes")
return hashes
hashes
} }
def getAhash(image:BufferedImage, alreadyGray:Boolean = false):Long = { def getAhash(image:BufferedImage, alreadyGray:Boolean = false):Long = {

13
src/main/scala/com/sothr/imagetools/image/Image.scala

@ -1,10 +1,9 @@
package com.sothr.imagetools.image package com.sothr.imagetools.image
import scala.collection.Traversable
import com.sothr.imagetools.dto.ImageHashDTO import com.sothr.imagetools.dto.ImageHashDTO
import com.sothr.imagetools.hash.HashService import com.sothr.imagetools.hash.HashService
class Image(val imagePath:String, val thumbnailPath:String, var hashes:ImageHashDTO = null) {
class Image(val imagePath:String, val thumbnailPath:String, val imageSize:Tuple2[Int,Int], var hashes:ImageHashDTO = null) {
var imageType:ImageType = ImageType.SingleFrameImage var imageType:ImageType = ImageType.SingleFrameImage
@ -20,16 +19,8 @@ class Image(val imagePath:String, val thumbnailPath:String, var hashes:ImageHash
}*/ }*/
def getPath:String = {
this.imagePath
}
def getThumbnailPath:String = {
this.thumbnailPath
}
override def toString:String = { override def toString:String = {
s"Image: $imagePath Thumbnail: $thumbnailPath Hashes: $hashes"
s"Image: $imagePath Thumbnail: $thumbnailPath Image Size: ${imageSize._1}x${imageSize._2} Hashes: $hashes"
} }
} }

7
src/main/scala/com/sothr/imagetools/image/ImageService.scala

@ -6,13 +6,16 @@ import net.coobird.thumbnailator.Thumbnails
import java.io.File import java.io.File
import com.sothr.imagetools.image.Image import com.sothr.imagetools.image.Image
import com.sothr.imagetools.hash.HashService import com.sothr.imagetools.hash.HashService
import javax.imageio.ImageIO
object ImageService extends Logging { object ImageService extends Logging {
def getImage(file:File):Image = { def getImage(file:File):Image = {
val thumbnailPath = getThumbnailPath(file) val thumbnailPath = getThumbnailPath(file)
val hashes = HashService.getImageHashes(file.getAbsolutePath)
val image = new Image(file.getAbsolutePath, thumbnailPath, hashes)
val bufferedImage = ImageIO.read(file)
val hashes = HashService.getImageHashes(bufferedImage, file.getAbsolutePath)
val imageSize = { (bufferedImage.getWidth, bufferedImage.getHeight) }
val image = new Image(file.getAbsolutePath, thumbnailPath, imageSize, hashes)
debug(s"Created image: $image") debug(s"Created image: $image")
image image
} }

10
src/main/scala/com/sothr/imagetools/image/SimilarImages.scala

@ -0,0 +1,10 @@
package com.sothr.imagetools.image
import grizzled.slf4j.Logging
/**
* Created by drew on 1/26/14.
*/
class SimilarImages(val rootImage:Image, val similarImages:List[Image]) extends Logging {
}
Loading…
Cancel
Save