二维码生成与读取

一、通过Zxing生成与读取:

生成二维码:

   int width=300;
   int height=300;
   String format="png";
   String content="www.link.com";

   HashMap hint=new HashMap();
   hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
   hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
   hint.put(EncodeHintType.MARGIN, 2);

   BitMatrix bitMatrix=new MultiFormatWriter().
   encode(content, BarcodeFormat.QR_CODE, width, height,hint);  
   Path file=new File("D:/img.png").toPath();
   MatrixToImageWriter.writeToPath(bitMatrix, format, file);

注:此处生成扫描是"www.link.com",添加为链接时转到链接

读取二维码:

  MultiFormatReader formatReader=new MultiFormatReader(); 
   File file=new File("D:/img.png");  
   BufferedImage image=ImageIO.read(file); 
   BinaryBitmap binaryBitmap=new BinaryBitmap(
     new HybridBinarizer(new BufferedImageLuminanceSource(image)));


   HashMap hint=new HashMap();
   hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
   Result result=formatReader.decode(binaryBitmap,hint);

   System.out.println(result.toString());
   System.out.println(result.getBarcodeFormat());
   System.out.println(result.getText());

二、QRcode生成二维码:

生成二维码:

Qrcode x=new Qrcode();
x.setQrcodeErrorCorrect('M');
x.setQrcodeEncodeMode('B');
    x.setQrcodeVersion(7);
    String qrData="www.imooc.com";
    int width=67+12*(7-1);
    int height=67+12*(7-1);
    int pixoff=2;

BufferedImage bufferedImage=
new BufferedImage
(300, 300, BufferedImage.TYPE_INT_RGB);  
  Graphics2D gs=bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);

   byte[] d=qrData.getBytes("gb2312");
   if(d.length>0&&d.length<120){
         boolean[][] s=x.calQrcode(d); 
        for(int i=0;i<s.length;i++){
            for(int j=0;j<s.length;j++){
                 if(s[j]){
                  gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
                }
          }
      }
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/qrcode.png"));

读取二维码:

   public class ReadQRcode {
   public static void main(String[] args) throws IOException {
           File file=new File("D:/qrcode.png"); 
           BufferedImage bufferedImage=ImageIO.read(file); 
           QRCodeDecoder codeDecoder=new QRCodeDecoder();
           String  result=
           new String(codeDecoder.decode(
           new QRcodeImage(bufferedImage)),"gb2312");
           System.out.println(result); 
          }
}
class QRcodeImage implements QRCodeImage{
         BufferedImage bufferedImage;
          public QRcodeImage(BufferedImage bufferedImage) {
                   this.bufferedImage=bufferedImage;
          }
  @Override
  public int getHeight() {
   return bufferedImage.getHeight();
  }
  @Override
public int getPixel(int arg0, int arg1) {
           return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
         return bufferedImage.getWidth();
}}

三、jquery.qrcode.min.js生成二维码:

   在jsp页面引入:
             <script type="text/javascript" src="js/jquery.min.js"></script>
                 <script type="text/javascript" 
                 src="js/jquery.qrcode.min.js"></script> 
                           
                  生成二维码:<br>
                  <div id="qrcode"></div> 
                  <script type="text/javascript">
                   jQuery('#qrcode').qrcode("www.imooc.com");
                  </script>
--------------------- 
作者:linkingfei 
来源:CSDN 
原文:https://blog.csdn.net/linkingfei/article/details/83099363 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ZXing一维码/二维码 使用文档 样例: System.IO.Stream stmYiWei = new System.IO.MemoryStream(); BitMatrix byteMatrix = new MultiFormatWriter().encode(sCode, BarcodeFormat.CODE_39, 230, 40); toBitmap(byteMatrix).Save(stmYiWei, ImageFormat.Bmp); Byte[] byteYiWei = new byte[stmYiWei.Length]; stmYiWei.Position = 0; stmYiWei.Read(byteYiWei, 0, (int)stmYiWei.Length); //将图片文件流保存为二进制文件以便保存到数据库中 System.IO.Stream stmErWei = new System.IO.MemoryStream(); IDictionary hints = new Dictionary(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); byteMatrix = new MultiFormatWriter().encode(sTmp, BarcodeFormat.QR_CODE, 200, 200, hints); toBitmap(byteMatrix).Save(stmErWei, ImageFormat.Bmp); Byte[] byteErWei = new byte[stmErWei.Length]; stmErWei.Position = 0; stmErWei.Read(byteErWei, 0, (int)stmErWei.Length); //将图片文件流保存为二进制文件以便保存到数据库中 strSQL = "insert into gdzc_biaoqian( bq_gd_no,bq_yiweima,bq_erweima,bq_us_no) values("; strSQL = strSQL + " @bq_gd_no,@bq_yiweima,@bq_erweima,@bq_us_no)"; SqlCommand commandImage = new SqlCommand(strSQL, connectionImage); commandImage.Parameters.Clear(); commandImage.Parameters.Add("@bq_gd_no", SqlDbType.Int).Value = Convert.ToInt32(sGdzcNo); commandImage.Parameters.Add("@bq_yiweima", SqlDbType.Image).Value = byteYiWei; commandImage.Parameters.Add("@bq_erweima", SqlDbType.Image).Value = byteErWei; commandImage.Parameters.Add("@bq_us_no", SqlDbType.Int).Value = Convert.ToInt32(Session["LoginUserID"]); commandImage.ExecuteNonQuery(); commandImage.Dispose();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值