Fix sizes

This commit is contained in:
Julius de Jeu 2019-01-21 21:18:31 +01:00
parent 3d34c197fe
commit d5edabddec

View file

@ -1,20 +1,21 @@
package nl.aegeedelft.watermarker package nl.aegeedelft.watermarker
import org.intellij.lang.annotations.Language import org.intellij.lang.annotations.Language
import java.awt.* import java.awt.BorderLayout
import java.awt.Component
import java.awt.Dimension
import java.awt.Image
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.awt.image.RenderedImage import java.awt.image.RenderedImage
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.File import java.io.File
import java.util.*
import javax.imageio.IIOImage import javax.imageio.IIOImage
import javax.imageio.ImageIO import javax.imageio.ImageIO
import javax.imageio.ImageWriteParam import javax.imageio.ImageWriteParam
import javax.imageio.stream.MemoryCacheImageOutputStream import javax.imageio.stream.MemoryCacheImageOutputStream
import javax.swing.* import javax.swing.*
import javax.swing.filechooser.FileFilter import javax.swing.filechooser.FileFilter
import kotlin.concurrent.timerTask
@Language("RegExp") @Language("RegExp")
@ -120,14 +121,16 @@ fun main(args: Array<String>) {
} }
fun doTheThing(basedir: File, outdir: File, watermarkpath: File) { fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
val watermark = ImageIO.read(watermarkpath) val wmbase = ImageIO.read(watermarkpath)
val scale = 3.6
//JOptionPane.showMessageDialog(jf, "", "Inserting watermark!", JOptionPane.INFORMATION_MESSAGE, ImageIcon(watermark)) //JOptionPane.showMessageDialog(jf, "", "Inserting watermark!", JOptionPane.INFORMATION_MESSAGE, ImageIcon(watermark))
//FrmPopUpInfo("Inserting Watermark", watermark) //FrmPopUpInfo("Inserting Watermark", watermark)
for (f in basedir.listFiles()) { for (f in basedir.listFiles().filter { filenameRegex.matches(it.name) }) {
if (filenameRegex.matches(f.name.toString()) && !"jpe?g".toRegex().matches(f.extension)) { if (filenameRegex.matches(f.name.toString()) && !"jpe?g".toRegex().matches(f.extension)) {
val image = ImageIO.read(f) val image = ImageIO.read(f)
val combined = addWM(watermark, image) as RenderedImage val combined = addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
ImageIO.write(combined, "JPG", File(outdir, f.nameWithoutExtension + ".jpg")) ImageIO.write(combined, "JPG", File(outdir, f.nameWithoutExtension + ".jpg"))
} else { } else {
@ -139,7 +142,7 @@ fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
val image = reader.read(0) val image = reader.read(0)
val imageMetadata = reader.getImageMetadata(0) val imageMetadata = reader.getImageMetadata(0)
val combined = addWM(watermark, image) as RenderedImage val combined = addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
val iter = ImageIO.getImageWritersByMIMEType("image/jpeg") val iter = ImageIO.getImageWritersByMIMEType("image/jpeg")
val writer = iter.next() val writer = iter.next()
@ -167,6 +170,18 @@ fun addWM(watermark: BufferedImage, image: BufferedImage): Image {
return combined return combined
} }
fun resize(img: BufferedImage, newW: Double, newH: Double): BufferedImage {
val tmp = img.getScaledInstance(newW.toInt(), newH.toInt(), Image.SCALE_SMOOTH)
val dimg = BufferedImage(newW.toInt(), newH.toInt(), BufferedImage.TYPE_INT_ARGB)
val g2d = dimg.createGraphics()
g2d.drawImage(tmp, 0, 0, null)
g2d.dispose()
return dimg
}
//class FrmPopUpInfo(message: String, image: Image) : JFrame() { //class FrmPopUpInfo(message: String, image: Image) : JFrame() {
// init { // init {
// this.title = message // this.title = message