LoginSignup
1
1

(Python)今更すぎるQRコード生成

Posted at

はじめに

思い立って即興で作ったので備忘録として投げとく
画面のボタン押す→ポップアップしてQRコード出てくる
って感じで

コード

画面

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>テスト</title>
</head>
<body>

<button onclick="openPopup()">表示</button>

<script type="text/javascript">

function openPopup() {
	let option = 'width=300,height=300,left=200,top=200';
	let newwin = open('qr.py','mywindow', option);
}
</script>

</body>
</html>

ボタンが付いてるだけ
もはやスクショ張る意味あるんか...?
スクリーンショット 2023-12-11 000332.png

Python

qr.py
#!/usr/local/bin/python3.9

import pyqrcode
import sys
import tempfile

class QR:

	def __init__(t):

		#qrコード生成
		t.qr = pyqrcode.create('https://www.google.com/')
		t.make()

	def make(t):

		#tmpDIR内で作業
		with tempfile.TemporaryDirectory() as td:
			#配置、大きさはお好みで
			t.qr.png(td + 'test.png', scale=5)
			#バイナリモードで読み込んで取得
			with open(td + 'test.png', 'rb') as f:
				b = f.read()
		QR.out(b)

	def out(t):

		#出力
		print('Content-Type:image/png')
		print('Content-Disposition: inline; filename="test.png"')
		print("")
		sys.stdout.flush()
		sys.stdout.buffer.write(t)
		exit()

#インスタンス化
QR()

ボタン押すとこんな感じ
スクリーンショット 2023-12-11 000227.png

所見

・そもそもpyqrcodeモジュールの選定って妥当なのか
・一時ディレクトリでの作業、もうちょっとスマートにできなかったのかね

参考文献

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1