riku
2022-06-17 3a5c011d9509d3bc0367921f463676c81ff2e374
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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)
    }
}