JAVA实现基于ZXing的二维码自动生成与图片合成

JAVA实现基于ZXing的二维码自动生成与图片合成

近日做项目需要生成带有信息的二维码,并嵌入到一张图片中。实现思路采用Zxing生成二维码,java图形库进行图片的嵌入。

生成二维码

ZXing是一种开放源代码的多格式1D / 2D条形码图像处理库,采用Java实现,具有其他语言的端口。需用mavan下载对应jar包,Android添加gradle依赖,或者添加jar包。

代码如下:

     private static BufferedImage createImage(String content, String logoImgPath, boolean needCompress) throws WriterException, IOException {
        Hashtable hints = new Hashtable();
        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);
            }
        }
        // 没有logo
        if (logoImgPath == null || "".equals(logoImgPath)) {
            return image;
        }

        // 插入图片
        logoInsertIntoImage(image, logoImgPath, needCompress);
        return image;
     }
private static void insertImage(BufferedImage source, String logoImgPath, boolean needCompress) throws IOException {
        File file = new File(logoImgPath);
        if (!file.exists()) {
            return;
        }

        Image src = ImageIO.read(new File(logoImgPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        //处理logo
        if (needCompress) {
            if (width > WIDTH) {
                width = WIDTH;
            }

            if (height > HEIGHT) {
                height = HEIGHT;
            }

            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics gMaker = tag.getGraphics();
            gMaker.drawImage(image, 0, 0, null); // 绘制缩小后的图
            gMaker.dispose();
            src = image;
        }

        // 在中心位置插入logo
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, 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();
    }

生成图片保存到本地如图,考虑到实际业务,这里没有插入logo

将二维码嵌入图片

生成二维码后,需要将二维码嵌入另一张图片中,实现比较简单,只需要在背景图片上画上已经生成的二维码即可 代码如下:

    public static InputStream changeMerchantSeatQrcodeImage(BufferedImage zxingImage, String backgroundPath) {
        InputStream imagein = null;
        ImageOutputStream imOut = null;
        try {
            imagein = new FileInputStream(backgroundPath);
            BufferedImage image = ImageIO.read(imagein);
            BufferedImage image2 = zxingImage;
            Graphics g = image.getGraphics();
            // 生成的二维码设置的较小,这里等比放大了二维码。也可在zxing中设置二维码生成的大小
            BufferedImage squreImage = resizeImage(image2, 2);
            g.drawImage(squreImage, 40, 25,
                    squreImage.getWidth(), squreImage.getHeight(), null);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(image, "jpg", imOut);
            InputStream is = new ByteArrayInputStream(bs.toByteArray());
            return is;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                imagein.close();
                imOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

放大图片方法:

    public static BufferedImage resizeImage(BufferedImage  originalImage, double times){
        int width = (int)(originalImage.getWidth()*times);
        int height = (int)(originalImage.getHeight()*times);

        int tType = originalImage.getType();
        if(0 == tType){
            tType = 5;
        }
        BufferedImage newImage = new BufferedImage(width,height, tType);
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0,0,width,height,null);
        g.dispose();

        return newImage;
    }

至此成功获得输入流,写入本地即可。此方法可用于批量生成管理的二维码。你可以查看www.zhaochengquan.com获取更多内容。

你可能感兴趣的:(java)