用cmd写一个将输入内容打印成条码或者二维码,可以调整大小
时间: 2024-02-28 16:55:00 浏览: 31
您好,要实现将输入内容打印成条码或二维码,可以使用Python第三方库qrcode和barcode。
以下是使用Python编写的示例代码,可以在cmd中运行,将输入的内容生成二维码和条码,并保存为png图片文件。
```python
import argparse
import qrcode
import barcode
from barcode.writer import ImageWriter
parser = argparse.ArgumentParser(description='Generate QRCode and Barcode')
parser.add_argument('--content', '-c', required=True, help='Content to generate')
parser.add_argument('--type', '-t', default='qr', choices=['qr', 'code128', 'code39', 'ean13'], help='Type of code to generate')
parser.add_argument('--scale', '-s', default=5, type=int, help='Scale of the code')
parser.add_argument('--output', '-o', required=True, help='Output filename')
args = parser.parse_args()
if args.type == 'qr':
img = qrcode.make(args.content, box_size=args.scale)
img.save(args.output)
else:
cls = getattr(barcode, args.type)
img = cls(args.content, writer=ImageWriter(), add_checksum=False)
img.save(args.output, options={'dpi': 300, 'module_width': args.scale, 'module_height': args.scale})
print(f'{args.type.upper()} code generated and saved as {args.output}')
```
该程序支持以下命令行参数:
- `-c`或`--content`:要生成的条码或二维码的内容。
- `-t`或`--type`:要生成的类型,可以是`qr`、`code128`、`code39`或`ean13`,默认为`qr`。
- `-s`或`--scale`:条码或二维码的大小,数字越大生成的代码越大,默认为5。
- `-o`或`--output`:生成的图片文件名,必须以`.png`结尾。
您可以在cmd中运行以下命令,将输入的内容`Hello, World!`生成一个二维码,并保存为`qrcode.png`文件:
```
python qrcode-barcode.py -c "Hello, World!" -t qr -s 8 -o qrcode.png
```
您也可以生成一个Code128条码,并保存为`code128.png`文件:
```
python qrcode-barcode.py -c "12345" -t code128 -s 3 -o code128.png
```
希望这个示例代码可以帮助到您。