This time, we mainly share the docking codes of the three free QR code interfaces and the points and differences obtained from the test. There are better ways to communicate more and promote progress with each other. Recently, I am learning the extension of JavasScript TypeScript. I feel that the syntax sugar is very sweet, most of which are more similar to C#. It may be the reason for the Microsoft project. Interested friends can communicate with each other more;
The above is my personal opinion. Let’s share today’s article:
- Google's Api QR code generation interface
- 2d-code Api QR code generation interface
- Topscan's API QR code generation interface
- Use object-oriented + loading assembly to create objects and merge the above interfaces to encapsulate them into QR code generation factory
Let’s share it step by step:
Google's Api QR code generation interface
First, here is a link to the Google interface documentqr_codes documentation, enthusiastic friends will open this link immediately. Let’s take a look at the parameters in detail. I won’t take a screenshot. The roughly common parameters are: API address, content parameters, and the height and width of the generated image. The other parameters are all default. You can read them in more detail if you need to see the parameters in the document for the first time. In fact, the first time I saw the parameters in the document, I felt that there were some things missing, such as why I can’t pass the image address of the icon in the middle of the QR code and how to jump to the URL link I want to redirect after scanning the QR code. I tried many times with this question; the following is an explanation of the results of my own attempt:
- The key parameters of the interface are: cht (fixed value qr), chl (content parameters), chs (generated QR code image size, format such as: 200x200, here is xyz's x not *)
- The API interface will only generate a QR code image stream. If you need to save the image locally, you need to directly access the interface through the browser or download the QR code through the program.
- If the content parameters are passed, the text content will not be displayed in the middle of the generated QR code picture. Only by scanning the QR code with your mobile phone can the transmitted text information be displayed on the mobile phone.
- If the content parameters are passed in a simple http:// format link address, then the mobile phone will automatically redirect to the http:// link address after scanning (this scan redirect can be used to view some products or articles)
- The Google interface has not yet developed the address parameters of this logo icon (I hope some friends will share it with me after researching it, thank you)
- Since the API address is foreign, the response to calling the interface is not so fast
Secondly, above are some personal summary. Let’s take a look at the encapsulated request interface method and the method of downloading QR code image:
#region Generate QR code
/// <summary> /// Generate QR code /// </summary> /// <param name="content">Display content (text content or scanned jump http:// format address)</param> /// <param name="savePath">Disk path to save QR code (default program and directory + QRCode)</param> /// <param name="logoUrl"> Logo icon address (format: http://), (Note: This parameter has not been developed by the Google interface)</param> /// <param name="apiUrl">Interface address (built-in default api address)</param> /// <param name="wAndh">Width and height (QR code square, height and width one to 200 by default)</param> /// <returns></returns> public virtual string CreateQRCode(string content = null, string savePath = null, string logoUrl = null, string apiUrl = null, int? wAndh = null) { var qrName = ; #region parameter initialization ApiUrl = apiUrl ?? ApiUrl; Content = content ?? Content; SaveQRPath = savePath ?? SaveQRPath; LogoUrl = logoUrl ?? LogoUrl; WAndH = wAndh ?? WAndH; #endregion if ((ApiUrl)) { return qrName; } ApiUrl = ("{0}?cht=qr&chl={1}&chs={2}x{2}", ApiUrl, (Content), WAndH); qrName = DownImg(ApiUrl, SaveQRPath, ); return qrName; } #endregion
Download the QR code image and save it to the program root directory:
#region Download image /// <summary> /// Download the picture /// </summary> /// <param name="url">Picture download address</param> /// <param name="savePath">SavePath Default: Img folder</param> /// <param name="format">Default: Jpeg</param> /// <returns>New picture name</returns> public virtual string DownImg(string url, string savePath = "QRCode", ImageFormat format = null) { var qrName = ; try { format = format ?? ; HttpClient http = new HttpClient(); = new TimeSpan(0, 1, 0); using (var stream = (url).Result) { if (!(SaveQRPath)) { (SaveQRPath); } qrName = ("yyyyMMddHHmmssfff") + "." + format; var path = (savePath, qrName); using (Image img = (stream)) { (path, format); } } } catch (Exception ex) { qrName = ; } return qrName; } #endregion
The above two methods use virtual methods, because I am using the Google interface as the default QR code generator. The other two interfaces I want to talk about later are rewrites and this; the method of downloading pictures is common and does not need to be rewrites for the time being; what needs to be mentioned here is that there is a fixed parameter cht=qr when calling the API interface. This parameter means that the QR method is used to generate QR codes. Because this API interface also has the function of generating icons, it is fixed here if you want to generate QR codes. More icon generation functions are not shared in this chapter, thank you.
2d-code Api QR code generation interface
First of all, the interface needs to register a key through their official website, and then obtain this key from the background before calling the interface address. Of course, after registration, there is a function that in addition to downloading the QR codes you generate through the API interface, in addition to downloading them through the stream, you can also download all generated pictures through their background. I have not paid attention to whether the generated volume is large, haha. The following lists the following interface parameter descriptions and points of attention obtained from the test:
- The key parameters of the interface are: key (register to obtain), text (text parameter), url (scan and redirect address), logo (Logo icon address), size (QR code square, height and width)
- The text parameters of the interface can only pass text, and cannot be used as the address parameters redirected after scanning are a bit different from those of Google and other interfaces.
- After scanning, the redirect address and the logo icon address are both accessible address links in http:// format.
- Logo icon address, the official said that it is not recommended to use png format, only jpg test is successful (maybe because there are not enough tests here, I will only introduce my results)
- The generation speed is relatively fast, and there is also an artistic word generation interface, which is quite good
- The API interface will only generate a QR code image stream. If you need to save the image locally, you need to directly access the interface through the browser or download the QR code through the program.
Secondly, the encapsulated code is given below. Since the download is common like the code introduced above, I will not make any statement here:
public QR_2dCode() { ApiUrl = "http:///2dcode/"; } #region Generate QR code /// <summary> /// Generate QR code /// </summary> /// <param name="content">Display content (text content or scanned jump http:// format address)</param> /// <param name="savePath">Disk path to save QR code (default program and directory + QRCode)</param> /// <param name="logoUrl"> Logo icon address (format: http://), the official does not recommend using png format, only jpg test is successful</param> /// <param name="directUrl">Redirect address after scanning (http://)</param> /// <param name="apiUrl">Interface address (built-in default api address)</param> /// <param name="wAndh">Width and height (QR code square, height and width one to 200 by default)</param> /// <returns></returns> public override string CreateQRCode(string content = null, string savePath = null, string logoUrl = null, string apiUrl = null, int? wAndh = null) { var qrName = ; #region parameter initialization ApiUrl = apiUrl ?? ApiUrl; Content = content ?? Content; SaveQRPath = savePath ?? SaveQRPath; LogoUrl = logoUrl ?? LogoUrl; WAndH = wAndh ?? WAndH; #endregion if ((ApiUrl)) { return qrName; } ApiUrl = ("{0}?key=c_d800OBbu6hDzJtXPE2Yd02IMtmpuK9VdCqHe6vrtar4&text={1}&url={2}&logo={3}&size={4}", ApiUrl, (("http") ? "" : Content), (Content), (LogoUrl), WAndH); qrName = DownImg(ApiUrl, SaveQRPath); return qrName; } #endregion
Topscan's API QR code generation interface
First of all, this interface is definitely free, and the parameter description is similar to Google. The difference is that it can pass the logo icon address (of course, I may not have found Google's parameters that can pass the logo, so friends can ignore it); the following lists the following interface parameter description and points of attention obtained from the test:
- The key parameters of the interface are: text (content parameters), logo (Logo icon address), w (generated QR code image size, format such as: 200x200, here is xyz's x not *)
- The API interface will only generate a QR code image stream. If you need to save the image locally, you need to directly access the interface through the browser or download the QR code through the program.
- If the content parameters are passed, the text content will not be displayed in the middle of the generated QR code picture. Only by scanning the QR code with your mobile phone can the transmitted text information be displayed on the mobile phone.
- If the content parameters are passed in a simple http:// format link address, then the mobile phone will automatically redirect to the http:// link address after scanning (this scan redirect can be used to view some products or articles)
- Logo icon address (format: http://), jpg, png test passed
- The test concluded that sometimes requesting to generate a QR code does not return data. It may be a problem with my network. The speed of generating a QR code is quite fast.
Secondly, the encapsulated code is given below. Since the download is common like the code introduced above, I will not make any statement here:
public QR_TopScan() { ApiUrl = "/"; } #region Generate QR code /// <summary> /// Generate QR code /// </summary> /// <param name="content">Display content (text content or scanned jump http:// format address)</param> /// <param name="savePath">Disk path to save QR code (default program and directory + QRCode)</param> /// <param name="logoUrl"> Logo icon address (format: http://), jpg, png test passed, and the test was found that there was no success. I don't know if the reason is related to the address</param> /// <param name="apiUrl">Interface address (built-in default api address)</param> /// <param name="wAndh">Width and height (QR code square, height and width one to 200 by default)</param> /// <returns></returns> public override string CreateQRCode(string content = null, string savePath = null, string logoUrl = null, string apiUrl = null, int? wAndh = null) { var qrName = ; #region parameter initialization ApiUrl = apiUrl ?? ApiUrl; Content = content ?? Content; SaveQRPath = savePath ?? SaveQRPath; LogoUrl = logoUrl ?? LogoUrl; WAndH = wAndh ?? WAndH; #endregion if ((ApiUrl)) { return qrName; } ApiUrl = ("{0}?text={1}&logo={2}&w={3}", ApiUrl, (Content), (LogoUrl), WAndH); qrName = DownImg(ApiUrl, SaveQRPath); return qrName; } #endregion
Use object-oriented + loading assembly to create objects and merge the above interfaces to encapsulate them into QR code generation factory
First, analyze the parameters of the above three interfaces and you can see that all of them need fixed parameters: interface API, content (text or jump http address), logo image address (except Google for the time being), width and height, etc. In this way, how can we define a unified parameter class to pass the parameter information? Here we also need to mention that since these interfaces are all obtaining image streams from other interfaces, if you want the image to be saved directly in our program locally when executing the program, you need to download it, so there is another parameter: Save the disk path of the QR code, so there is a public attribute:
#region Basic configuration information /// <summary> /// Interface address (required) /// </summary> protected string ApiUrl = "/chart"; /// <summary> /// Display content (text content), google text parameters are directly passed to http address and directly redirect /// </summary> protected string Content = "/wangrudong003/"; /// <summary> /// The disk path to save the QR code (default program and directory + QRCode) /// </summary> protected string SaveQRPath = (, "QRCode"); /// <summary> /// Logo image address (http://) /// </summary> protected string LogoUrl = "/baike/w%3D268%3Bg%3D0/sign=9a34e44d8bd4b31cf03c93bdbfed4042/"; /// <summary> /// Width and height (QR code square, height and width one to 200 by default) /// </summary> protected int WAndH = 200; #endregion
Then, here I don't want each interface to manually new once to create the object, so I used the module loading assembly to create the required object, so I have the entry to the factory class:
/// <summary> /// QR code generation factory /// </summary> public class QRCodeReposity { public static BaseQRCode Current(QREmType qrEmType = ) { var nspace = typeof(BaseQRCode); var fullName = ; var nowspace = (0, ('.') + 1); return ().CreateInstance(nowspace + (), true) as BaseQRCode; } }
This is similar to the previous cache factory article design. You can check the previous sharing article and like it more, thank you; the following is the following code:
/// <summary> /// Factory module definition /// </summary> public enum QREmType { /// <summary> /// Google interface /// </summary> BaseQRCode, /// <summary> /// 2d-code interface /// </summary> QR_2dCode, /// <summary> /// topscan interface /// </summary> QR_TopScan } /// <summary> /// QR code generation factory /// </summary> public class QRCodeReposity { public static BaseQRCode Current(QREmType qrEmType = ) { var nspace = typeof(BaseQRCode); var fullName = ; var nowspace = (0, ('.') + 1); return ().CreateInstance(nowspace + (), true) as BaseQRCode; } } /// <summary> /// The base class uses Google to provide Api: /chart/infographics/docs/qr_codes. Since it is a foreign address, it is relatively slow. /// </summary> public class BaseQRCode { #region Basic configuration information /// <summary> /// Interface address (required) /// </summary> protected string ApiUrl = "/chart"; /// <summary> /// Display content (text content), google text parameters are directly passed to http address and directly redirect /// </summary> protected string Content = "/wangrudong003/"; /// <summary> /// The disk path to save the QR code (default program and directory + QRCode) /// </summary> protected string SaveQRPath = (, "QRCode"); /// <summary> /// Logo image address (http://) /// </summary> protected string LogoUrl = "/baike/w%3D268%3Bg%3D0/sign=9a34e44d8bd4b31cf03c93bdbfed4042/"; /// <summary> /// Width and height (QR code square, height and width one to 200 by default) /// </summary> protected int WAndH = 200; #endregion #region method #region Generate QR code /// <summary> /// Generate QR code /// </summary> /// <param name="content">Display content (text content or scanned jump http:// format address)</param> /// <param name="savePath">Disk path to save QR code (default program and directory + QRCode)</param> /// <param name="logoUrl"> Logo icon address (format: http://), (Note: This parameter has not been developed by the Google interface)</param> /// <param name="apiUrl">Interface address (built-in default api address)</param> /// <param name="wAndh">Width and height (QR code square, height and width one to 200 by default)</param> /// <returns></returns> public virtual string CreateQRCode(string content = null, string savePath = null, string logoUrl = null, string apiUrl = null, int? wAndh = null) { var qrName = ; #region parameter initialization ApiUrl = apiUrl ?? ApiUrl; Content = content ?? Content; SaveQRPath = savePath ?? SaveQRPath; LogoUrl = logoUrl ?? LogoUrl; WAndH = wAndh ?? WAndH; #endregion if ((ApiUrl)) { return qrName; } ApiUrl = ("{0}?cht=qr&chl={1}&chs={2}x{2}", ApiUrl, (Content), WAndH); qrName = DownImg(ApiUrl, SaveQRPath, ); return qrName; } #endregion #region Download image /// <summary> /// Download the picture /// </summary> /// <param name="url">Picture download address</param> /// <param name="savePath">SavePath Default: Img folder</param> /// <param name="format">Default: Jpeg</param> /// <returns>New picture name</returns> public virtual string DownImg(string url, string savePath = "QRCode", ImageFormat format = null) { var qrName = ; try { format = format ?? ; HttpClient http = new HttpClient(); = new TimeSpan(0, 1, 0); using (var stream = (url).Result) { if (!(SaveQRPath)) { (SaveQRPath); } qrName = ("yyyyMMddHHmmssfff") + "." + format; var path = (savePath, qrName); using (Image img = (stream)) { (path, format); } } } catch (Exception ex) { qrName = ; } return qrName; } #endregion #endregion } /// <summary> /// Use 2d-code to provide API, you need to register on the official website to obtain the Key /// </summary> public class QR_2dCode : BaseQRCode { public QR_2dCode() { ApiUrl = "http:///2dcode/"; } #region Generate QR code /// <summary> /// Generate QR code /// </summary> /// <param name="content">Display content (text content or scanned jump http:// format address)</param> /// <param name="savePath">Disk path to save QR code (default program and directory + QRCode)</param> /// <param name="logoUrl"> Logo icon address (format: http://), the official does not recommend using png format, only jpg test is successful</param> /// <param name="directUrl">Redirect address after scanning (http://)</param> /// <param name="apiUrl">Interface address (built-in default api address)</param> /// <param name="wAndh">Width and height (QR code square, height and width one to 200 by default)</param> /// <returns></returns> public override string CreateQRCode(string content = null, string savePath = null, string logoUrl = null, string apiUrl = null, int? wAndh = null) { var qrName = ; #region parameter initialization ApiUrl = apiUrl ?? ApiUrl; Content = content ?? Content; SaveQRPath = savePath ?? SaveQRPath; LogoUrl = logoUrl ?? LogoUrl; WAndH = wAndh ?? WAndH; #endregion if ((ApiUrl)) { return qrName; } ApiUrl = ("{0}?key=c_d800OBbu6hDzJtXPE2Yd02IMtmpuK9VdCqHe6vrtar4&text={1}&url={2}&logo={3}&size={4}", ApiUrl, (("http") ? "" : Content), (Content), (LogoUrl), WAndH); qrName = DownImg(ApiUrl, SaveQRPath); return qrName; } #endregion } /// <summary> /// Use topscan to provide Api /// </summary> public class QR_TopScan : BaseQRCode { public QR_TopScan() { ApiUrl = "/"; } #region Generate QR code /// <summary> /// Generate QR code /// </summary> /// <param name="content">Display content (text content or scanned jump http:// format address)</param> /// <param name="savePath">Disk path to save QR code (default program and directory + QRCode)</param> /// <param name="logoUrl"> Logo icon address (format: http://), jpg, png test passed, and the test was found that there was no success. I don't know if the reason is related to the address</param> /// <param name="apiUrl">Interface address (built-in default api address)</param> /// <param name="wAndh">Width and height (QR code square, height and width one to 200 by default)</param> /// <returns></returns> public override string CreateQRCode(string content = null, string savePath = null, string logoUrl = null, string apiUrl = null, int? wAndh = null) { var qrName = ; #region parameter initialization ApiUrl = apiUrl ?? ApiUrl; Content = content ?? Content; SaveQRPath = savePath ?? SaveQRPath; LogoUrl = logoUrl ?? LogoUrl; WAndH = wAndh ?? WAndH; #endregion if ((ApiUrl)) { return qrName; } ApiUrl = ("{0}?text={1}&logo={2}&w={3}", ApiUrl, (Content), (LogoUrl), WAndH); qrName = DownImg(ApiUrl, SaveQRPath); return qrName; } #endregion }
There is not much content in this article in terms of programming. The focus is on the difference between these interfaces and sharing interfaces. Friends can use it to communicate with friends who are connecting with QR code generation, that's all; the notes for the key code are all in the sharing code. If there are any better or unclear areas, please leave a message. Thank you.
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!