-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (49 loc) · 1.85 KB
/
main.py
File metadata and controls
64 lines (49 loc) · 1.85 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
from flask import Flask, render_template, request, send_file
import qrcode
import io
import base64
import os
app = Flask(__name__)
# 一時保存用のQRコードファイルパス
TEMP_FILE_PATH = "static/qr_code.png"
def generate_qr(data):
"""QRコードを生成し、Base64形式で返す"""
if not data:
return None, "⚠️ 入力が空です。QRコードを生成できません。"
try:
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")
# `static` フォルダを作成して画像を保存
os.makedirs("static", exist_ok=True)
img.save(TEMP_FILE_PATH, format="PNG")
# Base64エンコードしてHTMLで表示
img_io = io.BytesIO()
img.save(img_io, format="PNG")
img_io.seek(0)
img_base64 = base64.b64encode(img_io.getvalue()).decode("utf-8")
return img_base64, None
except Exception as e:
return None, f"⚠️ QRコード生成エラー: {e}"
@app.route("/", methods=["GET", "POST"])
def index():
qr_image = None
error_message = None
if request.method == "POST":
data = request.form.get("qr_data", "").strip()
qr_image, error_message = generate_qr(data)
return render_template("index.html", qr_code=qr_image, error=error_message)
@app.route("/download")
def download_qr():
"""QRコードをダウンロード"""
if os.path.exists(TEMP_FILE_PATH):
return send_file(TEMP_FILE_PATH, mimetype="image/png", as_attachment=True, download_name="qr_code.png")
return "⚠️ QRコードが生成されていません。", 400
if __name__ == "__main__":
app.run(debug=True)