CN111797960A - Multithreading two-dimensional code generation algorithm based on Web Workers - Google Patents

Multithreading two-dimensional code generation algorithm based on Web Workers Download PDF

Info

Publication number
CN111797960A
CN111797960A CN202010667043.0A CN202010667043A CN111797960A CN 111797960 A CN111797960 A CN 111797960A CN 202010667043 A CN202010667043 A CN 202010667043A CN 111797960 A CN111797960 A CN 111797960A
Authority
CN
China
Prior art keywords
dimensional code
thread
worker
code generation
data
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Withdrawn
Application number
CN202010667043.0A
Other languages
Chinese (zh)
Inventor
汪金玲
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Individual
Original Assignee
Individual
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Individual filed Critical Individual
Priority to CN202010667043.0A priority Critical patent/CN111797960A/en
Publication of CN111797960A publication Critical patent/CN111797960A/en
Withdrawn legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06KGRAPHICAL DATA READING; PRESENTATION OF DATA; RECORD CARRIERS; HANDLING RECORD CARRIERS
    • G06K19/00Record carriers for use with machines and with at least a part designed to carry digital markings
    • G06K19/06Record carriers for use with machines and with at least a part designed to carry digital markings characterised by the kind of the digital marking, e.g. shape, nature, code
    • G06K19/06009Record carriers for use with machines and with at least a part designed to carry digital markings characterised by the kind of the digital marking, e.g. shape, nature, code with optically detectable marking
    • G06K19/06037Record carriers for use with machines and with at least a part designed to carry digital markings characterised by the kind of the digital marking, e.g. shape, nature, code with optically detectable marking multi-dimensional coding
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/95Retrieval from the web
    • G06F16/957Browsing optimisation, e.g. caching or content distillation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/10Text processing
    • G06F40/12Use of codes for handling textual entities
    • G06F40/126Character encoding

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Health & Medical Sciences (AREA)
  • Artificial Intelligence (AREA)
  • Audiology, Speech & Language Pathology (AREA)
  • Computational Linguistics (AREA)
  • General Health & Medical Sciences (AREA)
  • Information Transfer Between Computers (AREA)

Abstract

The invention relates to the technical field of two-dimensional code generation, and discloses a multithreading two-dimensional code generation algorithm based on Web Workers, which comprises the following steps: the browser end receives a two-dimension code generation request and acquires two-dimension code generation data according to the two-dimension code generation request; encoding the two-dimensional code generation data, and taking the encoded data as a two-dimensional code content information parameter; packaging the two-dimension code content information parameter and a preset two-dimension code parameter into a parameter object by a JavaScript main thread of the browser, and instantiating a Worker sub-thread; sending the parameter object in the JavaScript main thread to a Worker sub-thread; the Worker sub-thread analyzes the received parameter object and calculates a corresponding two-dimensional code matrix; the Worker sub-thread traverses the two-dimensional code matrix and renders the two-dimensional code by using a < rect > tag of the SVG so as to obtain an SVG image of the two-dimensional code; and the Worker sub-thread sends the SVG image back to the JavaScript main thread, and the JavaScript main thread directly displays the two-dimensional code on the HTML page in an inline SVG mode. The invention improves the generation efficiency of the two-dimensional code.

Description

