需求背景
====
针对常规的新媒体运营渠道,经常要推出一些福利商品,只能通过自媒体的渠道进行购买,因为在当前商城中商品一旦上架,所有的用户都可进行购买,所以需要控制商品购买入口,提供新的商品购买入口。
技术方案
====
针对以上的需求,开发侧和产品侧讨论之后确定使用`扫码购`的方式来实现这一需求。
程序设计
====
* 商品信息添加购买渠道标识
* 因为商品的购买一般是经过商品详情页进行加车的,所以直接提供特殊通道让用户可以跳转到详情页,而正常购买渠道无法进入此详情页,通过商品详情页路径生成商品二维码
* 为了提升品牌形象,在商品二维码中间添加商品logo或者品牌logo
代码实现
====
`代码入口Controller`
外汇经纪商对比https://www.fx61.com/brokerlist
```
@ApiOperation(value = "获取商品二维码", notes = "获取商品二维码", httpMethod = "GET")
@RequestMapping(value = "/qrcode/get", method = RequestMethod.GET)
@ResponseBody
public void getQrCode(Long itemNo, HttpServletResponse response) {
try{
// 定义商品详情路径
String detailUrl = "https://www.xxx.com/details?itemNo=123456";
// 获取二维码中心商品图,此处我设置的是固定的公司logo图,建议可以换成商品的缩略图
// 这个logo图是放在项目的resource目录下的
ClassPathResource resource = new ClassPathResource("logo.png");
InputStream inputStream = resource.getInputStream();
File file = new File("./logo.png");
FileUtils.copyInputStreamToFile(inputStream,file);
// 创建用来接收二维码的图片
String rootPath = "./"+itemNo+".png";
// 用商品详情信息和图片地址生成二维码,350为二维码尺寸
File qrCodeImge = ZxingUtils.getQRCodeImge(detailUrl, 350, rootPath);
// 将二维码图片与logo图片进行叠合
BufferedImage bufferedImage = ZxingUtils.encodeImgLogo(qrCodeImge, file);
ImageIO.write(bufferedImage,"png",qrCodeImge);
// 通过流将图片写回到前端
InputStream in = new FileInputStream(qrCodeImge);
response.setContentType("image/png");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//创建存放文件内容的数组
byte[] buff =new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while((n=in.read(buff))!=-1){
//将字节数组的数据全部写入到输出流中
outputStream.write(buff,0,n);
}
//强制将缓存区的数据进行输出
outputStream.flush();
//关流
outputStream.close();
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
```
`生成二维码工具类`
```javascript
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
/**
* @author huachao
* @生二维码工具类 2020年9月3日
*/
public class ZxingUtils {
private static Log log = LogFactory.getLog(ZxingUtils.class);
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 将内容contents生成长宽均为width的图片,图片路径由imgPath指定
*/
public static File getQRCodeImge(String contents, int width, String imgPath) {
return getQRCodeImge(contents, width, width, imgPath);
}
/**
* 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
*/
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
try {
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
File imageFile = new File(imgPath);
writeToFile(bitMatrix, "png", imageFile);
return imageFile;
} catch (Exception e) {
log.error("create QR code error!", e);
return null;
}
}
/**
* 在已有的二维码图片加上logo图片
*
* @param twodimensioncodeImg 二维码图片文件
* @param logoImg logo图片文件
* @return
*/
public static BufferedImage encodeImgLogo(File twodimensioncodeImg, File logoImg) {
BufferedImage twodimensioncode = null;
try {
if (!twodimensioncodeImg.isFile() || !logoImg.isFile()) {
System.out.println("输入非图片");
return null;
}
//读取二维码图片
twodimensioncode = ImageIO.read(twodimensioncodeImg);
//获取画笔
Graphics2D g = twodimensioncode.createGraphics();
//读取logo图片
BufferedImage logo = ImageIO.read(logoImg);
//透明底的图片
BufferedImage bi2 = new BufferedImage(logo.getWidth(),logo.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
Ellipse2D.Double shape = new Ellipse2D.Double(0,0,logo.getWidth(),logo.getHeight());
Graphics2D g2 = bi2.createGraphics();
g2.setClip(shape);
// 使用 setRenderingHint 设置抗锯齿
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(logo,0,0,null);
//设置颜色
g2.setBackground(Color.green);
g2.dispose();
//设置二维码大小,太大,会覆盖二维码,此处20%
int logoWidth = bi2.getWidth(null) > twodimensioncode.getWidth() * 3 / 10 ? (twodimensioncode.getWidth() * 3 / 10) : logo.getWidth(null);
int logoHeight = bi2.getHeight(null) > twodimensioncode.getHeight() * 3 / 10 ? (twodimensioncode.getHeight() * 3 / 10) : logo.getHeight(null);
// 确定二维码的中心位置坐标,设置logo图片放置的位置
int x = (twodimensioncode.getWidth() - logoWidth) / 2;
int y = (twodimensioncode.getHeight() - logoHeight) / 2;
g.drawOval(x, y, logoWidth, logoHeight);
//开始合并绘制图片
g.drawImage(bi2, x, y, logoWidth, logoHeight, null);
// 此处是划圆形,如果需要方形注释掉即可
g.drawRoundRect(x, y, logoWidth, logoHeight, logoWidth, logoHeight);
g.dispose();
logo.flush();
twodimensioncode.flush();
} catch (Exception e) {
System.out.println("二维码绘制logo失败");
}
return twodimensioncode;
}
}
``` |
|