Last active
September 26, 2015 13:07
-
-
Save m0n5t3r/1101896 to your computer and use it in GitHub Desktop.
read URLs encoded as QR codes, open browser; Linux, of course; needs opencv, pygame (for display), zbar for actual decoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Copyright 2013 Sabin Iacob <iacobs@gmail.com> | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
import pygame | |
from pygame.locals import QUIT, KEYDOWN | |
import sys | |
import os | |
import re | |
URL_RE = re.compile(r'((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)') | |
import opencv | |
from opencv import highgui | |
import zbar | |
def get_image(camera): | |
im = highgui.cvQueryFrame(camera) | |
return opencv.adaptors.Ipl2PIL(im) | |
def get_code(img): | |
w, h = img.size | |
zimg = zbar.Image(w, h, 'Y800', img.convert('L').tostring()) | |
proc = zbar.ImageScanner() | |
if proc.scan(zimg): | |
[sym] = list(zimg.symbols) | |
return sym.data | |
if __name__ == '__main__': | |
camera = highgui.cvCreateCameraCapture(0) | |
fps = 15.0 | |
pygame.init() | |
window = pygame.display.set_mode((640,480)) | |
pygame.display.set_caption("Get QR Code") | |
screen = pygame.display.get_surface() | |
while True: | |
events = pygame.event.get() | |
for event in events: | |
if event.type == QUIT or event.type == KEYDOWN: | |
sys.exit(0) | |
im = get_image(camera) | |
code = get_code(im) | |
if code: | |
highgui.cvReleaseCapture(camera) | |
match = re.search(URL_RE, code) | |
if match: | |
os.system('xdg-open "%s"' % match.group(0)) | |
else: | |
os.system('zenity --info --text="%s"' % re.escape(code)) | |
break | |
pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) | |
screen.blit(pg_img, (0,0)) | |
pygame.display.flip() | |
pygame.time.delay(int(1000 * 1.0/fps)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment