java程序生成二维码

来源:互联网 发布:linux黑客系统 编辑:程序博客网 时间:2024/06/10 16:12
<strong>1)JS生成二维码</strong>利用jquery.qrcode生成二维码在页面中导入jquery.js和jquery.qrcode.js<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.qrcode.min.js"></script>$("#code").qrcode({ render :"table",  //支持canvas(HTML5)和table两种方式 width  : 200,             //二维码宽度 height : 200,             //二维码高度 text: "www.baidu.com"     //二维码内容 }); 如果要识别中文需要进行编码由UTF-16转换为UTF-8,  function toUtf8(from) {   var to , i, len, char;       to = "";         len = from.length;         for(i = 0; i < len; i++) {        char = from.charCodeAt(i);            if ((char >= 0x0001) && (char <= 0x007F)) {                 to += from.charAt(i);            } else if (char > 0x07FF) {                 to += String.fromCharCode(0xE0 | ((char >> 12) & 0x0F));               to += String.fromCharCode(0x80 | ((char >>  6) & 0x3F));                to += String.fromCharCode(0x80 | ((char >>  0) & 0x3F));            } else {                   to += String.fromCharCode(0xC0 | ((char >>  6) & 0x1F));                to += String.fromCharCode(0x80 | ((char >>  0) & 0x3F));             }           }           return to;     }var str = toUtf8("js生成二维码"); 

<strong>2)java生成二维码</strong>用Google条码工具Zxing(可生成和解析条形码、二维码)导入Zxing-core.jar 到工程import com.google.zxing.common.BitMatrix;import javax.imageio.ImageIO;import java.io.File;import java.io.OutputStream;import java.io.IOException;import java.awt.image.BufferedImage;public final class QrCode {  private static final int BLACK = 0xFF000000;  private static final int WHITE = 0xFFFFFFFF;  private QrCode() {}//生成二维码    public 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;  }//将二维码写到文件  public 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);    }  }//将二维码写到输出流  public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)      throws IOException {    BufferedImage image = toBufferedImage(matrix);    if (!ImageIO.write(image, format, stream)) {      throw new IOException("Could not write an image of format " + format);    }  }}Public class QrCodeTest{  Public static void main(String[] args){    try {                  String content = "http://www.baidu.com ";  //二维码内容      String path = "E:/image";  //生成二维码保存路径      MultiFormatWriter multiFormatWriter = new MultiFormatWriter();      Map hints = new HashMap();      hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");      BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);      File file1 = new File(path,"二维码.jpg");      QrCode.writeToFile(bitMatrix, "jpg", file1);    } catch (Exception e) {      e.printStackTrace();    }  }}

0 0