Post

아이폰 카메라, 공유하기 버튼

아이폰 카메라, 공유하기 버튼

카메라 띄우기

1
2
3
4
5
6
7
8
<div class="jsQR">
  <div id="loadingMessage">🎥 비디오 스트림에 액세스할 수 없습니다.<br>(웹캠이 활성화되어 있는지 확인하십시오).</div>
  <canvas id="canvas" hidden></canvas>
  <div id="output" hidden>
    <div id="outputMessage">잘못된 QR코드입니다.</div>
    <div hidden><b>Data:</b> <span id="outputData"></span></div>
  </div>
</div>
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
// var video = document.createElement("video");
var video = document.getElementById("video");
// $("#wrap").append(video);
// console.log('1', 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");

// let videoLive = true;

const qrcodeEl = document.querySelector('#qrcode');
const downloadEl = document.querySelector('#download');
$(function() {
  qrcodeEl.innerHTML = ``;
  new QRCode(qrcodeEl, {
    text:`<?=session()->get('id')?>`,
    width: '256',
    height: '256'
  });
});


// $("#testbtn").on("click", function(e){
//     e.preventDefault();
//     console.log('dd');
//     video.play();
//     requestAnimationFrame(tick);
// });

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
// function playVideo(){
// { video: true, audio: true }
navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } }).then(function(stream) {
  // track = stream.getTracks()[0];
  video.srcObject = stream;
  // video.setAttribute("playsinline", false); // required to tell iOS safari we don't want fullscreen
  // video.setAttribute("muted", true); // required to tell iOS safari we don't want fullscreen
  video.play();
  requestAnimationFrame(tick);
})
.catch(function(error){
  alert(error);
});
// }
// playVideo();

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",
    });

    // alert(imageData);

    if (code) {
      drawLine(code.location.topLeftCorner, code.location.topRightCorner, "#00c6ff");
      drawLine(code.location.topRightCorner, code.location.bottomRightCorner, "#00c6ff");
      drawLine(code.location.bottomRightCorner, code.location.bottomLeftCorner, "#00c6ff");
      drawLine(code.location.bottomLeftCorner, code.location.topLeftCorner, "#00c6ff");
      outputMessage.hidden = true;
      outputData.parentElement.hidden = false;
      outputData.innerText = code.data;

      let $data = code.data;
      let regExpPhone = /^01([0|1|6|7|8|9])-?([0-9]{3,4})-?([0-9]{4})$/;

      if( regExpPhone.test($data) ){                    
        location.href = `/mobile/pin/pin_send?send_id=${$data}`;
      }

    } else {
      outputMessage.hidden = false;
      outputData.parentElement.hidden = true;
    }
  }

  requestAnimationFrame(tick);
}

// preCapture(container, onSuccess) {
//     const canvas = this.createCanvas(),
//         context = canvas.getContext("2d");
//     context.drawImage(container, 0, 0);
//     this.clearCanvas(canvas);

//     setTimeout(() => {
//         onSuccess(container);
//     }, 1000);
// }

공유하기

1
<span class="qr_share"><a href="#">공유</a></span>
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
$(document).on("click", ".qr_share", function(e){
    e.preventDefault();
    shareCanvas();
})


async function shareCanvas() {
  // const canvasElement = document.querySelector('canvas');
  const canvasElement = await html2canvas(
    document.querySelector("#qrcode img"),
    {
      scale: 1, // 줌 비율, 기본값으로 1
      width:'195',
      height:'195',
    }
  );
  const dataUrl = canvasElement.toDataURL();
  const blob = await (await fetch(dataUrl)).blob();
  const filesArray = [
    new File(
      [blob],
      'qrcode.png',
      {
        // type: blob.type,
        type: "image/png",
        lastModified: new Date().getTime()
      }
    )
  ];
  console.log(filesArray);
  const shareData = {
    files: filesArray,
    title : "PinKey",
    text : "PinKey QRcode 공유",
  };
  navigator.share(shareData);
}
This post is licensed under CC BY 4.0 by the author.