1、将oss的url转成BufferedImage
//oss的url转成BufferedImage工具类
package com.cxtplatform.promotion.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class OssUrlImgInputStreamTransfer {
public static ByteArrayInputStream ossUrlTransferStream(String ossUrl) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(ossUrl).openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
InputStream inStream = connection.getInputStream();
//下面要把inStream变成二进制
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
byte[] base64Str= outStream.toByteArray();//变成了二进制数组
return new ByteArrayInputStream(base64Str);
}
}
2、制作二维码工具
package com.cxtplatform.promotion.utils;
/**
* @program: universities
* @description: ${description}
* @author: qtian
* @create: 2024-02-28 10:00
**/
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
/**
* 二维码工具类
*
*/
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
public static BufferedImage createImage(String content, Image logoImage,
boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
if (ObjectUtils.isEmpty(logoImage)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, logoImage, needCompress);
return image;
}
/**
* 插入LOGO
*
* @param source
* 二维码图片
* @param logoImage
* LOGO图片
* @param needCompress
* 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, Image logoImage,
boolean needCompress) throws Exception {
int width = logoImage.getWidth(null);
int height = logoImage.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = logoImage.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
logoImage = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(logoImage, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 解析二维码
*
* @param file
* 二维码图片
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
}
3、根据网址及oss图片logo制作二维码
//获取logo的oss地址
String logoMedia = liveCode.getLogoMedia();
ByteArrayInputStream in = OssUrlImgInputStreamTransfer.ossUrlTransferStream(logoMedia);
BufferedImage image = ImageIO.read(in); //将in作为输入流,读取图片存入image中,
// 将 BufferedImage 转换为 Image 对象
Image image1 = Toolkit.getDefaultToolkit().createImage(image.getSource());
BufferedImage bufferedImage = QRCodeUtil.createImage(qrCodeUrl, image1, true);