Multithreading two-dimensional code generation algorithm based on Web Workers
Technical Field
The invention relates to the technical field of two-dimensional code generation, in particular to a multi-thread two-dimensional code generation algorithm based on Web Workers.
Background
With the rapid development of the internet and the internet of things and the popularization of intelligent terminal equipment, the two-dimensional code is rapidly developed as a new information carrier and a new transfer tool, plays an indispensable role in various industries, is not only applied to social networks, but also is more widely applied to business, payment and other service links.
The two-dimensional code records and stores data information by using the distribution of black and white alternating geometric figures on a two-dimensional plane according to a certain design rule, the internal code of the two-dimensional code respectively represents the bit streams of binary systems of '0' and '1' of a computer by using two graphical symbols of white and black, and related information such as characters, letters and the like is stored and recorded by using the geometric patterns of black and white alternating corresponding to the binary systems of '0' and '1' according to the rule, meanwhile, the two-dimensional code can realize the intelligent processing of the stored information of the two-dimensional code by means of image input or photoelectric automatic scanning, and the two-dimensional code has some commonalities of bar code technology: each code system has its specific character set; each character occupies a certain width; meanwhile, the two-dimensional code checking device has a certain checking function, can scan and process damaged two-dimensional codes, and can scan the two-dimensional codes in all directions.
The traditional two-dimensional code generation algorithm is mainly divided into a two-dimensional code generation algorithm based on JavaScript and a two-dimensional code generation algorithm based on jQuery. The two-dimension code generation algorithm based on the JavaScript can generate the two-dimension code without a library or a plug-in of a third party, but because the JavaScript is executed by a single thread, when the operation is complicated or the calculated amount is large, the JavaScript engine cannot execute other JavaScript scripts, and meanwhile, the GUI rendering thread of the browser is in a waiting state, so that the browser can possibly enter a false death state; compared with the JavaScript-based algorithm, the jQuery-based two-dimensional code generation algorithm can generate the two-dimensional code more conveniently, a developer only needs to import the jQuery library file and the two-dimensional code plug-in, and sets the two-dimensional code parameters to generate the two-dimensional code, but the HTML page needs to load the jQuery library file and the two-dimensional code plug-in to generate the required two-dimensional code, so that the response time of the HTML page is prolonged.
In view of this, how to generate a two-dimensional code efficiently while ensuring the stability of a two-dimensional code generating page and saving system resources becomes an urgent problem to be solved by those skilled in the art.
Disclosure of Invention
The invention provides a multithreading two-dimension code generation algorithm based on Web works, which mainly aims to improve the generation efficiency of a two-dimension code and the rendering efficiency of the two-dimension code on the basis of ensuring the stability of a two-dimension code generation page and saving system resources.
In order to achieve the above object, the present invention provides a multithreading two-dimensional code generation algorithm based on Web works, including:
the browser end receives a two-dimensional code generation request and acquires two-dimensional code generation data according to the two-dimensional code generation request;
generating data by aiming at two-dimensional codes of different data types, and performing coding processing by using different data coding modes, wherein coded data of the two-dimensional code generation data are used as two-dimensional code content information parameters;
a JavaScript main thread of the browser acquires the two-dimension code content information parameters and preset two-dimension code size parameters, packages the two-dimension code content information parameters and the preset two-dimension code parameters into parameter objects, and instantiates a plurality of Worker sub-threads;
sending a parameter object in the JavaScript main thread to one of the Worker sub-threads by using a main thread sending function;
the Worker sub-thread analyzes the received parameter object, so that a corresponding two-dimensional code matrix is calculated according to the analyzed two-dimensional code content information parameter and the two-dimensional code size parameter, and the two-dimensional code matrix is sent to another Worker sub-thread;
the Worker sub-thread traverses the two-dimensional code matrix and renders the two-dimensional code by using a < rect > tag of the SVG so as to obtain an SVG image of the two-dimensional code;
and sending the SVG image back to a JavaScript main thread, terminating the Worker sub-thread, and simultaneously, directly displaying the two-dimensional code on an HTML page by the JavaScript main thread in an inline SVG mode to finish the generation of the two-dimensional code.
Optionally, the acquiring two-dimensional code generation data according to the two-dimensional code generation request includes:
the two-dimensional code generation data are content information data which are required to be stored by the two-dimensional code, and the data types of the content information data include but are not limited to letters, numbers and Chinese characters.
Optionally, the generating data of the two-dimensional codes for different data types and performing encoding processing by using different data encoding modes include:
dividing the two-dimensional code generation data of the numerical type into one group of three bits, taking each group as a new decimal number after grouping, converting the decimal number into a binary number with the length of 10 bits, correspondingly converting redundant 1-bit or 2-bit digits into the binary number with the length of 4 bits or 7 bits when the digit number of the digit cannot be divided by 3, and then sequentially connecting all the binary numbers in sequence to obtain a coding result of the two-dimensional code generation data of the numerical type;
performing ASCII code conversion on the two-dimensional code generation data of the letter type to convert the two-dimensional code generation data of the letter type into a corresponding decimal ASCII code, performing coding processing on the converted decimal ASCII code by using the coding mode of the numerical value type data, and adding a letter type two-dimensional code generation data prefix prompt 1011 to a coding result; and
based on the Chinese character standard GB2312, each Chinese character is represented by two bytes, each Chinese character corresponds to a character internal code value, and the two-dimensional code generation data of the Chinese character type is converted into binary number with the length of 13 bits according to the following two conditions so as to realize the coding processing of the two-dimensional code generation data of the Chinese character type:
if the inner code value of the first byte is between A1HEX~AAHEXThe inner code value of the second byte is between A1HEX~FEHEXAnd then:
1) subtract A1 from the inner code value of the first byteHEXThe result R1 is obtained;
2) multiply the result R1 by 60HEXThe result R2 is obtained;
3) subtracting A1 from the inner code value of the second byteHEXThe result R3 is obtained;
4) adding R2 and R3 to obtain a result R;
5) and converting the result R into a binary code number with the length of 13 bits.
If the inner code value of the first byte is between BOHEX~FAHEXThe inner code value of the second byte is between A1HEX~FEHEXAnd then:
1) subtract A6 from the inner code value of the first byteHEXThe result R1 is obtained;
2) multiply the result R1 by 60HEXThe result R2 is obtained;
3) subtracting A1 from the inner code value of the second byteHEXThe result R3 is obtained;
4) adding R2 and R3 to obtain a result R;
5) and converting the result R into a binary code number with the length of 13 bits.
Optionally, the process of instantiating a number of Worker sub-threads is as follows:
the JavaScript main thread creates a plurality of js files by using a Worker () method;
putting a js file into a browser server, wherein the js file registers an event of message monitoring information;
the event is triggered when the JavaScript main thread initiates information for establishing a plurality of Worker instance sub-threads, so that a Worker end is established at a browser server, the Worker end simultaneously initializes a plurality of Worker sub-thread instances, and sends information that the Worker sub-thread instances are established to the JavaScript main thread.
Optionally, the sending the parameter object in the JavaScript main thread by using a main thread sending function includes:
the JavaScript main thread uses a postMessage () main thread sending function to copy the parameter object and create a copy of the parameter object in a memory of the Worker end, the Worker sub thread uses an onmessage () function to monitor information in the Worker end, and if the copy of the parameter object of the Worker end is monitored, the onmessage () function is used to receive the copy information of the parameter object.
Optionally, the calculating a corresponding two-dimensional code matrix according to the two-dimensional code content information parameter and the two-dimensional code size parameter obtained by the analysis includes:
constructing a two-dimensional code blank matrix according with the size parameter of the two-dimensional code, detecting blank areas corresponding to the graph, the two-dimensional code position detection graph separator, the two-dimensional code positioning graph and the correction graph at the two-dimensional code position, and filling proper black and white two-color squares;
alternately performing layout filling on binary data in the content information parameters of the two-dimensional code according to a rule of from right to left, from bottom to top or from top to bottom by taking two modules as units from the lower right corner of the blank matrix area of the two-dimensional code; and
and performing exclusive OR operation on the filled two-dimensional code matrix area by using a known mask reference pattern, then recording and grading the result of each exclusive OR operation, and selecting the mask with the lowest grade as the mode check of the two-dimensional code area.
Optionally, the Worker sub-thread traverses the two-dimensional code matrix and renders the two-dimensional code using a < rect > tag of SVG, including:
the Worker sub-matrix traverses each element of the two-dimensional code matrix, calculates the width and height of each module of the two-dimensional code according to the size of the two-dimensional code set by the size parameter of the two-dimensional code, and then circularly traverses the whole two-dimensional code matrix;
the < rect > tag comprises four parameters which respectively represent the distance between a rendering position and the left boundary and the upper boundary of the two-dimensional code and the length and the width of a rendering area;
< rect > the label is automatic to be analyzed to the element in the two-dimensional code matrix, uses black to render the two-dimensional code when the element in the two-dimensional code matrix is 1, then need not fill when the element in the two-dimensional code matrix is 0 and render.
Optionally, the termination of the Worker sub-thread is:
after the Worker sub-thread completes the calculation of the two-dimensional code matrix and the rendering of the two-dimensional code, in order to save system resources, the Worker sub-thread calls a close () method to immediately destroy the two-dimensional code matrix, even if the program of the Worker sub-thread is not finished, the Worker sub-thread can be destroyed, and the program cannot be executed any more.
The Web Workers can enable the client side to execute the JavaScript program in parallel, and break the limitation of a single thread of the JavaScript program, so that the standard of multi-thread programming can be realized. Compared with the traditional JavaScript single-thread standard, the Web works can run for a long time, can be started quickly and occupies a small memory.
Compared with the prior art, the invention provides a multi-thread two-dimensional code generation algorithm based on WebWorkers, and the technology has the following advantages:
firstly, most of the existing two-dimension code generation algorithms adopt an encryption algorithm to encrypt content information of a two-dimension code, and a JavaScript single-thread technology is used to generate the two-dimension code of the encrypted content, the prior art consumes a large amount of CPU computing resources to encrypt the content information of the two-dimension code, the two-dimension code generation algorithm automatically selects a corresponding data coding mode to code the content information of the two-dimension code according to different data types of the content information of the two-dimension code, so that the coded data obtained by the invention has better encryption performance, meanwhile, the invention is based on the graph modularization idea, a two-dimension code matrix is started from the lower right corner of a matrix area, two modules are taken as a unit, and binary coded data are used for alternately filling a blank matrix of the two-dimension code from a high position to a low position according to a rule of from right to left, from bottom to top or from top to bottom, compared with the prior art, the algorithm carries out modular processing on the two-dimensional code, when the two-dimensional code is generated, only the two-dimensional code matrix needs to be filled according to the corresponding position, an encryption algorithm is not needed, and more efficient two-dimensional code generation can be realized under the condition of less CPU resource loss;
secondly, because the process of generating the two-dimensional code is a calculation process for converting characters into a two-dimensional code matrix, the traditional front-end two-dimensional code generation algorithm only uses a JavaScript single thread to calculate the two-dimensional code matrix, and a GUI rendering thread is likely to be blocked when the coded data amount is large, the invention is based on the Web Workers multithreading theory, a plurality of Worker sub-threads are instantiated by using a JavaScript main thread, the calculation process of the two-dimensional code matrix is put in the Worker sub-threads to be processed, the pressure of the JavaScript main thread is reduced, and the problem of page collapse caused by the overlarge coded data amount is effectively avoided; and because the JavaScript main thread and the Worker sub-thread are not in the same context environment and cannot directly communicate, the JavaScript main thread copies the parameter object by using a postMessage () main thread sending function and creates a copy of the parameter object in a memory of the Worker end by means of the Worker end, the Worker sub-thread monitors information in the Worker end by using an onmessage () function, and if the copy of the parameter object of the Worker end is monitored, the onmessage () function is used for receiving the copy information of the parameter object, so that the message transfer of the JavaScript main thread and the Worker sub-thread is realized. Compared with the existing front-end two-dimensional code generation algorithm based on the JavaScript single thread, the multi-thread two-dimensional code generation algorithm based on the WebWorkers carries out information interaction by adopting the main thread, and a plurality of sub-threads carry out two-dimensional code calculation processing, so that the stability of a two-dimensional code generation page is better, and the two-dimensional code generation efficiency is higher.
The method comprises the steps that a Worker sub-thread in Web Workers cannot read a DOM object of a page where a JavaScript main thread is located, so that the Worker sub-thread cannot perform rendering operation of a two-dimensional code, the existing two-dimensional code rendering technology can only be executed in the JavaScript main thread, the service pressure of the main thread can be increased, and the problem of page collapse can be caused. The two-dimensional code generation algorithm takes the limitation that WebWorkers can not operate DOM nodes into consideration, two-dimensional codes are rendered by means of an SVG example, and two-dimensional code patterns are displayed in a main thread in an inline SVG mode, so that the service pressure of the main thread is reduced as much as possible, the utilization rate of a plurality of Worker sub-threads and the efficiency of the main thread are improved, and the generation efficiency of the two-dimensional codes is accelerated; meanwhile, a plurality of Worker sub-threads are created by the two-dimension code generation algorithm, so that compared with the existing JavaScript single-thread two-dimension code generation algorithm, a plurality of two-dimension codes can be generated simultaneously on the basis of ensuring the stability of a two-dimension code generation page.
Drawings
Fig. 1 is a schematic flow chart of a multithreading two-dimensional code generation algorithm based on Web Workers according to an embodiment of the present invention;
the implementation, functional features and advantages of the objects of the present invention will be further explained with reference to the accompanying drawings.
Detailed Description
It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
The invention provides a multithreading two-dimensional code generation algorithm based on Web Workers, which aims to improve the utilization rate of a CPU (Central processing Unit), prevent a browser from entering a dead state and solve the problems of low two-dimensional code generation efficiency, long system response time and low system stability of the traditional two-dimensional code generation algorithm. Referring to fig. 1, a schematic flow chart of a multi-threaded two-dimensional code generation algorithm based on WebWorkers according to an embodiment of the present invention is shown.
In this embodiment, the multi-threaded two-dimensional code generation algorithm based on Web Workers includes:
s1, the browser end receives the two-dimension code generation request, acquires two-dimension code generation data according to the two-dimension code generation request, performs coding processing by using different data coding modes aiming at the two-dimension code generation data with different data types, and takes coded data of the two-dimension code generation data as two-dimension code content information parameters.
The browser for generating the two-dimensional code receives a two-dimensional code generation request of a user, and acquires two-dimensional code generation data according to the two-dimensional code generation request, wherein the two-dimensional code generation data is content information data required to be stored by the two-dimensional code, and the data type of the content information data includes but is not limited to letters, numbers, Chinese characters and the like.
Furthermore, for the acquired two-dimensional code generation data, the data coding processing in different modes is carried out according to different two-dimensional code generation data types, the two-dimensional code generation data in different data types are coded in different modes, the two-dimensional code content information can be well encrypted, and the coded data of the acquired two-dimensional code generation data are used as the two-dimensional code content information parameters.
In the embodiment of the invention, for the numerical type two-dimensional code generated data, the invention divides the data into three groups, each group is used as a new decimal number after grouping, then the decimal number is converted into a binary number with the length of 10 bits, when the digit number of the digit cannot be divided by 3, the redundant 1-bit or 2-bit digit is correspondingly converted into the binary number with the length of 4 bits or 7 bits, and then all the binary numbers are sequentially connected in sequence, thereby realizing the coding of the numerical type two-dimensional code generated data;
for the two-dimensional code generation data of the letter type, firstly, ASCII code conversion is carried out on the two-dimensional code generation data of the letter type so as to convert the two-dimensional code generation data of the letter type into an ASCII code corresponding to decimal, the coded decimal ASCII code is coded by using the coding mode of the numerical value type data, and meanwhile, a letter type two-dimensional code generation data prefix prompt 1011 is added to a coded result;
for two-dimensional code generation data of Chinese character types, the two-dimensional code generation data of Chinese character types is converted into binary coding data based on Chinese character standard GB2312, each Chinese character is specified in the Chinese character standard GB2312 to be represented by two bytes, each Chinese character corresponds to a character internal code value, the standard records 6763 Chinese characters in total, each Chinese character corresponds to a uniquely determined internal code value, in the preferred embodiment of the invention, the two-dimensional code generation data of Chinese character types are converted into binary numbers with the length of 13 bits according to the following two conditions so as to realize coding processing of the two-dimensional code generation data of Chinese character types:
if the inner code value of the first byte is between A1HEX~AAHEXThe inner code value of the second byte is between A1HEX~FEHEXAnd then:
1) subtract A1 from the inner code value of the first byteHEXThe result R1 is obtained;
2) multiply the result R1 by 60HEXThe result R2 is obtained;
3) subtracting A1 from the inner code value of the second byteHEXThe result R3 is obtained;
4) adding R2 and R3 to obtain a result R;
5) and converting the result R into a binary code number with the length of 13 bits.
If the inner code value of the first byte is between BOHEX~FAHEXThe inner code value of the second byte is between A1HEX~FEHEXAnd then:
1) subtract A6 from the inner code value of the first byteHEXThe result R1 is obtained;
2) multiply the result R1 by 60HEXThe result R2 is obtained;
3) subtracting A1 from the inner code value of the second byteHEXThe result R3 is obtained;
4) adding R2 and R3 to obtain a result R;
5) and converting the result R into a binary code number with the length of 13 bits.
S2, the JavaScript main thread of the browser acquires the two-dimension code content information parameters and the preset two-dimension code size parameters, encapsulates the two-dimension code content information parameters and the preset two-dimension code parameters into parameter objects, and instantiates a plurality of Worker sub-threads.
Furthermore, a JavaScript main thread of the browser acquires the two-dimension code content information parameters and preset two-dimension code size parameters, and simultaneously packages the two-dimension code content information parameters and the preset two-dimension code parameters into a parameter object, wherein the parameter object comprises two set parameters, and after the two-dimension code parameters are packaged into an object, the two-dimension code parameters can be taken as a whole, so that data transmission between the main thread and the sub thread is conveniently realized;
further, the JavaScript main thread of the browser instantiates a plurality of Worker sub-threads by using a Worker () method, the plurality of Worker sub-threads can independently execute a calculation task, so that the main thread performs message interaction, the plurality of sub-threads perform two-dimensional code calculation processing to ensure the stability of a two-dimensional code generation page, and the generation process of the example of the Worker sub-threads is as follows:
(1) firstly, a JavaScript main thread creates a js file worker.js by using a Worker () method;
(2) putting the js file into a browser server, wherein the js file simultaneously registers an event of message monitoring information;
(3) the event is triggered when the JavaScript main thread initiates information for establishing a Worker instance sub-thread, so that a Worker end is established at a browser server, the Worker end simultaneously initializes a Worker sub-thread instance and sends information for completing the establishment of the Worker sub-thread instance to the JavaScript main thread.
S3, sending the parameter object in the JavaScript main thread to one of the Worker sub-threads by using a main thread sending function, analyzing the received parameter object by the Worker sub-thread, calculating a corresponding two-dimensional code matrix according to the analyzed two-dimensional code content information parameter and the two-dimensional code size parameter, sending the two-dimensional code matrix to the other Worker sub-thread, and closing the current Worker sub-thread.
The JavaScript main thread and the Worker sub-thread are not in the same context environment and cannot directly communicate with each other, after the JavaScript main thread obtains the created Worker sub-thread example, the JavaScript main thread uses a postMessage () main thread sending function to copy a parameter object and creates a copy of the parameter object in a memory of a Worker end, the Worker sub-thread uses an onmessage () function to monitor information in the Worker end, if the copy of the parameter object of the Worker end is monitored, the onmessage () function is used to receive the copy information of the parameter object, so that the communication between the main thread and the Worker sub-thread is realized, and the Worker sub-thread analyzes the two-dimensional code content information parameter and the two-dimensional code size parameter;
further, the Worker sub-thread calculates a corresponding two-dimensional code matrix according to the obtained two-dimensional code content information parameter and the two-dimensional code size parameter, and sends the obtained two-dimensional code matrix to another Worker sub-thread, and the current Worker sub-thread closes the sub-thread by using a close () method to save system resources, wherein the process of obtaining the two-dimensional code matrix by using the two-dimensional code parameters is as follows:
1) firstly, constructing a two-dimensional code blank matrix according with the size parameter of the two-dimensional code, detecting blank areas corresponding to a pattern, a two-dimensional code position detection pattern separator, a two-dimensional code positioning pattern and a correction pattern at the position of the two-dimensional code, and filling proper black and white two-color squares;
2) alternately performing layout filling on binary data in the content information parameters of the two-dimensional code according to a rule of from right to left, from bottom to top or from top to bottom by taking two modules as units from the lower right corner of the blank matrix area of the two-dimensional code; in detail, the two-dimensional code blank matrix is provided with a plurality of columns with the width of 2 modules from the right side to the left side, the two-dimensional code blank matrix follows the rule from right to left in the layout modes of two columns from bottom to top and from top to bottom, and each module fills the two-dimensional code blank matrix with binary data from high order to low order according to the sequence of the binary data because the content information parameters of the two-dimensional code are all binary data, wherein 7 represents the highest order and is insufficient or is left for 0;
3) if the left and right boundaries of the two-dimension code positioning graph or the two-dimension code correction graph are met during the filling of the two-dimension code blank matrix, the boundary can be skipped to continue to fill the two-dimension code matrix;
4) because the problem of uneven black-white distribution sometimes occurs in the process of rendering the two-dimensional code due to the particularity of the filled data after the modules are arranged in the matrix, a large-area black module or a large-area white module may occur, the method uses the known mask reference pattern to perform exclusive-or operation on the filled two-dimensional code matrix area, then records and scores the result of each exclusive-or operation, and then selects the mask with the lowest score as the mode check of the two-dimensional code area.
And S4, traversing the two-dimensional code matrix by the Worker sub-thread, rendering the two-dimensional code by using a < rect > tag of the SVG to obtain an SVG image of the two-dimensional code, sending the SVG image back to the JavaScript main thread by the Worker sub-thread, and simultaneously directly displaying the two-dimensional code on an HTML page by the JavaScript main thread in an inline SVG mode to complete the generation of the two-dimensional code.
Furthermore, the Worker sub-matrix traverses each element of the two-dimensional code matrix, calculates the width and height of each module of the two-dimensional code according to the size of the two-dimensional code set by the size parameter of the two-dimensional code, and then circularly traverses the whole two-dimensional code matrix;
further, according to the traversal result of the two-dimensional code matrix, because the two-dimensional code is a small square with black and white alternated, the invention uses the < rect > tag of the SVG to render the two-dimensional code square, so as to obtain the SVG image of the two-dimensional code, wherein the < rect > tag only needs 4 parameters, in the embodiment of the invention, for example: the method comprises the steps that < rect x ═ 10y ═ 10width ═ 100height ═ 100fill ═ black'/>, wherein x and y respectively represent distances from a left boundary and an upper boundary, width and height respectively represent the width and height of a rendering area, and fill represents colors needing to be filled; because the two-dimensional code pattern only has black and white colors, the black color is filled when the element in the two-dimensional code matrix is 1, and the black color is not required to be filled when the element in the two-dimensional code matrix is 0; the SVG is a graphic format for describing two-dimensional vector graphics, is different from a general picture format, uses a syntactic structure of XML, and can directly use a < rect > tag in the SVG technology to render two-dimensional codes without rendering the two-dimensional codes in a JavaScript main thread because the SVG adopts a text form to describe two-dimensional code vector graphics and completely supports DOM; because the rendering process of the two-dimensional code is calculated and processed in the sub-thread, the service pressure of the JavaScript main thread is reduced.
The Worker sub-thread sends the two-dimensional code SVG image to a JavaScript main thread by using a sub-thread sending function postMessage (), the JavaScript main thread monitors and receives the two-dimensional code matrix by using a main thread monitoring and receiving function onmessage (), and the Worker sub-thread closes the sub-thread by using a close () method so as to save system resources;
in the embodiment of the invention, the JavaScript main thread uses an inline SVG mode to directly embed SVG elements in an HTML page, so that SVG two-dimensional codes are directly displayed on the HTML page to complete the generation of the two-dimensional codes.
The following describes the embodiments of the present invention through a simulation experiment, and tests the inventive algorithm. Based on the generation efficiency of the two-dimension code, the efficiency of generating the two-dimension code by utilizing WebWorkers multithreading and JavaScript single thread for the two-dimension code information with different data lengths is respectively compared in different browsers; the CPU of the hardware environment of the simulation experiment is Intel (R) core (TM) i5-4570, the CPU dominant frequency is 3.20Ghz, the memory capacity is 4.00GB, the memory type is DDR3800MHz, the display card is Intel (R) HD Graphics 4600, and the operating system is 64-bit Windows7 flagship edition;
under the experimental environment, the time required by generating the two-dimensional code by respectively utilizing a JavaScript single-thread algorithm and a Web Workers multi-thread algorithm is respectively tested under a Chrome 50.0 browser, a Firefox48.0 browser, an Opera 39.0 browser and an IE 11 browser when the size of the two-dimensional code input data is respectively 17 bytes and 1852 bytes;
when the size of the input data of the two-dimensional code is 17 bytes, for the browser with Chrome 50.0, Firefox48.0, IE 11 and Opera 39.0 selected by the invention, the time required for generating the two-dimensional code by using a JavaScript single-thread algorithm is 22ms, 8ms, 10ms and 8ms respectively, and the time required for generating the two-dimensional code by using a Web Workers multi-thread algorithm is 20ms, 6ms, 9ms and 5ms respectively, so that the two-dimensional code generation efficiency is improved by 9%, 25%, 10% and 37% respectively;
when the size of the input data of the two-dimensional code is 1852 bytes, for the browser 50.0, Firefox48.0, IE 11 and Opera 39.0 selected by the invention, the time required for generating the two-dimensional code by using a JavaScript single-thread algorithm is respectively 60ms, 50ms, 310ms and 55ms, and the time required for generating the two-dimensional code by using a Web Workers multi-thread algorithm is respectively 55ms, 40ms, 280ms and 50ms, so that the two-dimensional code generation efficiency is respectively improved by 8%, 20%, 10% and 9%;
according to the experimental results, the multi-thread two-dimensional code generation algorithm based on the Web works is feasible, and compared with the existing JavaScript single-thread two-dimensional code generation algorithm, the two-dimensional code generation algorithm can effectively improve the generation efficiency of the two-dimensional code in different browsers selected by the invention.
It should be noted that the above-mentioned numbers of the embodiments of the present invention are merely for description, and do not represent the merits of the embodiments. And the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, apparatus, article, or method that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, apparatus, article, or method. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, apparatus, article, or method that includes the element.
The above description is only a preferred embodiment of the present invention, and not intended to limit the scope of the present invention, and all modifications of equivalent structures and equivalent processes, which are made by using the contents of the present specification and the accompanying drawings, or directly or indirectly applied to other related technical fields, are included in the scope of the present invention.

Claims (8)

1. A multithreading two-dimensional code generation algorithm based on Web Workers is characterized by comprising the following steps:
the browser end receives a two-dimensional code generation request and acquires two-dimensional code generation data according to the two-dimensional code generation request;
generating data by aiming at two-dimensional codes of different data types, and performing coding processing by using different data coding modes, wherein coded data of the two-dimensional code generation data are used as two-dimensional code content information parameters;
a JavaScript main thread of the browser acquires the two-dimension code content information parameters and preset two-dimension code size parameters, packages the two-dimension code content information parameters and the preset two-dimension code parameters into parameter objects, and instantiates a plurality of Worker sub-threads;
sending a parameter object in the JavaScript main thread to one of the Worker sub-threads by using a main thread sending function;
the Worker sub-thread analyzes the received parameter object, so that a corresponding two-dimensional code matrix is calculated according to the analyzed two-dimensional code content information parameter and the two-dimensional code size parameter, and the two-dimensional code matrix is sent to another Worker sub-thread;
the Worker sub-thread traverses the two-dimensional code matrix and renders the two-dimensional code by using a < rect > tag of the SVG so as to obtain an SVG image of the two-dimensional code;
and sending the SVG image back to a JavaScript main thread, terminating the Worker sub-thread, and simultaneously, directly displaying the two-dimensional code on an HTML page by the JavaScript main thread in an inline SVG mode to finish the generation of the two-dimensional code.
2. The Web Workers-based multi-threaded two-dimensional code generation algorithm as recited in claim 1, wherein the obtaining of the two-dimensional code generation data according to the two-dimensional code generation request comprises:
the two-dimensional code generation data are content information data which are required to be stored by the two-dimensional code, and the data types of the content information data include but are not limited to letters, numbers and Chinese characters.
3. The Web Workers-based multi-thread two-dimensional code generation algorithm as claimed in claim 2, wherein the encoding processing is performed by using different data encoding modes for the two-dimensional code generation data of different data types, including:
dividing the two-dimensional code generation data of the numerical type into one group of three bits, taking each group as a new decimal number after grouping, converting the decimal number into a binary number with the length of 10 bits, correspondingly converting redundant 1-bit or 2-bit digits into the binary number with the length of 4 bits or 7 bits when the digit number of the digit cannot be divided by 3, and then sequentially connecting all the binary numbers in sequence to obtain a coding result of the two-dimensional code generation data of the numerical type;
performing ASCII code conversion on the two-dimensional code generation data of the letter type to convert the two-dimensional code generation data of the letter type into a corresponding decimal ASCII code, performing coding processing on the converted decimal ASCII code by using the coding mode of the numerical value type data, and adding a letter type two-dimensional code generation data prefix prompt 1011 to a coding result; and
based on the Chinese character standard GB2312, each Chinese character is represented by two bytes, each Chinese character corresponds to a character internal code value, and the two-dimensional code generation data of the Chinese character type is converted into binary number with the length of 13 bits according to the following two conditions so as to realize the coding processing of the two-dimensional code generation data of the Chinese character type:
if the inner code value of the first byte is between A1HEX~AAHEXThe inner code value of the second byte is between A1HEX~FEHEXAnd then:
1) subtract A1 from the inner code value of the first byteHEXThe result R1 is obtained;
2) multiply the result R1 by 60HEXThe result R2 is obtained;
3) subtracting A1 from the inner code value of the second byteHEXThe result R3 is obtained;
4) adding R2 and R3 to obtain a result R;
5) and converting the result R into a binary code number with the length of 13 bits.
If the inner code value of the first byte is between B0HEX~FAHEXThe inner code value of the second byte is between A1HEX~FEHEXAnd then:
1) subtract A6 from the inner code value of the first byteHEXThe result R1 is obtained;
2) multiply the result R1 by 60HEXThe result R2 is obtained;
3) subtracting A1 from the inner code value of the second byteHEXThe result R3 is obtained;
4) adding R2 and R3 to obtain a result R;
5) and converting the result R into a binary code number with the length of 13 bits.
4. The Web Workers-based multi-threaded two-dimensional code generation algorithm as recited in claim 1, wherein the process of instantiating a number of Worker sub-threads is:
the JavaScript main thread creates a plurality of js files by using a Worker () method;
putting a js file into a browser server, wherein the js file registers an event of message monitoring information;
the event is triggered when the JavaScript main thread initiates information for establishing a plurality of Worker instance sub-threads, so that a Worker end is established at a browser server, the Worker end simultaneously initializes a plurality of Worker sub-thread instances, and sends information that the Worker sub-thread instances are established to the JavaScript main thread.
5. The WebWorkers-based multi-threaded two-dimensional code generation algorithm as recited in claim 4, wherein sending the parameter object in the JavaScript main thread using a main thread sending function comprises:
the JavaScript main thread uses a postMessage () main thread sending function to copy the parameter object and create a copy of the parameter object in a memory of the Worker end, the Worker sub thread uses an onmessage () function to monitor information in the Worker end, and if the copy of the parameter object of the Worker end is monitored, the onmessage () function is used to receive the copy information of the parameter object.
6. The WebWorkers-based multi-thread two-dimensional code generation algorithm as claimed in claim 5, wherein the calculating of the corresponding two-dimensional code matrix according to the analyzed two-dimensional code content information parameter and the two-dimensional code size parameter comprises:
constructing a two-dimensional code blank matrix according with the size parameter of the two-dimensional code, detecting blank areas corresponding to the graph, the two-dimensional code position detection graph separator, the two-dimensional code positioning graph and the correction graph at the two-dimensional code position, and filling proper black and white two-color squares;
alternately performing layout filling on binary data in the content information parameters of the two-dimensional code according to a rule of from right to left, from bottom to top or from top to bottom by taking two modules as units from the lower right corner of the blank matrix area of the two-dimensional code; and
and performing exclusive OR operation on the filled two-dimensional code matrix area by using a known mask reference pattern, then recording and grading the result of each exclusive OR operation, and selecting the mask with the lowest grade as the mode check of the two-dimensional code area.
7. The Web Workers-based multi-threaded two-dimensional code generation algorithm of claim 6, wherein the Worker sub-thread traverses the two-dimensional code matrix and renders the two-dimensional code using a < rect > tag of SVG, including:
the Worker sub-matrix traverses each element of the two-dimensional code matrix, calculates the width and height of each module of the two-dimensional code according to the size of the two-dimensional code set by the size parameter of the two-dimensional code, and then circularly traverses the whole two-dimensional code matrix;
the < rect > tag comprises four parameters which respectively represent the distance between a rendering position and the left boundary and the upper boundary of the two-dimensional code and the length and the width of a rendering area;
< rect > the label is automatic to be analyzed to the element in the two-dimensional code matrix, uses black to render the two-dimensional code when the element in the two-dimensional code matrix is 1, then need not fill when the element in the two-dimensional code matrix is 0 and render.
8. The Web Workers-based multi-threaded two-dimensional code generation algorithm as recited in claim 7, wherein the termination of the Worker sub-thread is:
after the Worker sub-thread completes the calculation of the two-dimensional code matrix and the rendering of the two-dimensional code, the Worker sub-thread calls a close () method to immediately destroy the two-dimensional code.
CN202010667043.0A 2020-07-13 2020-07-13 Multithreading two-dimensional code generation algorithm based on Web Workers Withdrawn CN111797960A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010667043.0A CN111797960A (en) 2020-07-13 2020-07-13 Multithreading two-dimensional code generation algorithm based on Web Workers

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010667043.0A CN111797960A (en) 2020-07-13 2020-07-13 Multithreading two-dimensional code generation algorithm based on Web Workers

