生成二维码(内嵌logo)条形码大全[附源码下载]
程序员文章站
2022-07-14 18:25:48
...
本例配套源码下载:https://download.csdn.net/download/djk8888/10505344
用到了:jquery.qrcode.js(实现简单,推荐此法)、ThoughtWorks.QRCode.dll等
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>生成二维码</title>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/jquery.qrcode.js"></script>
<script type="text/javascript" src="js/qrcode.js"></script>
<script type="text/javascript" src="js/utf.js"></script>
</head>
<body>
<table border="1">
<tr>
<td>
<p>一行代码写法:</p>
<div id="qrcode"></div>
<script type="text/javascript">
$('#qrcode').qrcode(encodeURI("https://blog.csdn.net/djk8888"));//中文参数需要encode转码(下同)
</script>
</td>
<td>
<p>标准写法:</p>
<div id="qrcodeTable"></div>
<script type="text/javascript">
$('#qrcodeTable').qrcode({
render: "table", //二维码生成方式 table 或 canvas
text: "https://blog.csdn.net/djk8888", //二维码内容 网址
width: "200", //二维码的宽度
height: "200", //二维码的高度
});
</script>
</td>
</tr>
<tr>
<td>
<p>内嵌LOGO写法:</p>
<div id="qrcodeCanvas"></div>
<script>
$('#qrcodeCanvas').qrcode({
render: "canvas",
text: "https://blog.csdn.net/djk8888",
width: "200", //二维码的宽度
height: "200", //二维码的高度
background: "#ffffff", //二维码的后景色
foreground: "#000000", //二维码的前景色
src: 'img/1.jpg' //二维码中间的图片
});
</script>
</td>
<td>
<p>用webAPI接口的写法:</p>
<img src="http://qr.liantu.com/api.php?text=https://blog.csdn.net/djk8888" />
</td>
</tr>
<tr>
<td>
<p>用ThoughtWorks.QRCode.dll组件生成:</p>
<img id="picEncode" />
<script>
var url = "QRCode.ashx?qrUrl=" + encodeURI("https://blog.csdn.net/djk8888");
$("#picEncode").attr("src", url);
</script>
</td>
<td>
<p>生成条形码(API):</p>
<img src='https://barcode.tec-it.com/barcode.ashx?data=https://blog.csdn.net/djk8888' />
</td>
</tr>
</table>
</body>
</html>
QRCode.ashx(ThoughtWorks.QRCode.Codec;)
<%@ WebHandler Language="C#" Class="QRCode" %>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ThoughtWorks.QRCode.Codec;
public class QRCode : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string data = context.Request.QueryString["qrUrl"];
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //byte类型
qrCodeEncoder.QRCodeScale = 4; //大小
qrCodeEncoder.QRCodeVersion = 7; //版本
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //等级
//将bitmap格式的数据转换成byte格式
byte[] img = ImageToByteArray(qrCodeEncoder.Encode(data));
context.Response.ContentType = "image/jpeg"; //输出的文本格式
context.Response.BinaryWrite(img); //将二进制输出
context.Response.Flush();
context.Response.End();
}
catch { }
}
public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public bool IsReusable
{
get
{
return false;
}
}
}
效果如图:
本例配套源码下载:https://download.csdn.net/download/djk8888/10505344
上一篇: Android -Webview内存泄漏
下一篇: 浅谈new/delete