SoFunction
Updated on 2025-04-04

VUE html5-qrcode implements H5 scan function example

Official Documentationhtml5-qrcode

Install npm i html5-qrcode

1. Create a new component

<div class="qrcode">
		<div ></div>
	</div>
//style.qrcode{
		    position: relative;
			height: 100%;
			width: 100%;
		  	background: rgba($color: #000000, $alpha: 0.48);
}
#reader{
		  top: 50%;
		  left: 0;
		  transform: translateY(-50%);
}

2. Introduce

import { Html5Qrcode } from "html5-qrcode";

3. Get camera permissions and call created

getCameras() {
			      ()
			        .then((devices) =&gt; {
			          if (devices &amp;&amp; ) {
						this.html5QrCode = new Html5Qrcode("reader");
			            ();//Scan the code			          }
			        })
			        .catch((err) =&gt; {
			          // handle err
					  this.html5QrCode = new Html5Qrcode("reader");
					   = "ERROR: You need to grant camera access"
					  this.$emit("err",)
			        });
},

4. Get scan code content

start() {
			      //Environment rear user front			      this.html5QrCode 
			        .start(
			           {facingMode: "environment" },
			          {
			            fps: 2,//Scan once in 500 milliseconds			            qrbox: { width: 250, height: 250 }, 
			          },
			          (decodedText, decodedResult) =&gt; {
						  this.$emit("ok",decodedText)
			          }
			        )
			        .catch((err) =&gt; {
			          (`Unable to start scanning, error: ${err}`);
			        });
},

5. The scan function must be turned off before destroying the page, otherwise an error will be reported. Could not start video source

//Called before destructionbeforeDestroy() {
		    ();
}
//Close Scan the codestop() {
			      this.().then((ignore) =&gt; {
			          // QR Code scanning is stopped.
			          ("QR Code scanning stopped.");
			        })
			        .catch((err) =&gt; {
			          // Stop failed, handle it.
			          ("Unable to stop scanning.");
			        });
},

6. Refer to components on the scan code page

&lt;BarScan ref="qrcode" @ok="getResult" @err="geterror" &gt;&lt;/BarScan&gt;
getResult(e){
    //e: Scan the QR code content}
geterror(e){
    //e: Report error content}

Component full code

&lt;template&gt;
    &lt;div class="qrcode"&gt;
		&lt;div &gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
	import { Html5Qrcode } from "html5-qrcode";
	export default {
		created() {
			()
		},
		beforeDestroy() {
		    ();
		},
		methods:{
			getCameras() {
			      ()
			        .then((devices) =&gt; {
			          if (devices &amp;&amp; ) {
						this.html5QrCode = new Html5Qrcode("reader");
			            ();
			          }
			        })
			        .catch((err) =&gt; {
			          // handle err
					  this.html5QrCode = new Html5Qrcode("reader");
					   = "ERROR: You need to grant camera access"
					  this.$emit("err",)
			        });
			    },
			 start() {
			      //Environment rear user front			      this.html5QrCode 
			        .start(
			           {facingMode: "environment" },
			          {
			            fps: 2,
			            qrbox: { width: 250, height: 250 }, 
			          },
			          (decodedText, decodedResult) =&gt; {
						  this.$emit("ok",decodedText)
			          }
			        )
			        .catch((err) =&gt; {
			            this.$emit("err",err)
			        });
			    },
			    stop() {
			      this.().then((ignore) =&gt; {
			          // QR Code scanning is stopped.
			          ("QR Code scanning stopped.");
			        })
			        .catch((err) =&gt; {
			          // Stop failed, handle it.
			          ("Unable to stop scanning.");
			        });
			    },
		}
	}
&lt;/script&gt;
&lt;style lang="scss" scoped&gt;
	.qrcode{
		    position: relative;
			height: 100%;
			width: 100%;
		  	background: rgba($color: #000000, $alpha: 0.48);
	}
	#reader{
		  top: 50%;
		  left: 0;
		  transform: translateY(-50%);
	}
&lt;/style&gt;

Reference component page

<BarScan ref="qrcode" @ok="getResult" @err="geterror" ></BarScan>
import BarScan from '@/components/'
	var _self;
	export default {
		components:{
			BarScan
		},
        methods:{
            getResult(result){
			      =result;
			},
			geterror(e){
				  this.$toast(e)
			},}
}

Summarize

This is the article about VUE html5-qrcode implementing H5 scanning function. For more related VUE html5-qrcode H5 scanning content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!