-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (82 loc) · 2.48 KB
/
main.py
File metadata and controls
91 lines (82 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from base64 import b64encode
from io import BytesIO
from fastapi import FastAPI, UploadFile, responses
from PIL import Image
import zxingcpp
app = FastAPI()
@app.post("/read")
async def read(
file: UploadFile,
response: responses.Response,
format: str = "",
formats: str = "",
try_rotate: bool = True,
try_downscale: bool = True,
pure: bool = False,
):
try:
barcode_formats = (
zxingcpp.barcode_formats_from_str(formats)
if formats
else zxingcpp.barcode_format_from_str(format)
)
except ValueError as e:
response.status_code = 400
return {"error": str(e)}
results = zxingcpp.read_barcodes(
Image.open(file.file),
formats=barcode_formats,
try_rotate=try_rotate,
try_downscale=try_downscale,
is_pure=pure,
)
return [
{
"base64": b64encode(result.bytes),
"content_type": result.content_type.name,
"format": result.format.name,
"orientation": result.orientation,
"position": {
(result.position.top_left.x, result.position.top_left.y),
(result.position.top_right.x, result.position.top_right.y),
(result.position.bottom_right.x, result.position.bottom_right.y),
(result.position.bottom_left.x, result.position.bottom_left.y),
},
"text": result.text,
"valid": result.valid,
}
for result in results
]
@app.get(
"/write/{format}",
responses={200: {"content": {"image/png": {}}}},
response_class=responses.Response,
)
async def write(
format: str,
text: str,
response: responses.Response,
width: int = 0,
height: int = 0,
quiet_zone: int = -1,
ec_level: int = 0,
):
try:
barcode_format = zxingcpp.barcode_format_from_str(format)
if barcode_format == zxingcpp.BarcodeFormat.NONE:
raise ValueError("This is not a valid barcode format: {}".format(format))
except ValueError as e:
response.status_code = 400
return responses.JSONResponse({"error": str(e)})
file = BytesIO()
result = zxingcpp.write_barcode(
format=barcode_format,
text=text,
width=width,
height=height,
quiet_zone=quiet_zone,
ec_level=ec_level,
)
Image.fromarray(result).save(file, "PNG")
file.seek(0)
return responses.Response(content=file.read(), media_type="image/png")