This tutorial demonstrates how to read QR codes and barcodes from images and camera input using Python with OpenCV (cv2) and pyzbar libraries. The core process involves decoding the image or video frame to extract code data, then processing the results to identify the code type and content. For practical applications, the tutorial shows how to implement a validation system that tracks used codes to prevent unauthorized multiple uses, which is essential for event ticketing and access control systems.
Python QR & Barcode Reader: A Step-by-Step Tutorial
Added:hello and welcome in another python tutorial and in this one we are going to see how we can read qr codes and barcodes using python in fact the code that we are going to create is exactly the same regardless if we read qr codes or barcodes this tutorial is divided into three parts in the first part we're going to read these two images that contain qr code that are in the folder on the right side of the screen in the second part we're going to use our camera to read codes and in the third part we're going to put all this into practice through a short project so let's get started the first thing of course we need to import the necessary libraries and in this case it would be cv2 which will be used to read the image in the first part of the tutorial or later on let's say the camera or let's say video input and from pi set bar dot pi z bar we're going to import decode and this is basically what we're going to use to decode the code regardless of its qr code or barcode to get the content out of it so we have two images on the right side the first one is they're both png so the first one is just the tutorial and the second one the second one actually contains the first one and there's an extra one on the right side so img or image would be equal to cv to dot im or image read and in the brackets we are going to specify tutorial.png so if we print decode of ing so the content of the img this would be the outcome so we get a lot of information but as you can see it's a list so it is a list that contains one element and it contains the data so hello this is a qr code tutorial this is something that was created in the previous tutorial then we can see what kind of a code it is so this is specified oh this is a qr code then we have a form so it's a rectangular that's the width and height and we get the polygon or the outside points of the qr code if you're interested in that and if we have more than one qr code in one image or barcode well as you can see python can recognize that so this list now contains two elements so we have decoded that's the first one that stops here it has all the information that we just saw and we have the second one which is the second element of this list and you can check that if you just print the length of this list you it should be two so the the length of this list should be two now ideally when you're creating a script you want it to work regardless of the number of codes that you have in the image and otherwise you would have to keep changing that so what we're going to do is for code in the code of ing so that regardless of the image that we have we want to decode it and then for every code that you find there it could be just one it could be more than one we want to print code dot type so if you want to see if it's a qr code or not or barcode and then you would like to print code dot data now if you print it as it is it would work but the data should also be decoded using utf 8 otherwise you get this b letters and the string outside of it so you can see that these are both qr codes and we get the content so but let's also use decode and then utf-8 to get rid of those weird things in front or after it so now you can see that it clearly says okay so this is qr code this is the content of the first one and this is the second one at the content is basically a link to my youtube channel so this is how you would go into reading a qr code or multiple qr codes or barcodes from an image now let's continue and see how we would do that if we have to use our camera now the flow of information is actually quite similar instead of having an image well we would have a capture of our camera so cap would be equal to cv2 dot video capture and then in the brackets we have zero then what we can adjust here is the width and height of our camera so let's say that we would like to set our width so 3 is for width 640 pixels and number 3 width and then cap dot set number 4 is the height to 480 so number four is height then if we compare a camera to an image basically a camera is something that keeps recording it's an ongoing event compared to an image that's just a snapshot and what we want to do is we want to have let's say camera to be equal to true now basically if you're using a a qr code or a barcode scanner it has a button and this this camera would be basically false and it would turn through based on the whether the button is pressed or not but while camera is equal to true in this case we don't have a button so i'll just set it to to be true always but while it's true well it's equal equal to true basically what we want to do is something that we have here below already so for code in decode but we're not going to decode image what we're going to decode is basically the frame so we need to get the frame or what is being recorded by the camera now when we read this capture there are two information that we get we get success which is a boolean of one or a zero and we get the frame so cap dot read would be say stored in these two variables say success it would be just one or zero and frame is basically what we are interested in so for the code that you see for python sees in the code of frame what we want to do is well something that we just did already so we want to print let's say the type of the code and we want to decode that code using utf-8 if we run it as it is although it would work so the camera would turn on you would not be able to see it because we don't show it on the desktop if we want to do that well we're going to use i am show or image show the the window name would be just let's say testing code scan and then we would like to also display the frame and then two dot weight key i'm going to set it to one or at the frame after one millisecond you can change that as you prefer but let's run it as it is and see what is the outcome and on the right side i'm going to have this part which is the shell that would display the outcome of whatever we scan so this is the camera so as you can see it works what i have is a barcode i have this notebook that has a barcode and let's see what it now as you can see on the right side it shows the type of the code but it keeps running because every one millisecond it keeps getting scanned again and again so we have the type of the code and we have the content now even though this might be what you're looking for and it's it's a great way to well make use of this the third part of this tutorial is creating an a bit more of a project out of this so let's say that you have a concert that's about to happen or you have a sport event and this is not enough scanning the type of code and the content is not enough because well somebody might just create a fake code and well it would still be readable right or maybe more than one person would use the same code well that would again be um something that would not appear here so we need to make some checks on that how do we do that well let's start by creating a list of used codes this would prevent people from entering the event using the same code more than one so more than one person would not be able to enter the venue with the same code so how do we do that well what we want to do is for code in decode frame first we want to check if code dot data dot decode using utf-8 is not in our used codes if it's not in there then what we would like to say is approved you can enter so it's a brand new code it has not been used you can't enter we can also print i don't know if the type is is important i would not print it in this case but let's say we would still want to have the content and so we have the content of the code and then what we want to do is we want to used codes all right make sure it's you can see so use code.append and we want to append this code.data.decode so well it has been used already so now it is in the list it can no longer be used but what you also want to do is use time dot sleep for five seconds so you want that camera to not work for five seconds otherwise we get this spamming now not only that it's spamming but it's also going to be changed from being approved to else if the code is in used codes then we would like to print sorry this code has been already used now we don't want um to not have this time.sleep which means we need to also import time because otherwise it would convert really quickly from being approved to sorry this one code has been already used because it just takes one millisecond so we want that to happen so we want to have time.sleep for again for five seconds um you can also have else here but there is no other scenario that it can that can happen because the code is either in the used codes or not in used codes and let's give it a shot and just try to run this and see if this would work if we can use the same code more than once so the first thing is of course running the code you can actually take just any item that you have around you that should have a code so this is the first attempt so as you can see it says approved you can enter and as you can see the camera is has stopped for five seconds now we try to do the same thing again so i'm just let's see i did it let's see sorry this code has been already used so as you can see you can't use the same code more than once so we are sure that um this works that we that it would not be abused so not more than one person and if you have a let's say an event that has seats assigned then that's more difficult to be abused but if you have an event that has just a bunch of standing places with no seats then it can be of use so you want to make sure that this works and if you usually you would have valid codes that would be equal to not an empty list but it would contain all these um valid codes that people can use and before checking this part before checking the used codes basically you would have if this data decoded is in the valid codes and if it is a valid code that you would like to check if it's used code or not so that's the one thing that can be improved uh on the script and the second one is that you met you might want to add a timestamp and the reason for that is let's say that the person who is outside and scanning this code has just scanned the code but is not sure if if it was scanned or not um well basically if it tries to or she tries to scan it again then the outcome would be that it has been already used so maybe it's good to have yes sorry it has been used um let's say if it's ten seconds ago then that person knows that well it seems that i've scanned it already so it's more of a check for the person as well so you whenever you're writing a code you have to question is there something that i'm missing that can go wrong and just keep asking that question make sure that you cover all scenarios and then you're good to go so thank you for following this tutorial until the very end and see you in the next one
Up Next

OCaml Tutorial for Beginners: Setup on Windows with VS Code
@redirecttutorials7281
66.2K views•2018-04-02

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

FastAPI vs Flask vs Django: Choosing the Right Python Web Framework
@TechWithTim
302.5K views•2024-05-26

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies















!["[디지업] 위조, 도용 범죄를 차단할 완벽한 방법 l DGIST 로봇및기계전자공학과 김봉훈 교수 l EP_02"](https://i.ytimg.com/vi_webp/xuyN57T_3DQ/maxresdefault.webp)
![[2022 EBS 수능특강] 개념학습 2-1강 바코드 지문 분석 및 문제 풀이](https://i.ytimg.com/vi/KJ65UAvxzPM/maxresdefault.jpg)














![Como Criar uma Tela em Python Para Seus Códigos - [Interface Gráfica Intuitiva com Tkinter]](https://i.ytimg.com/vi/AiBC01p58oI/maxresdefault.jpg)







