需要背景

针对惯例的新媒体经营渠道,常常要推出一些福利商品,只能通过自媒体的渠道进行购买,因为在以后商城中商品一旦上架,所有的用户都可进行购买,所以须要管制商品购买入口,提供新的商品购买入口。

技术计划

针对以上的需要,开发侧和产品侧探讨之后确定应用扫码购的形式来实现这一需要。

程序设计

  • 商品信息增加购买渠道标识
  • 因为商品的购买个别是通过商品详情页进行加车的,所以间接提供非凡通道让用户能够跳转到详情页,而失常购买渠道无奈进入此详情页,通过商品详情页门路生成商品二维码
  • 为了晋升品牌形象,在商品二维码两头增加商品logo或者品牌logo

代码实现

外汇经纪商动静https://www.fx61.com/news

代码入口Controller

 @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();        }    }

生成二维码工具类

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;    }}