绘制图片带二维码

/**
     * 生成图片
     *有报红,自己将包导一下,就可以
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "pic/image", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "生成图片", notes = "生成图片", response = String.class)
    public BaseResult<Object> Image(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BaseResult<Object> br = new BaseResult<>();
        List<HashMap<String, Object>> retList = Lists.newArrayList();
        //获取箱子id、编号、根据自己实际需要更改
        List<Box> paramList = boxDao.findByNamedParamList(null);
        for (Box param : paramList) {
            String id = String.valueOf(param.getId());
            String boxNo = param.getBoxNo();
            //根据箱子id生成二维码图片
            BufferedImage image = QrCodeUtil.createImage(id, 300, 300);
            // 本地生成图片、看效果可以将下面注释打开
//            String name = boxNo + ".png";
//            ImageIO.write(image, "png", new FileOutputStream("D:/" + name));
            BufferedImage bi = new BufferedImage(400, 500, BufferedImage.TYPE_INT_ARGB);//INT精确度达到一定,RGB三原色,高度70,宽度150
            Graphics2D gh = (Graphics2D) bi.getGraphics();
            //设置颜色
            gh.setColor(Color.WHITE);
            gh.fillRect(0, 0, 400, 500);//填充整张图片(其实就是设置背景颜色)
            //透明色
//            gh.getDeviceConfiguration().createCompatibleImage(400, 500, Transparency.TRANSLUCENT);
            gh.setColor(Color.BLACK);
            gh.drawRect(0, 0, 400 - 1, 500 - 1); //画边框
            gh.setFont(new Font(boxNo, Font.PLAIN, 50));
            gh.drawString(boxNo, 60, 90);//向图片上写随机字符串
            //将图片变成流
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(image, "png", os);
            BufferedImage bimg = null;
            bimg = javax.imageio.ImageIO.read(new ByteArrayInputStream(os.toByteArray()));
            if (bimg != null) {
                gh.drawImage(bimg, 50, 150, 300, 300, null);
                gh.dispose();
            }
            gh.setColor(Color.BLACK);//设置背景颜色
            //本地生成图片、看效果可以将下面注释打开
//            String outName = boxNo + "out" + ".png";
//            ImageIO.write(bi, "png", new FileOutputStream("D:/" + outName));
//            ImageIO.write(bi, "png", os);
            // 变成BASE64位返给前端
            Base64.Encoder encoder = Base64.getEncoder();
            String base64Str = encoder.encodeToString(os.toByteArray());
            HashMap<String, Object> dataMap = new HashMap();
            dataMap.put("img", base64Str);
            retList.add(dataMap);
        }
        br.setData(retList);
        return br;
    }
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 java.awt.image.BufferedImage;
import java.util.Hashtable;

/**
 * @Classname QrCodeUtil
 * @Description 二维码工具类
 */
public class QrCodeUtil {
    /**
     * @param content   内容
     * @param imgWidth  二维码宽
     * @param imgHeight 二维码高
     * @MethodName createImage
     * @Description 创建二维码
     */
    public static BufferedImage createImage(String content, int imgWidth, int imgHeight) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, imgWidth, imgHeight, 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);
            }
        }
        return image;
    }
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201221171834942.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NTU1NTE0,size_16,color_FFFFFF,t_70)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值