zoukankan      html  css  js  c++  java
  • Document

    其实在写这个东西的时候,有些细节问题的配置,我就不再详细的写了,只写了相关的主要源代码:

    package com.tz.util;

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;

    import javax.imageio.ImageIO;

    import com.swetake.util.Qrcode;//引进的包,我自己

    /**
    * 生成二维码
    * @author yanlong
    * content 二维码的内容
    * imgPath二维码的路径
    * return void 返回的类型
    *
    */
    public class QrcodeImg {
    //生成一个二维码的方法
    public static void getQrcodeImg(String content,String imgPath){
    //实例化Qrcode 对象
    Qrcode qcQrcode=new Qrcode();
    //编码
    qcQrcode.setQrcodeEncodeMode('B');
    //排错率15%的大小
    qcQrcode.setQrcodeErrorCorrect('M');
    //版本
    qcQrcode.setQrcodeVersion(15);

    int width=235;
    int height=235;
    //花板
    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
    //绘制工具
    Graphics2D gs=image.createGraphics();
    //开始绘制
    //背景色
    gs.setBackground(Color.white);
    //绘制矩形
    gs.clearRect(0, 0, width, height);
    //设置内容的颜色
    gs.setColor(Color.black);
    //开始处理我们的信息
    byte[] codeOut;
    try {
    codeOut =content.getBytes("utf-8");
    //通过byte返回布尔类型的数组。
    boolean[][] code=qcQrcode.calQrcode(codeOut);
    for(int i=0;i<code.length;i++){
    for(int j=0;j<code.length;j++){
    if(code[j][i]){
    //如果为真则涂成黑色
    gs.fillRect(j*3+2, i*3+2, 3, 3);
    }
    }
    }
    //加载图片
    File file=new File("C:/Users/yanlong/Desktop/123456.png");
    Image srcImage=ImageIO.read(file);
    int _width=srcImage.getWidth(null);
    int _heigth=srcImage.getHeight(null);
    gs.drawImage(srcImage,(width-_width)/2,(height-_heigth)/2,_width,_heigth,null);
    //释放资源
    gs.dispose();
    image.flush();
    //保存------,写入指定路径

    ImageIO.write(image, "png", new File(imgPath));
    System.out.println("二维码生成成功");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    //主方法
    public static void main(String[] args){

    getQrcodeImg("虽然我相信付出和收获不一定成正比,但是我更相信天道酬勤!---陈燕龙","C:/Users/yanlong/Desktop/3.png");


    }

    }

    ————————————————————————————————————————————————————————————

    二维码

  • 相关阅读:
    坚持博客
    虚拟机CentOS7.2 1611 Minimal最小化安装后桥接固定ip
    Js 希望某链接只能点击一次
    ThinkPHP3.2 杂记
    Mysql 杂记
    Linux挂载Win共享文件夹 一
    Linux 监测系统资源
    Phpstrom 书签应用
    php默认有最大执行时间
    tp3.2中配置链接多个数据库
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/6934776.html
Copyright © 2011-2022 走看看