1.二维码搭建, fastapi,qrcode,python-barcode
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import qrcode
from io import BytesIO
from fastapi.responses import StreamingResponse
import barcode
from barcode.writer import ImageWriter
app = FastAPI()
@app.get("/generate_qr")
async def generate_qr(data: str = ""):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img_io = BytesIO()
img.save(img_io, 'PNG')
img_io.seek(0)
return StreamingResponse(img_io, media_type="image/png")
@app.get("/generate_barcode")
async def generate_barcode(data: str = "123456789012"):
if not data:
raise HTTPException(status_code=400, detail="Data parameter is required")
# 选择条形码格式,例如EAN-13
#ean = barcode.get_barcode_class('ean13')
# 选择条形码格式(Code 39)
code39 = barcode.get_barcode_class('code39')
try:
#ean_barcode = ean(data, writer=ImageWriter())
code39_barcode = code39(data, writer=ImageWriter())
except barcode.errors.BarcodeError:
raise HTTPException(status_code=400, detail="Invalid data for EAN-13 barcode")
img_io = BytesIO()
#ean_barcode.write(img_io)
code39_barcode.write(img_io)
img_io.seek(0)
return StreamingResponse(img_io, media_type="image/png")
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='localhost', port=8000)
2.地址生成
http://localhost:8000/generate_qr
3.SSRS配置