-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add thumbnail extractors * Add thumbnail to File Entry * Add choosing icon or thumbnail in html
- Loading branch information
1 parent
a4ee123
commit 37b01f3
Showing
10 changed files
with
219 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/kotlin/pl/edu/uj/ii/ksi/mordor/services/thumbnail/ImageThumbnailExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package pl.edu.uj.ii.ksi.mordor.services.thumbnail | ||
|
||
import java.awt.AlphaComposite | ||
import java.awt.Color | ||
import java.awt.Graphics2D | ||
import java.awt.Image | ||
import java.awt.Toolkit | ||
import java.awt.image.BufferedImage | ||
import java.awt.image.FilteredImageSource | ||
import java.awt.image.ImageFilter | ||
import java.awt.image.RGBImageFilter | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import javax.imageio.ImageIO | ||
import org.apache.tika.Tika | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ImageThumbnailExtractor(private val tika: Tika) : ThumbnailExtractor() { | ||
override fun extract(file: File): ByteArray? { | ||
return extract(ImageIO.read(file)) | ||
} | ||
|
||
override fun canParse(file: File): Boolean { | ||
return tika.detect(file).startsWith("image") | ||
} | ||
|
||
fun extract(image: BufferedImage): ByteArray? { | ||
val thumbnail = getTransparentScaledImage(image, width, height) | ||
val bos = ByteArrayOutputStream() | ||
ImageIO.write(thumbnail, "png", bos) | ||
return bos.toByteArray() | ||
} | ||
|
||
private fun getTransparentScaledImage(image: BufferedImage, finalWidth: Int, finalHeight: Int): BufferedImage { | ||
val scaledWidth = computeScaledWidth(image, finalWidth, finalHeight) | ||
val scaledHeight = computeScaledHeight(image, finalWidth, finalHeight) | ||
|
||
val scaledImg = BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_RGB) | ||
val transparentImg = BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_ARGB) | ||
|
||
initGraphics2D(scaledImg, finalWidth, finalHeight) | ||
.drawImage(image.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH), | ||
0, 0, null) | ||
|
||
initGraphics2D(transparentImg, finalWidth, finalHeight) | ||
.drawImage(makeColorTransparent(scaledImg, Color(0, 0, 0, 0)), | ||
(finalWidth - scaledWidth) / 2, (finalHeight - scaledHeight) / 2, | ||
finalWidth, finalHeight, Color(0, 0, 0, 0), null) | ||
|
||
return transparentImg | ||
} | ||
|
||
private fun computeScaledWidth(image: BufferedImage, finalWidth: Int, finalHeight: Int): Int { | ||
if (image.width < image.height) { | ||
return (image.width * finalHeight / image.height) | ||
} | ||
return finalWidth | ||
} | ||
|
||
private fun computeScaledHeight(image: BufferedImage, finalWidth: Int, finalHeight: Int): Int { | ||
if (image.width > image.height) { | ||
return (image.height * finalWidth / image.width) | ||
} | ||
return finalHeight | ||
} | ||
|
||
private fun initGraphics2D(image: BufferedImage, finalWidth: Int, finalHeight: Int): Graphics2D { | ||
val graphics2D = image.createGraphics() | ||
graphics2D.composite = AlphaComposite.SrcOver | ||
graphics2D.color = Color(0, 0, 0, 0) | ||
graphics2D.fillRect(0, 0, finalWidth, finalHeight) | ||
return graphics2D | ||
} | ||
|
||
private fun makeColorTransparent(image: BufferedImage, color: Color): Image { | ||
val markerRGB = color.rgb or -0x1000000 | ||
|
||
val filter: ImageFilter = object : RGBImageFilter() { | ||
override fun filterRGB(x: Int, y: Int, rgb: Int): Int { | ||
return if (rgb or -0x1000000 == markerRGB) { | ||
transparent and rgb | ||
} else { | ||
rgb | ||
} | ||
} | ||
} | ||
return Toolkit.getDefaultToolkit().createImage(FilteredImageSource(image.source, filter)) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/pl/edu/uj/ii/ksi/mordor/services/thumbnail/PDFThumbnailExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pl.edu.uj.ii.ksi.mordor.services.thumbnail | ||
|
||
import java.io.File | ||
import org.apache.pdfbox.pdmodel.PDDocument | ||
import org.apache.pdfbox.rendering.PDFRenderer | ||
import org.apache.tika.Tika | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class PDFThumbnailExtractor(private val tika: Tika) : ThumbnailExtractor() { | ||
override fun extract(file: File): ByteArray? { | ||
val doc = PDDocument.load(file) | ||
doc.use { | ||
return ImageThumbnailExtractor(tika).extract(PDFRenderer(doc).renderImage(0)) | ||
} | ||
} | ||
|
||
override fun canParse(file: File): Boolean { | ||
return tika.detect(file) == "application/pdf" | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/pl/edu/uj/ii/ksi/mordor/services/thumbnail/ThumbnailAutoExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pl.edu.uj.ii.ksi.mordor.services.thumbnail | ||
|
||
import java.io.File | ||
import org.apache.tika.Tika | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Service | ||
import pl.edu.uj.ii.ksi.mordor.services.repository.RepositoryService | ||
|
||
@Service | ||
class ThumbnailAutoExtractor(tika: Tika) : ThumbnailExtractor() { | ||
|
||
private val extractors = listOf(ImageThumbnailExtractor(tika), PDFThumbnailExtractor(tika)) | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(RepositoryService::class.java) | ||
} | ||
|
||
override fun extract(file: File): ByteArray? { | ||
for (extractor in extractors) { | ||
if (extractor.canParse(file)) { | ||
logger.info("Extracting thumbnail for " + file.absolutePath + " using " + extractor.javaClass.name) | ||
return extractor.extract(file) | ||
} | ||
} | ||
logger.warn("No thumbnail extractor for file " + file.absolutePath) | ||
return null | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/pl/edu/uj/ii/ksi/mordor/services/thumbnail/ThumbnailExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pl.edu.uj.ii.ksi.mordor.services.thumbnail | ||
|
||
import java.io.File | ||
|
||
abstract class ThumbnailExtractor { | ||
|
||
val width: Int | ||
get() = 200 | ||
|
||
val height: Int | ||
get() = 200 | ||
|
||
val transparent: Int | ||
get() = 0x00FFFFFF | ||
|
||
open fun extract(file: File): ByteArray? { | ||
return null | ||
} | ||
|
||
open fun canParse(file: File): Boolean { | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,8 @@ code.hljs { | |
outline: none; | ||
overflow: hidden; | ||
} | ||
|
||
.thumbnail { | ||
width: 30px; | ||
height: 30px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters