memoの二维码分享WiFi

memoの二维码分享WiFi

用下面的网址就好了。
https://zxing.appspot.com/generator

不自己动手多没意思!借助python,自己生成一下。

二维码生成库

pip install qrcode,使用方法看这里。出于记录目的,贴下来:

Usage

From the command line, use the installed qr script:

qr "Some text" > test.png

Or in Python, use the make shortcut function:

import qrcode
img = qrcode.make('Some data here')

Advanced Usage

For more control, use the QRCode class. For example:

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('Some data')
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")

The version parameter is an integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21x21 matrix). Set to None and use the fit parameter when making the code to determine this automatically.

fill_color and back_color can change the background and the painting color of the QR, when using the default image factory.

The error_correction parameter controls the error correction used for the QR Code. The following four constants are made available on the qrcode package:

ERROR_CORRECT_L
About 7% or less errors can be corrected.
ERROR_CORRECT_M (default)
About 15% or less errors can be corrected.
ERROR_CORRECT_Q
About 25% or less errors can be corrected.
ERROR_CORRECT_H
About 30% or less errors can be corrected.

The box_size parameter controls how many pixels each "box" of the QR code is.

The border parameter controls how many boxes thick the border should be (the default is 4, which is the minimum according to the specs).

手机扫描wifi的格式

通用wifi的格式:WIFI:T:WPA;S:mynetwork;P:mypass;;
可以应付大多数情况了。如果有其他需求,详见这里。不过出于记录的目的,原文贴下来:

Parameter Example Description
T WPA Authentication type; can be WEP or WPA or WPA2-EAP, or nopass for no password. Or, omit for no password.
S mynetwork Network SSID. Required. Enclose in double quotes if it is an ASCII name, but could be interpreted as hex (i.e. "ABCD")
P mypass Password, ignored if T is nopass (in which case it may be omitted). Enclose in double quotes if it is an ASCII name, but could be interpreted as hex (i.e. "ABCD")
H true Optional. True if the network SSID is hidden. Note this was mistakenly also used to specify phase 2 method in releases up to 4.7.8 / Barcode Scanner 3.4.0. If not a boolean, it will be interpreted as phase 2 method (see below) for backwards-compatibility
E TTLS (WPA2-EAP only) EAP method, like TTLS or PWD
A anon (WPA2-EAP only) Anonymous identity
I myidentity (WPA2-EAP only) Identity
PH2 MSCHAPV2 (WPA2-EAP only) Phase 2 method, like MSCHAPV2

Order of fields does not matter. Special characters , ;, ,, " and : should be escaped with a backslash () as in MECARD encoding. For example, if an SSID was literally "foo;baraz" (with double quotes part of the SSID name itself) then it would be encoded like: WIFI:S:"foo;bar\baz";;

示例代码

下面是我的代码:

from qrcode import QRCode
import colorama

def GenerateYourWifi(
    type: str, ssid: str, psswd: str, outputPath: str = None, preview: bool = True
):
    content = f"WIFI:T:{type};S:{ssid};P:{psswd};;"
    c = QRCode()
    c.add_data(content)

    if preview:
        colorama.init()
        c.make()
        c.print_tty()

    if outputPath:
        c.make_image().save(outputPath)


if __name__ == "__main__":
    ssid = "my-sweet-home"
    psswd = "12345678"
    GenerateYourWifi("WPA", ssid, psswd)

其他有趣的东西

看这里

原文地址:https://www.cnblogs.com/daiday/p/14767755.html