Add progress bar and multithreading

This commit is contained in:
Julius de Jeu 2019-01-22 12:16:58 +01:00
parent d5edabddec
commit 67193f8869
2 changed files with 80 additions and 32 deletions

3
.gitignore vendored
View file

@ -131,4 +131,5 @@ gradle-app.setting
input/ input/
output/ output/
watermerk.png /*.png
/*.jp*g

View file

@ -10,12 +10,15 @@ 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.concurrent.atomic.AtomicInteger
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.fixedRateTimer
import kotlin.concurrent.thread
@Language("RegExp") @Language("RegExp")
@ -23,6 +26,13 @@ val filenameRegex = ".*\\.(png|jpe?g|bmp)".toRegex()
val jf = JFrame("WaterMarker") val jf = JFrame("WaterMarker")
val counter = AtomicInteger()
val runbtn = JButton("Run!")
val pbar = JProgressBar()
val dlg = JDialog(jf, "Progress Dialog", true)
fun main(args: Array<String>) { fun main(args: Array<String>) {
var watermark: File? = null var watermark: File? = null
@ -95,11 +105,13 @@ fun main(args: Array<String>) {
}.forceToCenter() }.forceToCenter()
) )
box.add(wms.forceToCenter()) box.add(wms.forceToCenter())
runbtn.apply {
box.add(JButton("Run!").apply {
addActionListener { addActionListener {
if (watermark != null && outdir != null && basedir != null) { if (watermark != null && outdir != null && basedir != null) {
doTheThing(basedir!!, outdir!!, watermark!!) this.isEnabled = false
thread {
doTheThing(basedir!!, outdir!!, watermark!!)
}
} else { } else {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
jf, jf,
@ -109,7 +121,8 @@ fun main(args: Array<String>) {
) )
} }
} }
}.forceToCenter()) }
box.add(runbtn.forceToCenter())
jf.add(box, BorderLayout.CENTER) jf.add(box, BorderLayout.CENTER)
@ -118,6 +131,11 @@ fun main(args: Array<String>) {
jf.preferredSize = Dimension(300, 300) jf.preferredSize = Dimension(300, 300)
jf.size = jf.preferredSize jf.size = jf.preferredSize
jf.isVisible = true jf.isVisible = true
dlg.add(BorderLayout.CENTER, pbar)
dlg.defaultCloseOperation = JDialog.DO_NOTHING_ON_CLOSE
dlg.setSize(300, 75)
dlg.setLocationRelativeTo(jf)
} }
fun doTheThing(basedir: File, outdir: File, watermarkpath: File) { fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
@ -126,36 +144,65 @@ fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
val scale = 3.6 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().filter { filenameRegex.matches(it.name) }) { val files = basedir.listFiles().filter { filenameRegex.matches(it.name) }.chunked(5)
if (filenameRegex.matches(f.name.toString()) && !"jpe?g".toRegex().matches(f.extension)) { val propersize = files.sumBy { it.size }
val image = ImageIO.read(f) counter.set(0)
SwingUtilities.invokeLater {
pbar.value = 0
pbar.maximum = propersize
dlg.isVisible = true
}
val combined = addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage fixedRateTimer("theWait", period = 500, initialDelay = 0) {
if (counter.get() == propersize) {
ImageIO.write(combined, "JPG", File(outdir, f.nameWithoutExtension + ".jpg")) SwingUtilities.invokeLater {
} else { dlg.isVisible = false
val `is` = BufferedInputStream(ByteArrayInputStream(f.readBytes())) runbtn.isEnabled = true
val it = ImageIO.getImageReadersByMIMEType("image/jpeg") this.cancel()
val reader = it.next() }
val iis = ImageIO.createImageInputStream(`is`)
reader.setInput(iis, false, false)
val image = reader.read(0)
val imageMetadata = reader.getImageMetadata(0)
val combined = addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
val iter = ImageIO.getImageWritersByMIMEType("image/jpeg")
val writer = iter.next()
val iwp = writer.defaultWriteParam
iwp.compressionMode = ImageWriteParam.MODE_EXPLICIT
iwp.compressionQuality = 0.9f
val imgOut = MemoryCacheImageOutputStream(File(outdir, f.nameWithoutExtension + ".jpg").outputStream())
writer.output = imgOut
val ending = IIOImage(combined, null, imageMetadata)
writer.write(null, ending, iwp)
writer.dispose()
} }
} }
for (fl in files) {
thread {
for (f in fl) {
if (filenameRegex.matches(f.name.toString()) && !"jpe?g".toRegex().matches(f.extension)) {
val image = ImageIO.read(f)
val combined =
addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
ImageIO.write(combined, "JPG", File(outdir, f.nameWithoutExtension + ".jpg"))
} else {
val `is` = BufferedInputStream(ByteArrayInputStream(f.readBytes()))
val it = ImageIO.getImageReadersByMIMEType("image/jpeg")
val reader = it.next()
val iis = ImageIO.createImageInputStream(`is`)
reader.setInput(iis, false, false)
val image = reader.read(0)
val imageMetadata = reader.getImageMetadata(0)
val combined =
addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
val iter = ImageIO.getImageWritersByMIMEType("image/jpeg")
val writer = iter.next()
val iwp = writer.defaultWriteParam
iwp.compressionMode = ImageWriteParam.MODE_EXPLICIT
iwp.compressionQuality = 0.9f
val imgOut = MemoryCacheImageOutputStream(File(outdir, f.nameWithoutExtension + ".jpg").outputStream())
writer.output = imgOut
val ending = IIOImage(combined, null, imageMetadata)
writer.write(null, ending, iwp)
writer.dispose()
}
SwingUtilities.invokeLater {
pbar.value = counter.incrementAndGet()
}
}
}
}
} }
fun Component.forceToCenter(): Component = JPanel().apply { this.add(this@forceToCenter) } fun Component.forceToCenter(): Component = JPanel().apply { this.add(this@forceToCenter) }