Add progress bar and multithreading
This commit is contained in:
parent
d5edabddec
commit
67193f8869
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -131,4 +131,5 @@ gradle-app.setting
|
|||
|
||||
input/
|
||||
output/
|
||||
watermerk.png
|
||||
/*.png
|
||||
/*.jp*g
|
|
@ -10,12 +10,15 @@ import java.awt.image.RenderedImage
|
|||
import java.io.BufferedInputStream
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.File
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import javax.imageio.IIOImage
|
||||
import javax.imageio.ImageIO
|
||||
import javax.imageio.ImageWriteParam
|
||||
import javax.imageio.stream.MemoryCacheImageOutputStream
|
||||
import javax.swing.*
|
||||
import javax.swing.filechooser.FileFilter
|
||||
import kotlin.concurrent.fixedRateTimer
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
|
||||
@Language("RegExp")
|
||||
|
@ -23,6 +26,13 @@ val filenameRegex = ".*\\.(png|jpe?g|bmp)".toRegex()
|
|||
|
||||
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>) {
|
||||
var watermark: File? = null
|
||||
|
@ -95,11 +105,13 @@ fun main(args: Array<String>) {
|
|||
}.forceToCenter()
|
||||
)
|
||||
box.add(wms.forceToCenter())
|
||||
|
||||
box.add(JButton("Run!").apply {
|
||||
runbtn.apply {
|
||||
addActionListener {
|
||||
if (watermark != null && outdir != null && basedir != null) {
|
||||
this.isEnabled = false
|
||||
thread {
|
||||
doTheThing(basedir!!, outdir!!, watermark!!)
|
||||
}
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(
|
||||
jf,
|
||||
|
@ -109,7 +121,8 @@ fun main(args: Array<String>) {
|
|||
)
|
||||
}
|
||||
}
|
||||
}.forceToCenter())
|
||||
}
|
||||
box.add(runbtn.forceToCenter())
|
||||
|
||||
jf.add(box, BorderLayout.CENTER)
|
||||
|
||||
|
@ -118,6 +131,11 @@ fun main(args: Array<String>) {
|
|||
jf.preferredSize = Dimension(300, 300)
|
||||
jf.size = jf.preferredSize
|
||||
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) {
|
||||
|
@ -126,11 +144,33 @@ fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
|
|||
val scale = 3.6
|
||||
//JOptionPane.showMessageDialog(jf, "", "Inserting watermark!", JOptionPane.INFORMATION_MESSAGE, ImageIcon(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)
|
||||
val propersize = files.sumBy { it.size }
|
||||
counter.set(0)
|
||||
SwingUtilities.invokeLater {
|
||||
pbar.value = 0
|
||||
pbar.maximum = propersize
|
||||
dlg.isVisible = true
|
||||
}
|
||||
|
||||
fixedRateTimer("theWait", period = 500, initialDelay = 0) {
|
||||
if (counter.get() == propersize) {
|
||||
SwingUtilities.invokeLater {
|
||||
dlg.isVisible = false
|
||||
runbtn.isEnabled = true
|
||||
this.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
val combined =
|
||||
addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
|
||||
|
||||
ImageIO.write(combined, "JPG", File(outdir, f.nameWithoutExtension + ".jpg"))
|
||||
} else {
|
||||
|
@ -142,7 +182,8 @@ fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
|
|||
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 combined =
|
||||
addWM(resize(wmbase, wmbase.width / scale, wmbase.height / scale), image) as RenderedImage
|
||||
|
||||
val iter = ImageIO.getImageWritersByMIMEType("image/jpeg")
|
||||
val writer = iter.next()
|
||||
|
@ -155,7 +196,13 @@ fun doTheThing(basedir: File, outdir: File, watermarkpath: File) {
|
|||
writer.write(null, ending, iwp)
|
||||
writer.dispose()
|
||||
}
|
||||
SwingUtilities.invokeLater {
|
||||
pbar.value = counter.incrementAndGet()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun Component.forceToCenter(): Component = JPanel().apply { this.add(this@forceToCenter) }
|
||||
|
|
Loading…
Reference in a new issue