package cn.flightfeather.supervision.business
|
|
import cn.flightfeather.supervision.common.utils.QRCodeUtil
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook
|
import org.junit.Test
|
import java.awt.Color
|
import java.awt.Font
|
import java.awt.image.BufferedImage
|
import java.io.File
|
import java.io.FileInputStream
|
import javax.imageio.IIOImage
|
import javax.imageio.ImageIO
|
import javax.imageio.ImageTypeSpecifier
|
import javax.imageio.metadata.IIOInvalidTreeException
|
import javax.imageio.metadata.IIOMetadata
|
import javax.imageio.metadata.IIOMetadataNode
|
|
|
/**
|
* @author riku
|
* Date: 2020/8/10
|
*/
|
class QRCodeTest {
|
|
@Test
|
fun export() {
|
val infoList = mutableListOf<Pair<String, String>>()
|
val workbook = HSSFWorkbook(FileInputStream("E:/qrcode/qrcode_info.xls"))
|
val sheet1 = workbook.getSheetAt(0)
|
val lastRow = sheet1.lastRowNum
|
for (i in 0 until lastRow) {
|
val row = sheet1.getRow(i)
|
val cell1 = row.getCell(0).stringCellValue
|
val cell2 = row.getCell(1).stringCellValue
|
infoList.add(Pair(cell1, cell2))
|
|
}
|
|
val baseUrl = "http://47.100.191.150/ledger/page/qrcode.html"
|
infoList.forEach {
|
val url = "$baseUrl?id=${it.first}"
|
val picName = it.second
|
QRCodeUtil.createQRCodeBitmap(url, picName)
|
}
|
}
|
|
@Test
|
fun export2() {
|
val url = "http://47.100.191.150/ledger/page/creditresult.html"
|
val picName = listOf("green", "yellow", "red")
|
picName.forEach {
|
val u = "$url?color=$it"
|
val color = when (it) {
|
"green" -> Color.GREEN
|
"yellow" -> Color.ORANGE
|
"red" -> Color.RED
|
else -> Color.GREEN
|
}
|
QRCodeUtil.createQRCodeBitmap(u, it, color_black = color)
|
}
|
}
|
|
@Test
|
fun export3() {
|
val bgWidth = 1181
|
val bgHeight = 1654
|
val qrCodeWidth = 440
|
|
val infoList = mutableListOf<Triple<String, String, String>>()
|
val workbook = HSSFWorkbook(FileInputStream("E:/qrcode/qrcode_info.xls"))
|
val sheet1 = workbook.getSheetAt(0)
|
val lastRow: Int = sheet1.lastRowNum
|
for (i in 0..lastRow) {
|
val row = sheet1.getRow(i)
|
val cell1 = row.getCell(0).stringCellValue
|
val cell2 = row.getCell(1).stringCellValue
|
val cell3 = row.getCell(2).stringCellValue
|
infoList.add(Triple(cell1, cell2, cell3))
|
}
|
|
val baseUrl = "http://47.100.191.150/ledger/page/qrcode.html"
|
infoList.forEach {
|
val url = "$baseUrl?id=${it.first}"
|
val picName = it.second
|
val qrCode = QRCodeUtil.createQRCodeBitmap2(url, picName, qrCodeWidth, qrCodeWidth)
|
|
val bg = ImageIO.read(FileInputStream("E:/qrcode/qr_code_bg.png"))
|
val source = BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB)
|
|
|
val f1 = Font("思源黑体 CN Medium", Font.PLAIN, 46)
|
|
val graphics2D = source.graphics
|
|
graphics2D.drawImage(bg, 0, 0) { _, _, _, _, _, _ ->
|
true
|
}
|
|
graphics2D.font = f1
|
graphics2D.color = Color.BLACK
|
val w1 = graphics2D.fontMetrics.stringWidth(it.second)
|
val x1 = (bgWidth - w1) / 2
|
graphics2D.drawString(it.second, x1, 160 + graphics2D.fontMetrics.ascent)
|
|
val f2 = Font("思源黑体 CN Medium", Font.PLAIN, 40)
|
graphics2D.font = f2
|
val w2 = graphics2D.fontMetrics.stringWidth(it.third)
|
val x2 = (bgWidth - w2) / 2
|
graphics2D.drawString(it.third, x2, 250 + graphics2D.fontMetrics.ascent)
|
|
|
val x3 = (bgWidth - qrCodeWidth) / 2
|
graphics2D.drawImage(qrCode, x3, 410) { img, infoflags, x, y, width, height ->
|
true
|
}
|
|
val a = ImageIO.getImageWritersByFormatName("png")
|
while (a.hasNext()) {
|
val iw = a.next()
|
val writeParam = iw.defaultWriteParam
|
val typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB)
|
val metadata = iw.getDefaultImageMetadata(typeSpecifier, writeParam)
|
if (metadata.isReadOnly || !metadata.isStandardMetadataFormatSupported) {
|
continue
|
}
|
|
setDPI(metadata)
|
|
val stream = ImageIO.createImageOutputStream(File("E:/qrcode/天钥桥餐饮二维码/${it.second}.png"))
|
stream.use {
|
iw.output = it
|
iw.write(metadata, IIOImage(source, null, metadata), writeParam)
|
}
|
break
|
}
|
|
//水印,工具类hutool
|
// ImageUtil.pressText(source, "文字", Color.white, f1, 240, 595, 1f)
|
|
// ImageIO.write(source, "png", File("E:/qrcode/${it.second}.png"))
|
}
|
}
|
|
@Test
|
fun export4() {
|
val bgWidth = 1181
|
val bgHeight = 1654
|
val qrCodeWidth = 440
|
|
val infoList = mutableListOf<Triple<String, String, String>>()
|
val workbook = HSSFWorkbook(FileInputStream("E:/qrcode/qrcode_info.xls"))
|
val sheet1 = workbook.getSheetAt(0)
|
val lastRow: Int = sheet1.lastRowNum
|
for (i in 0..lastRow) {
|
val row = sheet1.getRow(i)
|
val cell1 = row.getCell(0).stringCellValue
|
val cell2 = row.getCell(1).stringCellValue
|
val cell3 = row.getCell(2).stringCellValue
|
infoList.add(Triple(cell1, cell2, cell3))
|
}
|
|
val baseUrl = "http://47.100.191.150/ledger/page/qrcode.html"
|
infoList.forEach {
|
val url = "$baseUrl?id=${it.first}"
|
val picName = it.second
|
val qrCode = QRCodeUtil.createQRCodeBitmap2(url, picName, qrCodeWidth, qrCodeWidth)
|
|
val bg = ImageIO.read(FileInputStream("E:/qrcode/qr_code_bg.png"))
|
val source = BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB)
|
|
|
val f1 = Font("思源黑体 CN Medium", Font.PLAIN, 40)
|
|
val graphics2D = source.graphics
|
|
graphics2D.drawImage(bg, 0, 0) { _, _, _, _, _, _ ->
|
true
|
}
|
|
graphics2D.font = f1
|
graphics2D.color = Color.BLACK
|
val w1 = graphics2D.fontMetrics.stringWidth(it.second)
|
val x1 = (bgWidth - w1) / 2
|
graphics2D.drawString(it.second, x1, 205 + graphics2D.fontMetrics.ascent)
|
|
// val f2 = Font("思源黑体 CN Medium", Font.PLAIN, 40)
|
// graphics2D.font = f2
|
// val w2 = graphics2D.fontMetrics.stringWidth(it.third)
|
// val x2 = (bgWidth - w2) / 2
|
// graphics2D.drawString(it.third, x2, 250 + graphics2D.fontMetrics.ascent)
|
|
|
val x3 = (bgWidth - qrCodeWidth) / 2
|
graphics2D.drawImage(qrCode, x3, 410) { img, infoflags, x, y, width, height ->
|
true
|
}
|
|
val a = ImageIO.getImageWritersByFormatName("png")
|
while (a.hasNext()) {
|
val iw = a.next()
|
val writeParam = iw.defaultWriteParam
|
val typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB)
|
val metadata = iw.getDefaultImageMetadata(typeSpecifier, writeParam)
|
if (metadata.isReadOnly || !metadata.isStandardMetadataFormatSupported) {
|
continue
|
}
|
|
setDPI(metadata)
|
|
val stream = ImageIO.createImageOutputStream(File("E:/qrcode/天钥桥餐饮二维码/${it.second}.png"))
|
stream.use {
|
iw.output = it
|
iw.write(metadata, IIOImage(source, null, metadata), writeParam)
|
}
|
break
|
}
|
|
//水印,工具类hutool
|
// ImageUtil.pressText(source, "文字", Color.white, f1, 240, 595, 1f)
|
|
// ImageIO.write(source, "png", File("E:/qrcode/${it.second}.png"))
|
}
|
}
|
|
@Throws(IIOInvalidTreeException::class)
|
private fun setDPI(metadata: IIOMetadata) {
|
|
// for PMG, it's dots per millimeter
|
val dotsPerMilli = 1.0 * 300 / 10 / 2.54
|
|
val horiz = IIOMetadataNode("HorizontalPixelSize")
|
horiz.setAttribute("value", java.lang.Double.toString(dotsPerMilli))
|
|
val vert = IIOMetadataNode("VerticalPixelSize")
|
vert.setAttribute("value", java.lang.Double.toString(dotsPerMilli))
|
|
val dim = IIOMetadataNode("Dimension")
|
dim.appendChild(horiz)
|
dim.appendChild(vert)
|
|
val root = IIOMetadataNode("javax_imageio_1.0")
|
root.appendChild(dim)
|
|
metadata.mergeTree("javax_imageio_1.0", root)
|
}
|
}
|