From e7a971ce2e85ff6c875ea0023f7e69672acc0608 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Sun, 26 Jan 2014 11:55:19 -0600 Subject: [PATCH] Added a similarImages class, updated some hashing methods --- src/main/scala/com/sothr/imagetools/Engine.scala | 6 +++++- .../com/sothr/imagetools/hash/HashService.scala | 15 ++++++++++----- .../scala/com/sothr/imagetools/image/Image.scala | 13 ++----------- .../com/sothr/imagetools/image/ImageService.scala | 7 +++++-- .../sothr/imagetools/image/SimilarImages.scala | 10 ++++++++++ 5 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 src/main/scala/com/sothr/imagetools/image/SimilarImages.scala diff --git a/src/main/scala/com/sothr/imagetools/Engine.scala b/src/main/scala/com/sothr/imagetools/Engine.scala index 7b7d66f..8e300ff 100644 --- a/src/main/scala/com/sothr/imagetools/Engine.scala +++ b/src/main/scala/com/sothr/imagetools/Engine.scala @@ -1,6 +1,6 @@ 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.mutable import java.io.File @@ -28,4 +28,8 @@ class Engine extends Logging{ images.toList } + def getSimilarImagesForDirectory(directoryPath:String):List[SimilarImages] = { + null + } + } diff --git a/src/main/scala/com/sothr/imagetools/hash/HashService.scala b/src/main/scala/com/sothr/imagetools/hash/HashService.scala index ff61774..b899970 100644 --- a/src/main/scala/com/sothr/imagetools/hash/HashService.scala +++ b/src/main/scala/com/sothr/imagetools/hash/HashService.scala @@ -8,6 +8,7 @@ import java.awt.image.BufferedImage import javax.imageio.ImageIO import java.io.{FileInputStream, File} 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 @@ -17,8 +18,12 @@ import org.apache.commons.codec.digest.DigestUtils object HashService extends Logging { def getImageHashes(imagePath:String):ImageHashDTO = { - 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 dhash:Long = 0L @@ -26,7 +31,7 @@ object HashService extends Logging { val md5:String = getMD5(imagePath) //Get Image Data - val grayImage = ImageService.convertToGray(ImageIO.read(new File(imagePath))) + val grayImage = ImageService.convertToGray(image) if (PropertiesService.get(PropertiesEnum.UseAhash.toString) == "true") { ahash = getAhash(grayImage, true) @@ -37,11 +42,11 @@ object HashService extends Logging { if (PropertiesService.get(PropertiesEnum.UseAhash.toString) == "true") { phash = getPhash(grayImage, true) } - + val hashes = new ImageHashDTO(ahash, dhash, phash, md5) debug(s"Generated hashes: $hashes") - - return hashes + + hashes } def getAhash(image:BufferedImage, alreadyGray:Boolean = false):Long = { diff --git a/src/main/scala/com/sothr/imagetools/image/Image.scala b/src/main/scala/com/sothr/imagetools/image/Image.scala index 91b186f..d1e982c 100644 --- a/src/main/scala/com/sothr/imagetools/image/Image.scala +++ b/src/main/scala/com/sothr/imagetools/image/Image.scala @@ -1,10 +1,9 @@ package com.sothr.imagetools.image -import scala.collection.Traversable import com.sothr.imagetools.dto.ImageHashDTO 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 @@ -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 = { - s"Image: $imagePath Thumbnail: $thumbnailPath Hashes: $hashes" + s"Image: $imagePath Thumbnail: $thumbnailPath Image Size: ${imageSize._1}x${imageSize._2} Hashes: $hashes" } } diff --git a/src/main/scala/com/sothr/imagetools/image/ImageService.scala b/src/main/scala/com/sothr/imagetools/image/ImageService.scala index 3796fb1..27e2ef2 100644 --- a/src/main/scala/com/sothr/imagetools/image/ImageService.scala +++ b/src/main/scala/com/sothr/imagetools/image/ImageService.scala @@ -6,13 +6,16 @@ import net.coobird.thumbnailator.Thumbnails import java.io.File import com.sothr.imagetools.image.Image import com.sothr.imagetools.hash.HashService +import javax.imageio.ImageIO object ImageService extends Logging { def getImage(file:File):Image = { 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") image } diff --git a/src/main/scala/com/sothr/imagetools/image/SimilarImages.scala b/src/main/scala/com/sothr/imagetools/image/SimilarImages.scala new file mode 100644 index 0000000..14097fc --- /dev/null +++ b/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 { + +}