Created
April 16, 2024 05:12
-
-
Save laifugroup/db328a12188fa18de496ffb1530a4067 to your computer and use it in GitHub Desktop.
打印尺寸和电子尺寸转换关系
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bbbang.piece.api.piece | |
import com.xiaoleilu.hutool.util.ImageUtil | |
import io.micronaut.http.MediaType | |
import io.micronaut.http.server.types.files.StreamedFile | |
import net.glxn.qrgen.core.image.ImageType | |
import net.glxn.qrgen.javase.QRCode | |
import java.awt.AlphaComposite | |
import java.awt.Color | |
import java.awt.Font | |
import java.awt.RenderingHints | |
import java.awt.image.BufferedImage | |
import java.awt.image.RenderedImage | |
import java.io.File | |
import javax.imageio.ImageIO | |
/** | |
* 测试图片生成 | |
*/ | |
fun main(args: Array<String>) { | |
val outFilePath="C:\\Users\\bbbang\\Downloads\\out.png" | |
val outFile= File(outFilePath) | |
// 打印输出分辨率 | |
val dpi=300f//java default 72ppi | |
//打印输出尺寸3X4cm | |
val xCm=3// 3cm | |
val yCm=4// 4cm | |
//计算出来输出尺寸 354px, 471px | |
val width = (xCm/2.54 * dpi).toInt() | |
val height =(yCm/2.54 * dpi).toInt() | |
val color = Color.BLACK | |
val qrcodeOnePieceImage = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) | |
val graphics2D = qrcodeOnePieceImage.createGraphics() | |
//g.setRenderingHint(RenderingHints.KEY_RESOLUTION_VARIANT,dotsPerInch) | |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) | |
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_LCD_CONTRAST, 100) | |
//消除文字锯齿 | |
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON) | |
//消除画图锯齿 | |
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) | |
//画浅色背景 | |
graphics2D.color = Color(170, 233, 233) // 设置背景为浅色 | |
graphics2D.fillRect(0, 0, width, height) // 填充矩形区域 | |
//小程序名称 水印 | |
val title="乌鸦标题" | |
val titleFontSize = 24 | |
val titleFont= Font("微软雅黑", Font.BOLD, titleFontSize) | |
val titleWidth = graphics2D.getFontMetrics(titleFont).stringWidth(title) | |
val titleX=(width - titleWidth) / 2 | |
val titleY: Int = titleFontSize+20 | |
graphics2D.color = color | |
graphics2D.font =titleFont | |
graphics2D.composite = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f) | |
// 在指定坐标绘制水印文字 | |
graphics2D.drawString(title, titleX,titleY) | |
//提醒扫码文字水印 | |
val tips="微信扫码 开始探索" | |
val tipsFontSize = 14 | |
val tipsFont= Font("微软雅黑", Font.PLAIN, tipsFontSize) | |
val tipsWidth = graphics2D.getFontMetrics(tipsFont).stringWidth(tips) | |
val tipsX: Int = (width -tipsWidth) / 2 | |
val tipsY: Int = tipsFontSize+8 | |
graphics2D.font =tipsFont | |
graphics2D.drawString(tips,tipsX,titleY+tipsY) | |
//二维码 水印 | |
val fileName="qrcode.png" | |
val file: File = QRCode.from("https://qrcode.abc.com/html/111111").to(ImageType.PNG).withSize(125, 125).file() | |
val streamFile= StreamedFile(file.inputStream(), MediaType.APPLICATION_OCTET_STREAM_TYPE).apply { | |
attach(fileName) | |
} | |
val qrCodeImg = ImageIO.read(streamFile.inputStream) | |
val qrCodeX=(width-qrCodeImg.width)/2 | |
val qrCodeY=tipsY+titleY+8 | |
graphics2D.drawImage(qrCodeImg, qrCodeX, qrCodeY, qrCodeImg.width, qrCodeImg.height, null) | |
//编号水印 编号再最下面 | |
val no="编号: 010123456" | |
val noFontSize = 14 | |
val noY=height-8 | |
val noFont= Font("微软雅黑", Font.PLAIN, noFontSize) | |
val noWidth = graphics2D.getFontMetrics(noFont).stringWidth(no) | |
val noX: Int = (width - noWidth) / 2 | |
graphics2D.font =noFont | |
graphics2D.drawString(no,noX,noY) | |
//口号水印 再编号水印上面 | |
val slogan="做最好的小米汽车参观" | |
val sloganFontSize = 14 | |
val sloganFont= Font("微软雅黑", Font.PLAIN, sloganFontSize) | |
val sloganWidth = graphics2D.getFontMetrics(sloganFont).stringWidth(slogan) | |
val sloganX: Int = (width - sloganWidth) / 2 | |
graphics2D.font =sloganFont | |
graphics2D.drawString(slogan,sloganX,height-noFontSize-16) | |
graphics2D.dispose() | |
ImageIO.write(qrcodeOnePieceImage as RenderedImage, ImageUtil.IMAGE_TYPE_PNG, outFile) // 输出到文件流 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment