laravel6でqrコードを生成して、safariからiphoneカメラ経由でURLを読み取って、リダイレクトしてみた。
1, url文字列をQRコードにして表示する
参考URL
https://qiita.com/nobuhiro-kobayashi/items/7d6d74df4ceb344f1647
1 2 |
# コマンドラインからインストール composer require simplesoftwareio/simple-qrcode |
config/app.phpに追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
'providers' => [ ... /* * Package Service Providers... */ // 追加!!! SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class, ], 'aliases' => [ ... // 追加!!! 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class, ], |
routes/web.phpに記述して、http://localhost/laravel6/public/qr でQRコードが表示された。簡単!
1 2 3 4 5 |
Route::get('qr', function() { $url = "https://www.google.co.jp"; $src = base64_encode(QrCode::format('png')->size(200)->generate($url)); return response('<img src="data:image/png;base64, ' . $src . '">'); }); |
2, safariブラウザからiphoneカメラを使って、QRコードを読み込んでリダイレクトする。
jsライブラリはjsqr.jsを使う。demoサイト( https://cozmo.github.io/jsQR/ )のHTMLに1行だけ追加して使う。なぜかchrome/firefoxだと動作しない(safariだけ動作した)
window.location.href = code.data; // 読み込んだURLへリダイレクト
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<html> <head> <meta charset="utf-8"> <title>jsQR Demo</title> <script src="./jsQR.js"></script> <link href="https://fonts.googleapis.com/css?family=Ropa+Sans" rel="stylesheet"> <style> body { font-family: 'Ropa Sans', sans-serif; color: #333; max-width: 640px; margin: 0 auto; position: relative; } #githubLink { position: absolute; right: 0; top: 12px; color: #2D99FF; } h1 { margin: 10px 0; font-size: 40px; } #loadingMessage { text-align: center; padding: 40px; background-color: #eee; } #canvas { width: 100%; } #output { margin-top: 20px; background: #eee; padding: 10px; padding-bottom: 0; } #output div { padding-bottom: 10px; word-wrap: break-word; } #noQRFound { text-align: center; } </style> </head> <body> <h1>jsQR Demo</h1> <a id="githubLink" href="https://github.com/cozmo/jsQR">View documentation on Github</a> <p>Pure JavaScript QR code decoding library.</p> <div id="loadingMessage">🎥 Unable to access video stream (please make sure you have a webcam enabled)</div> <canvas id="canvas" hidden></canvas> <div id="output" hidden> <div id="outputMessage">No QR code detected.</div> <div hidden><b>Data:</b> <span id="outputData"></span></div> </div> <script> var video = document.createElement("video"); var canvasElement = document.getElementById("canvas"); var canvas = canvasElement.getContext("2d"); var loadingMessage = document.getElementById("loadingMessage"); var outputContainer = document.getElementById("output"); var outputMessage = document.getElementById("outputMessage"); var outputData = document.getElementById("outputData"); function drawLine(begin, end, color) { canvas.beginPath(); canvas.moveTo(begin.x, begin.y); canvas.lineTo(end.x, end.y); canvas.lineWidth = 4; canvas.strokeStyle = color; canvas.stroke(); } // Use facingMode: environment to attemt to get the front camera on phones navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } }).then(function(stream) { video.srcObject = stream; video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen video.play(); requestAnimationFrame(tick); }); function tick() { loadingMessage.innerText = "⌛ Loading video..." if (video.readyState === video.HAVE_ENOUGH_DATA) { loadingMessage.hidden = true; canvasElement.hidden = false; outputContainer.hidden = false; canvasElement.height = video.videoHeight; canvasElement.width = video.videoWidth; canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height); var imageData = canvas.getImageData(0, 0, canvasElement.width, canvasElement.height); var code = jsQR(imageData.data, imageData.width, imageData.height, { inversionAttempts: "dontInvert", }); if (code) { drawLine(code.location.topLeftCorner, code.location.topRightCorner, "#FF3B58"); drawLine(code.location.topRightCorner, code.location.bottomRightCorner, "#FF3B58"); drawLine(code.location.bottomRightCorner, code.location.bottomLeftCorner, "#FF3B58"); drawLine(code.location.bottomLeftCorner, code.location.topLeftCorner, "#FF3B58"); outputMessage.hidden = true; outputData.parentElement.hidden = false; outputData.innerText = code.data; // 読み込んだURLへリダイレクト window.location.href = code.data; } else { outputMessage.hidden = false; outputData.parentElement.hidden = true; } } requestAnimationFrame(tick); } </script> </body> </html> |