赞
踩
目录
你知道的 这只是一个简单笔记......
1)将文字和二维码合成为一张图片;
2)将图片保存到文件。
其中,com.google.zxing是一个二维码处理库,com.itextpdf是一个PDF处理库,org.apache.commons.lang3是一个常用的Java工具库。这些库可以通过Maven中央仓库进行下载和引用。
- <dependency>
- <groupId>com.google.zxing</groupId>
- <artifactId>core</artifactId>
- <version>3.4.1</version>
- </dependency>
- <dependency>
- <groupId>com.google.zxing</groupId>
- <artifactId>javase</artifactId>
- <version>3.4.1</version>
- </dependency>
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>itextpdf</artifactId>
- <version>5.5.13</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.12.0</version>
- </dependency>
生成海报代码可以分为两个部分,分别处理文字和二维码。
处理文字
处理文字需要使用iText库,通过创建PDF文档、设置字体、添加文本、设置位置等方式生成一个带有文字的PDF页面,然后将PDF页面转换为图片。
代码如下:
- import com.itextpdf.text.*;
- import com.itextpdf.text.pdf.PdfContentByte;
- import com.itextpdf.text.pdf.PdfWriter;
- import org.apache.commons.lang3.StringUtils;
-
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- public class PosterGenerator {
- private static final String FONT_PATH = "src/main/resources/msyh.ttf";
-
- /**
- * 生成带有文字的海报图片
- *
- * @param text 文字内容
- * @param imageSize 图片大小
- * @param fontName 字体名称
- * @param fontSize 字体大小
- * @return 生成的海报图片
- */
- public static BufferedImage generateTextPoster(String text, int imageSize, String fontName, float fontSize) throws DocumentException, IOException {
- Document document = new Document();
- PdfWriter writer = PdfWriter.getInstance(document, new ByteArrayOutputStream());
- document.open();
- PdfContentByte contentByte = writer.getDirectContent();
- Font font = FontFactory.getFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize);
- Phrase phrase = new Phrase(text, font);
- ColumnText.showTextAligned(contentByte, Element.ALIGN_CENTER, phrase, imageSize / 2, imageSize / 2, 0);
- document.close();
- ByteArrayOutputStream baos = (ByteArrayOutputStream) writer.getDirectContent().getOutputStream();
- byte[] pdfBytes = baos.toByteArray();
- return convertPDFToImage(pdfBytes, imageSize, imageSize);
- }
-
- private static BufferedImage convertPDFToImage(byte[] pdfBytes, int width, int height) throws IOException {
- Image image = Image.getInstance(pdfBytes);
- BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics2D graphics = bufferedImage.createGraphics();
- graphics.setColor(Color.WHITE);
- graphics.fillRect(0, 0, width, height);
- graphics.drawImage(image, 0, 0, width, height, null);
- graphics.dispose();
- return bufferedImage;
- }
- }
代码中,generateTextPoster方法用于生成带有文字的海报图片。首先创建一个PDF文档,设置字体、添加文本、设置位置等,然后将PDF文档转换为图片。convertPDFToImage方法用于将PDF文档转换为BufferedImage类型的图片。
处理二维码
处理二维码需要使用Google的ZXing库,通过创建二维码内容、设置参数等方式生成二维码图片。
代码如下:
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.WriterException;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.QRCodeWriter;
-
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- public class PosterGenerator {
-
- /**
- * 生成带有文字和二维码的海报图片
- *
- * @param text 文字内容
- * @param imageSize 图片大小
- * @param fontName 字体名称
- * @param fontSize 字体大小
- * @param qrCodeString 二维码内容
- * @param qrCodeSize 二维码大小
- * @param qrCodeX 二维码X坐标
- * @param qrCodeY 二维码Y坐标
- * @return 生成的海报图片
- */
- public static BufferedImage generatePoster(String text, int imageSize, String fontName, float fontSize,
- String qrCodeString, int qrCodeSize, int qrCodeX, int qrCodeY) throws Exception {
- BufferedImage textImage = generateTextPoster(text, imageSize, fontName, fontSize);
- BufferedImage qrCodeImage = generateQRCode(qrCodeString, qrCodeSize);
- Graphics2D graphics = textImage.createGraphics();
- graphics.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);
- graphics.dispose();
- return textImage;
- }
-
- /**
- * 生成带有文字的海报图片
- *
- * @param text 文字内容
- * @param imageSize 图片大小
- * @param fontName 字体名称
- * @param fontSize 字体大小
- * @return 生成的海报图片
- */
- public static BufferedImage generateTextPoster(String text, int imageSize, String fontName, float fontSize) throws Exception {
- Document document = new Document();
- PdfWriter writer = PdfWriter.getInstance(document, new ByteArrayOutputStream());
- document.open();
- PdfContentByte contentByte = writer.getDirectContent();
- Font font = FontFactory.getFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize);
- Phrase phrase = new Phrase(text, font);
- ColumnText.showTextAligned(contentByte, Element.ALIGN_CENTER, phrase, imageSize / 2, imageSize / 2, 0);
- document.close();
- ByteArrayOutputStream baos = (ByteArrayOutputStream) writer.getDirectContent().getOutputStream();
- byte[] pdfBytes = baos.toByteArray();
- return convertPDFToImage(pdfBytes, imageSize, imageSize);
- }
-
- /**
- * 生成二维码图片
- *
- * @param qrCodeString 二维码内容
- * @param size 二维码大小
- * @return 生成的二维码图片
- */
- public static BufferedImage generateQRCode(String qrCodeString, int size) throws WriterException, IOException {
- Map<EncodeHintType, Object> hints = new HashMap<>();
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- QRCodeWriter qrCodeWriter = new QRCodeWriter();
- BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeString, BarcodeFormat.QR_CODE, size, size, hints);
- BufferedImage qrCodeImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
- for (int x = 0; x < size; x++) {
- for (int y = 0; y < size; y++) {
- qrCodeImage.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
- }
- }
- return qrCodeImage;
- }
-
- private static BufferedImage convertPDFToImage(byte[] pdfBytes, int width, int height) throws IOException {
- Image image = Image.getInstance(pdfBytes);
- BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics2D graphics = bufferedImage.createGraphics();
- graphics.setColor(Color.WHITE);
- graphics.fillRect(0, 0, width, height);
- graphics.drawImage(image, 0, 0, width, height, null);
- graphics.dispose();
- return bufferedImage;
- }
-
- public static void main(String[] args) throws Exception {
- String text = "这是一段测试文字,用于生成海报";
- String qrCodeString = "https://www.baidu.com/";
- int imageSize = 600;
- int qrCodeSize =
将文字和二维码合成为一张图片,需要将二维码图片放置到文字图片上,并设置二维码的位置和大小。
示例代码如下:
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- public class PosterGenerator {
- private static final String FONT_PATH = "src/main/resources/msyh.ttf";
-
- /**
- * 生成带有文字和二维码的海报图片
- *
- * @param text 文字内容
- * @param imageSize 图片大小
- * @param fontName 字体名称
- * @param fontSize 字体大小
- * @param qrCodeString 二维码内容
- * @param qrCodeSize 二维码大小
- * @param qrCodeX 二维码X坐标
- * @param qrCodeY 二维码Y坐标
- * @return 生成的海报图片
- */
- public static BufferedImage generatePoster(String text, int imageSize, String fontName, float fontSize,
- String qrCodeString, int qrCodeSize, int qrCodeX, int qrCodeY) throws Exception {
- BufferedImage textImage = generateTextPoster(text, imageSize, fontName, fontSize);
- BufferedImage qrCodeImage = generateQRCode(qrCodeString, qrCodeSize);
- Graphics2D graphics = textImage.createGraphics();
- graphics.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);
- graphics.dispose();
- return textImage;
- }
-
- /**
- * 生成带有文字的海报图片
- *
- * @param text 文字内容
- * @param imageSize 图片大小
- * @param fontName 字体名称
- * @param fontSize 字体大小
- * @return 生成的海报图片
- */
- public static BufferedImage generateTextPoster(String text, int imageSize, String fontName, float fontSize) throws Exception {
- Document document = new Document();
- PdfWriter writer = PdfWriter.getInstance(document, new ByteArrayOutputStream());
- document.open();
- PdfContentByte contentByte = writer.getDirectContent();
- Font font = FontFactory.getFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize);
- Phrase phrase = new Phrase(text, font);
- ColumnText.showTextAligned(contentByte, Element.ALIGN_CENTER, phrase, imageSize / 2, imageSize / 2, 0);
- document.close();
- ByteArrayOutputStream baos = (ByteArrayOutputStream) writer.getDirectContent().getOutputStream();
- byte[] pdfBytes = baos.toByteArray();
- return convertPDFToImage(pdfBytes, imageSize, imageSize);
- }
-
- /**
- * 生成二维码图片
- *
- * @param qrCodeString 二维码内容
- * @param size 二维码大小
- * @return 生成的二维码图片
- */
- public static BufferedImage generateQRCode(String qrCodeString, int size) throws WriterException {
- Map<EncodeHintType, Object> hints = new HashMap<>();
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- BitMatrix bitMatrix = new MultiFormatWriter().encode(qrCodeString, BarcodeFormat.QR_CODE, size, size, hints);
- int width = bitMatrix.getWidth();
- int height = bitMatrix.getHeight();
- BufferedImage qrCodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- qrCodeImage.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
- }
- }
- return qrCodeImage;
- }
-
- private static BufferedImage convertPDFToImage(byte[] pdfBytes, int width, int height) throws IOException {
- Image image = Image.getInstance(pdfBytes);
- BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics2D graphics = bufferedImage.createGraphics();
- graphics.setColor(Color.WHITE);
- graphics.fillRect(0, 0, width, height);
- graphics.drawImage(image, 0, 0, width, height, null);
- graphics.dispose();
- return bufferedImage;
- }
-
- public static void main(String[] args) throws Exception {
- String text = "这是一段测试文字,用于生成海报";
- String qrCodeString = "https://www.baidu.com/";
- int imageSize = 600;
- int qrCodeSize = 200;
- int qrCodeX = 200;
- int qrCodeY = 200;
- String fontPath = "src/main/resources/msyh.ttf";
- float fontSize = 30;
-
- BufferedImage posterImage = generatePoster(text, imageSize, fontPath, fontSize,
- qrCodeString, qrCodeSize, qrCodeX, qrCodeY);
-
- File outputFile = new File("poster.png");
- ImageIO.write(posterImage, "png", outputFile);
- }
- }
- public static void main(String[] args) throws Exception {
- String text = "这是一段测试文字,用于生成海报";
- String qrCodeString = "https://www.baidu.com/";
- int imageSize = 600;
- int qrCodeSize = 200;
- int qrCodeX = 200;
- int qrCodeY = 200;
- String fontPath = "src/main/resources/msyh.ttf";
- float fontSize = 30;
-
- BufferedImage posterImage = generatePoster(text, imageSize, fontPath, fontSize,
- qrCodeString, qrCodeSize, qrCodeX, qrCodeY);
-
- File outputFile = new File("poster.png");
- ImageIO.write(posterImage, "png", outputFile);
- }
在主函数中,我们设置了以下参数:
text
:海报中要显示的文字内容。qrCodeString
:生成海报中的二维码内容,这里设置为百度首页。imageSize
:生成的海报图片大小。qrCodeSize
:生成的二维码大小。qrCodeX
、qrCodeY
:二维码在海报中的位置。fontPath
:文字所使用的字体路径。fontSize
:文字大小。最后,我们调用generatePoster
方法生成海报图片,并将其保存到文件中。注意,这里我们将图片保存为png格式,也可以保存为其他格式。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。