首先需要引入该jar
implementation "com.drewnoakes:metadata-extractor:2.14.0"
直接上代码
package com.example.lib
import com.drew.imaging.ImageMetadataReader
import com.drew.metadata.Directory
import sun.java2d.pipe.DrawImage
import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
import javax.imageio.ImageIO
import javax.naming.directory.DirContext
import kotlin.collections.HashMap
/**
* 2023/8/21
* ..╭︿︿︿╮
* {/ . . /}
* .( (oo) ) create by cps
* .︶︶︶
* 为照片添加时间戳
*/
class AddTimeUtil {
fun readFile(dirPath: String) {
val dirFile = File(dirPath)
val fileList = dirFile.listFiles()?.filter {
it.name.endsWith(".png") || it.name.endsWith(".jpg")
}?.toList()
val hashMap = HashMap<String, String>()
fileList?.forEach { file ->
println("===================================================")
println(file.name)
hashMap.clear()
val readMetadata = ImageMetadataReader.readMetadata(file)
readMetadata.directories.forEach { directory: Directory? ->
directory?.let { di ->
di.tags.forEach {
hashMap[it.tagName] = it.description
println("${it.tagName} ${it.description}")
}
}
}
var width: Int = 0
hashMap.entries.firstOrNull { hm ->
hm.key.toLowerCase().contains("width".toLowerCase())
}?.let {
width = it.value.split(" ")[0].toInt()
}
//星期一 八月 21 14:39:51 +08:00 2023
var timeStr: String = hashMap.entries.firstOrNull { hm ->
hm.key.toLowerCase().contains("Modified Date".toLowerCase())
}?.value ?: getTodayStr()
timeStr.split(" ").let {
timeStr = "${it[5]}-${it[1]}-${it[2]} "
}
println("$width $timeStr")
addTimeMark(file, timeStr)
}
}
fun getTodayStr(): String {
return with(Calendar.getInstance()) {
"${get(Calendar.YEAR)}-${get(Calendar.MONTH) + 1}-${get(Calendar.DAY_OF_MONTH)}"
}
}
fun addTimeMark(file: File, contentStr: String) {
val ivFile: BufferedImage = ImageIO.read(FileInputStream(file))
val width = ivFile.getWidth()
val height = ivFile.getHeight()
val bufferedImage = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val graphicsIv = bufferedImage.createGraphics()
with(graphicsIv) {
drawImage(ivFile, 0, 0, width, height, null)
color = Color(250,0,250)
font = Font("宋体", Font.BOLD, 50)
}
val fontSize = getFontSizeByWidth(width, contentStr, graphicsIv)
println("最适合的文本大小$fontSize")
graphicsIv.font = Font("宋体", Font.BOLD, fontSize)
val tvWidth = getContentWidth(contentStr, fontSize, graphicsIv)
val tvHeight = getContentHeight(fontSize, graphicsIv)
graphicsIv.drawString(contentStr, width - tvWidth, height - tvHeight)
val fileOutputStream =
FileOutputStream("${file.parent}/${file.nameWithoutExtension}_back.png")
ImageIO.write(bufferedImage, "png", fileOutputStream)
fileOutputStream.flush()
fileOutputStream.close()
}
fun getFontSizeByWidth(picWidth: Int, contentStr: String, g: Graphics2D): Int {
val fontSize: Int = (100 downTo 0 step 2).first {
getContentWidth(contentStr, it, g) < picWidth / 3
}.run {
this
}
return fontSize
}
fun getContentWidth(contentStr: String, fontSize: Int, g: Graphics2D): Int {
val font = Font("宋体", Font.BOLD, fontSize)
val charsWidth =
g.getFontMetrics(font).charsWidth(contentStr.toCharArray(), 0, contentStr.length)
val height = g.getFontMetrics(font).height
return charsWidth
}
fun getContentHeight(fontSize: Int, g: Graphics2D): Int {
val font = Font("宋体", Font.BOLD, fontSize)
return g.getFontMetrics(font).height
}
}
todo
本代码段,未实现角度旋转的问题。有空再研究。
为啥会有此篇。因为我的R10打印照片没有水印。我觉得,照片作为一种保存记忆的载体,应该有时间来铭刻。
网友评论