<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>React Native | Student Projects</title>
	<atom:link href="https://studentprojects.in/category/software-development/react-native/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Tue, 29 Nov 2022 12:02:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>React Native WebView based LightWeight QR Code Scanner</title>
		<link>https://studentprojects.in/software-development/react-native/react-native-advanced-solutions/react-native-webview-based-lightweight-qr-code-scanner/</link>
					<comments>https://studentprojects.in/software-development/react-native/react-native-advanced-solutions/react-native-webview-based-lightweight-qr-code-scanner/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Sun, 27 Nov 2022 16:43:59 +0000</pubDate>
				<category><![CDATA[React Native Advanced Solutions]]></category>
		<category><![CDATA[WebView]]></category>
		<category><![CDATA[QR Code Scanner]]></category>
		<category><![CDATA[React Native]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=9861</guid>

					<description><![CDATA[<p>There are many recourses available on the net on how to integrate likes of react-native-qrcode-scanner package into your app. Although that is a straightforward and quick way to implement the QR code scanner, such packages require multiple dependencies like the React-Native Camera package to scan a QR code and are bulky both size and performance-wise.</p>
<p>The post <a href="https://studentprojects.in/software-development/react-native/react-native-advanced-solutions/react-native-webview-based-lightweight-qr-code-scanner/">React Native WebView based LightWeight QR Code Scanner</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-full"><img decoding="async" width="1000" height="323" src="https://studentprojects.in/wp-content/uploads/2022/11/ScanQRCode.jpg" alt="" class="wp-image-9870" srcset="https://studentprojects.in/wp-content/uploads/2022/11/ScanQRCode.jpg 1000w, https://studentprojects.in/wp-content/uploads/2022/11/ScanQRCode-300x97.jpg 300w, https://studentprojects.in/wp-content/uploads/2022/11/ScanQRCode-768x248.jpg 768w" sizes="(max-width: 1000px) 100vw, 1000px" /></figure>



<p>There are many recourses available on the net on how to integrate likes of <a href="https://www.npmjs.com/package/react-native-qrcode-scanner" target="_blank" rel="noreferrer noopener">react-native-qrcode-scanner</a> package into your app. Although that is a straightforward and quick way to implement the QR code scanner, such packages require multiple dependencies like the React-Native Camera package to scan a QR code and are bulky both size and performance-wise. Your apk size will suddenly increase by a minimum of 15MB when you import these two packages. That is too much!</p>



<h2>Our Solution</h2>



<p>Can you imagine you can build a QR Code Scanner on a React-Native app without using <strong>React-Native Camera</strong> and <strong>react-native-qrcode-scanner </strong>packages?</p>



<p>Yes. Use the simple HTML QR Code scanner and open it using a WebView. That&#8217;s it!</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="427" height="495" src="https://studentprojects.in/wp-content/uploads/2022/11/SP_HTML_QRCode_Scan.png" alt="" class="wp-image-9875" srcset="https://studentprojects.in/wp-content/uploads/2022/11/SP_HTML_QRCode_Scan.png 427w, https://studentprojects.in/wp-content/uploads/2022/11/SP_HTML_QRCode_Scan-259x300.png 259w" sizes="(max-width: 427px) 100vw, 427px" /></figure></div>


<p><a href="https://codesdoc.com/demo/qrcodescanner" target="_blank" rel="noreferrer noopener">Here&#8217;s a demo</a> of a QR code scanner that works on mobile phones. To scan a QR code, all you need is a camera and a QR code. Use the below-given QR code to scan.&nbsp;</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="300" height="300" src="https://studentprojects.in/wp-content/uploads/2022/11/studentprojects_qr_code.png" alt="" class="wp-image-9874" srcset="https://studentprojects.in/wp-content/uploads/2022/11/studentprojects_qr_code.png 300w, https://studentprojects.in/wp-content/uploads/2022/11/studentprojects_qr_code-150x150.png 150w, https://studentprojects.in/wp-content/uploads/2022/11/studentprojects_qr_code-24x24.png 24w, https://studentprojects.in/wp-content/uploads/2022/11/studentprojects_qr_code-48x48.png 48w, https://studentprojects.in/wp-content/uploads/2022/11/studentprojects_qr_code-96x96.png 96w" sizes="(max-width: 300px) 100vw, 300px" /></figure></div>


<h2>Let&#8217;s Start Coding</h2>



<p>Here we need HTML, JavaScript and a CSS file. First, we will create an index.html.</p>



<h3>Creating an HTML index file</h3>



<pre class="wp-block-code"><code lang="markup" class="language-markup">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;QR Code Scanner&lt;/title&gt;
    &lt;meta charset="UTF-8" /&gt;
    &lt;meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /&gt;
    &lt;link rel="stylesheet" href="src/styles.css" /&gt;
    &lt;script src="https://rawgit.com/sitepoint-editors/jsqrcode/master/src/qr_packed.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="container"&gt;
      &lt;a id="btn-scan-qr"&gt;
        &lt;img src="./src/scan.jpg"&gt;&lt;br/&gt;
        &lt;button&gt;Scan a QR code&lt;/button&gt;  
      &lt;/a&gt;
      &lt;canvas hidden="" id="qr-canvas"&gt;&lt;/canvas&gt;
      &lt;div id="qr-result" hidden=""&gt;
        &lt;b&gt;Result:&lt;/b&gt; &lt;span id="outputData"&gt;&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;script src="./src/qrCodeScanner.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>



<p>Here canvas element is added to show the camera view. It also includes a QR code scanner JavaScript file and a CSS file. output data id will print the scanned output. </p>



<h3>Linking a QR code scanner library</h3>



<p>QR code requires a JavaScript library to scan a QR code. As it will be hard to write our own, we use a readily available one. </p>



<p>There is a famous &#8216;<strong>JavaScript QRCode reader for HTML5 enabled browser</strong>&#8216; by <a href="https://github.com/LazarSoft/jsqrcode" target="_blank" rel="noreferrer noopener">LazarSoft</a>. There are 17 files in it. So we will use the packed version of it developed by  Sitepoint.</p>



<pre class="wp-block-code"><code lang="markup" class="language-markup">&lt;script src="https://rawgit.com/sitepoint-editors/jsqrcode/master/src/qr_packed.js"&gt;&lt;/script&gt;</code></pre>



<h3>Creating a JavaScript file</h3>



<p>On the index.html file, I have linked a QRCodeScanner.js file as shown in below code block.</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">&lt;script src="./src/QRCodeScanner.js"&gt;&lt;/script&gt;</code></pre>



<p>Here is the content of this file. </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">qrcode = window.qrcode;
const video = document.createElement("video");
const canvasElement = document.getElementById("qr-canvas");
const canvas = canvasElement.getContext("2d");
const qrResult = document.getElementById("qr-result");
const outputData = document.getElementById("outputData");
const btnScanQR = document.getElementById("btn-scan-qr");
let scanning = false;
qrcode.callback = res =&gt; {
  if (res) {
    scanning = false;
    outputData.innerText = res;
    video.srcObject.getTracks().forEach(track =&gt; {
      track.stop();
    });
    qrResult.hidden = false;
    canvasElement.hidden = true;
    btnScanQR.hidden = false;
    window.open(res,"_self")
  }
};
btnScanQR.onclick = () =&gt; {
  navigator.mediaDevices
    .getUserMedia({ video: { facingMode: "environment" } })
    .then(function(stream) {
      scanning = true;
      qrResult.hidden = true;
      btnScanQR.hidden = true;
      canvasElement.hidden = false;
      video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen
      video.srcObject = stream;
      video.play();
      tick();
      scan();
    });
};
function tick() {
  canvasElement.height = video.videoHeight;
  canvasElement.width = video.videoWidth;
  canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);
  scanning &amp;&amp; requestAnimationFrame(tick);
}
function scan() {
  try {
    qrcode.decode();
  } catch (e) {
    setTimeout(scan, 300);
  }
}</code></pre>



<p>First, we will get the qrcode object from the window and then the code related to the camera to handle the video and image. Next, assign the elements to const variables. </p>



<p>Implemented callback and button onclick methods. On the callback function, as soon as the scanner reads a URL, window.open() function will open the URL. Also, the scanned URL will be printed on the screen using the id outputData. </p>



<h3>Creating a Stylesheet</h3>



<p>I have linked a style.css to index.html as shown below</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">&lt;link rel="stylesheet" href="src/styles.css" /&gt;</code></pre>



<p>Here are the contents of the style.css file </p>



<pre class="wp-block-code"><code lang="css" class="language-css">body {
  font-family: sans-serif;
  padding: 0 10px;
  height: 100%;
  background: white;
  margin: 0;
}
#container {
  text-align: center;
  margin: 0;
}
#qr-canvas {
  margin: auto;
  width: calc(100% - 20px);
  max-width: 400px;
}
#btn-scan-qr {
  cursor: pointer;
}
#btn-scan-qr img {
  padding: 15px;
  margin-top: 15px;
  background: white;
  width: 300px;
}
button {
  background-color: #21353e;
  border: none;
  border-radius: 5px;
  font-weight: 700;
  color: #fff;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}</code></pre>



<h3>Test the QR code scanner before we jump to react native code. </h3>



<p>Now open the index.html file on the mobile browser. On clicking on the scan QR code button, it will ask for the camera permission.  Once you provide permission, the camera should open. Now scan any QR code with a URL. After the successful scan browser should redirect to the new URL. </p>



<p>Great! If you are facing any trouble, then download the complete code here.  </p>



<h3>Link it from React Native App</h3>



<p>Now open this HTML QR code scanner in a WebView on your React Native app. Create a component and import WebView from react-native-webview package. Pass the QR code scanner URL to WebView.</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">return (
  &lt;View style={styles.flex1}&gt;
    &lt;WebView
      source={{uri: 'https://studentprojects.in/demo/qrcodescanner/'}}
      startInLoadingState={true}
      onShouldStartLoadWithRequest={event =&gt; {
        if (event.url !== 'https://studentprojects.in/demo/qrcodescanner/') {
          console.log('onShouldStartLoadWithRequest', event.url);
          // Write your code here.
          return false;
        }
        return true;
      }}
    /&gt;
  &lt;/View&gt;
);</code></pre>



<p>Here the challenge is to get the scanned URL on the React Native code from the WebView. This can be achieved using <strong>onShouldStartLoadWithRequest()</strong>.</p>



<p>So it is mandatory that you open the URL on a successful QR code scan so that you can catch it using <strong>onShouldStartLoadWithRequest()</strong> and cancel the loading of that URL by returning &#8216;return false&#8217;. </p>



<p>console log should print the scanned URL. You can write your code once you get the URL. </p>



<p>If you have any problems putting this together, please leave a comment.</p><p>The post <a href="https://studentprojects.in/software-development/react-native/react-native-advanced-solutions/react-native-webview-based-lightweight-qr-code-scanner/">React Native WebView based LightWeight QR Code Scanner</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/react-native/react-native-advanced-solutions/react-native-webview-based-lightweight-qr-code-scanner/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