Publications (1)

Publication Number Publication Date
CN111797960A true CN111797960A (en) 2020-10-20

Family

ID=72808316

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010667043.0A Withdrawn CN111797960A (en) 2020-07-13 2020-07-13 Multithreading two-dimensional code generation algorithm based on Web Workers

Country Status (1)

Country Link
CN (1) CN111797960A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115203619A (en) * 2022-09-14 2022-10-18 杭州悦数科技有限公司 Web-based three-dimensional graph data layout method and system

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115203619A (en) * 2022-09-14 2022-10-18 杭州悦数科技有限公司 Web-based three-dimensional graph data layout method and system

Similar Documents

Publication Publication Date Title
CN105447546B (en) The two dimensional code and its coding and decoding methods and equipment of embeddable large scale figure
CN109857515B (en) Bridge communication method, device, equipment and computer readable storage medium
CN108985066B (en) Intelligent contract security vulnerability detection method, device, terminal and storage medium
CN110879937A (en) Method and device for generating webpage from document, computer equipment and storage medium
CA3190942A1 (en) Selecting locations for base-calling
CN112926061B (en) Plug-in processing method and device
CN110286917A (en) File packing method, device, equipment and storage medium
CN112507260A (en) Webpage loading method and device, electronic equipment and computer readable storage medium
CN109948762A (en) Method and apparatus for generating two dimensional code
CN111797960A (en) Multithreading two-dimensional code generation algorithm based on Web Workers
CN104915698B (en) Chemical producting safety information quick search and complete period tracking digital labelling system
CN107944525A (en) A kind of method and system of the expansion Quick Response Code capacity based on data segmentation
CN114218052B (en) Service interaction diagram generation method, device, equipment and storage medium
CN111181769A (en) Network topological graph drawing method, system, device and computer readable storage medium
CN102810065B (en) For the method and system of loader module
CN104424619B (en) Information processing equipment and information processing method
CN102486731A (en) Method, device and system for enhancing visualization of software call stack of software
CN111130554B (en) File compression method and device, electronic equipment and readable storage medium
US8827168B2 (en) Method for generating images from text
CN107291614A (en) File method for detecting abnormality and electronic equipment
CN113536762A (en) JSON text comparison method and device
CN113076440A (en) Map tile image processing method, device, equipment and storage medium
CN114491326A (en) Webpage loading state detection method and device, electronic equipment and storage medium
CN114398125B (en) Point nine effect graph generation method and related device thereof
CN113643082B (en) Invoice processing method and device based on artificial intelligence, computer equipment and medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
WW01 Invention patent application withdrawn after publication

Application publication date: 20201020

WW01 Invention patent application withdrawn after publication