由于需求需要的是PDF的base64,所以中转了一下,比价麻烦了,代码如下:
/**
* Description: 将图片的base64转成PDF的base64编码
* @param qrCodeUrl 需要转成二维码的URL字符串
* @Author zhanglifeng
* Create Date: 2020年3月30日 下午8:25:37
*/
public String imageBase64ToPdfBase64(String qrCodeUrl,String custOrderNbr){
String bas64ImageStr="";//图片的base64编码
String pdfBase64 = "";//PDF的base64编码
try{
//1.根据微厅返回的请求参数生成二维码 base64 编码
bas64ImageStr = QrcodeUtils.encoderQRCodeToBase64(qrCodeUrl, "png", 10);
bas64ImageStr = bas64ImageStr.replaceAll("\r|\n", "");
//2.获取要存储文件的路径,即获取src资源文件编译后的路径(即classes路径)
String url = this.getClass().getClassLoader().getResource("").getPath();
//对路劲进行拼接添加
String serviceImgPath = url + "serviceImg/";//服务器上存放二维码图片的路径
String servicePdfPath = url + "servicePdf/";//服务器上存放二维码PDF的路径
//3.判断服务器是否存在此文件夹,不存在则新建
File file1 =new File(serviceImgPath);
File file2 =new File(servicePdfPath);
if(!file1.exists() && !file1.isDirectory()) {
file1.mkdir();
}
if(!file2.exists() && !file2.isDirectory()) {
file2.mkdir();
}
//4.二维码图片的文件名字和最终保存的二维码文件路径
String fileImageName = custOrderNbr+"_phone.png";//二维码图片路径名字
String filePdfName = custOrderNbr+"_phone.pdf";//PDF图片路径名字
String lastImagePath = serviceImgPath+fileImageName;//最终二维码图片存放的路径
String lastPdfPath = servicePdfPath+filePdfName;//最终二维码PDF存放的路径
//5.首先保存二维码图片
Base64ToImage(bas64ImageStr,lastImagePath);
//6.然后把二维码图片转成PDF二维码文件进行储存
ImagePdf.image2pdf(lastImagePath,lastPdfPath);
//7.最后将PDF转成base64,PDF的base64才是最终能推送到签字版的
File file3 =new File(lastPdfPath);
pdfBase64 = PDFToBase64(file3);
pdfBase64 = pdfBase64.replaceAll("\r|\n", "");
//8.需要删除创建的临时文件
File imagefile = new File(lastImagePath);
if(imagefile.exists()){
imagefile.delete();
}
File pdffile = new File(lastPdfPath);
if(pdffile.exists()){
pdffile.delete();
}
}catch (Exception e){
e.printStackTrace();
}
return pdfBase64;
}
下面是直接转成图片的base64代码:
/**
* Description: 将图片的base64字符串,直接供前台展示
* @param qrCodeUrl 需要转成二维码的URL
* @Author zhanglifeng
* Create Date: 2020年3月30日 下午8:25:37
*/
public String imageBase64ToPdfBase64(String qrCodeUrl,String custOrderNbr){
String bas64ImageStr="";//图片的base64编码
try{
//1.根据微厅返回的请求参数生成二维码 base64 编码
bas64ImageStr = QrcodeUtils.encoderQRCodeToBase64(qrCodeUrl, "png", 10);
bas64ImageStr = bas64ImageStr.replaceAll("\r|\n", "");
}catch (Exception e){
e.printStackTrace();
}
return bas64ImageStr;
}
下面是二维码工具类:
/**
* @Date: 2020/3/27 17:38
* @Author: Lifeng.Zhang
* @Description: 二维码处理工具类
*/
public class QrcodeUtils {
/**
* 根据参数生成二维码base64编码
* 容错率默认M,二维码内容支持大小为10
* @param content 存储内容
* @param imgType 图片类型
* @param imgSize 二维码图片大小像素
* @param qrWidth 二维码 像素宽度 此值的60倍为图片大小 即:qrWidth*60(约等于) <=imgSize(需要微调)
* @return String
*/
public static String encoderQRCodeToBase64(String content, String imgType, int imgSize,int qrWidth) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
BufferedImage image = encoderQRCodeFixSize(content, imgType, imgSize, qrWidth);
if (image == null) {
return "";
}
ImageIO.write(image, imgType, baos);
return Base64.encodeBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* 生成二维码(QRCode)图片的公共方法
* 生成后的效果需要微调 imgSize 及qrWidth来达到最佳图片效果
* 如:imgSize=244时,qrWidth=4
*
* @param content 存储内容
* @param imgType 图片类型
* @param imgSize 二维码图片大小像素
* @param qrWidth 二维码 像素宽度 此值的60倍为图片大小 即:qrWidth*60(约等于) <=imgSize(需要微调)
* @return BufferedImage
*/
public static BufferedImage encoderQRCodeFixSize(String content, String imgType, int imgSize, int qrWidth) {
BufferedImage bufImg = null;
try {
Qrcode qrcodeHandler = new Qrcode();
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
//以字符串存
qrcodeHandler.setQrcodeEncodeMode('B');
// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
qrcodeHandler.setQrcodeVersion(6);
// 获得内容的字节数组,设置编码格式
byte[] contentBytes = content.getBytes("UTF-8");
// 图片尺寸
bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 设置背景颜色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
// 设置偏移量,不设置可能导致解析出错
int pixoff = 7;
// 输出内容> 二维码
if (contentBytes.length > 0 && contentBytes.length < 2048) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * qrWidth + pixoff, i * qrWidth + pixoff, qrWidth, qrWidth);
}
}
}
} else {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
}
gs.dispose();
bufImg.flush();
} catch (Exception e) {
e.printStackTrace();
}
return bufImg;
}
/**
* 根据参数生成二维码base64编码
*
* @param content
* @param imgType
* @param size
* @return
* @see
*/
public static String encoderQRCodeToBase64(String content, String imgType, int size) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
BufferedImage image = encoderQRCode(content, imgType, size, 10);
if (image == null) {
return "";
}
ImageIO.write(image, imgType, baos);
return Base64.encodeBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* 生成二维码(QRCode)图片
*
* @param content 存储内容
* @param imgType 图片类型
* @param size 二维码尺寸
*/
public static BufferedImage encoderQRCode(String content, String imgType, int size, int margin) {
try {
BufferedImage bufImg = encoderQRCodeCommon(content, imgType, size);
int newWidth = bufImg.getWidth() + margin;
int newHeight = bufImg.getHeight() + margin;
BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = newImage.createGraphics();
gs.setColor(Color.white);
gs.fillRect(0, 0, newWidth, newHeight);
gs.drawImage(bufImg, margin / 2, margin / 2, null);
return newImage;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 生成二维码(QRCode)图片的公共方法
*
* @param content 存储内容
* @param imgType 图片类型
* @param size 二维码尺寸 0到40
* @return BufferedImage
*/
private static BufferedImage encoderQRCodeCommon(String content, String imgType, int size) {
BufferedImage bufImg = null;
try {
Qrcode qrcodeHandler = new Qrcode();
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('L');
//以字符串存
qrcodeHandler.setQrcodeEncodeMode('B');
// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
qrcodeHandler.setQrcodeVersion(6);
// 获得内容的字节数组,设置编码格式
byte[] contentBytes = content.getBytes("utf-8");
// 图片尺寸
int imgSize = 67 + 12 * (size - 1);
bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
// 设置背景颜色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize, imgSize);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
// 设置偏移量,不设置可能导致解析出错
int pixoff = 5;
// 输出内容> 二维码
if (contentBytes.length > 0 && contentBytes.length < 2000) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 4 + pixoff, i * 4 + pixoff, 4, 4);
}
}
}
} else {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
}
gs.dispose();
bufImg.flush();
} catch (Exception e) {
e.printStackTrace();
}
return bufImg;
}
}
本文介绍了一种将图片的Base64编码转换为PDF的Base64编码的方法,通过生成二维码图片,将其转换为PDF并编码,适用于需要在不同格式间转换的应用场景。
6478

被折叠的 条评论
为什么被折叠?



