# Path: bot/hardware/qr_code.py
# class QRCode(object):
#     """ Represents a QR code.
#     Locations are not accurate.
#     Useful properties:
#         self.value: the value of the code.
#         self.points: numpy 
#         self.X: X value of center
#         self.Y: Y value of center
# 
#         """
# 
#     def __init__(self, symbol, size):
# 
#         # Raw object from zbar.
#         self.symbol = symbol        
#         self.value = self.symbol.data
# 
#         # Begin Processing symbol
#         self.topLeft, self.bottomLeft, self.bottomRight, self.topRight = [item for item in self.symbol.location]
# 
#         self.points = np.float32(symbol.location)
#         self.set_centroid_location()
# 
#         l = size
#         self.verts = np.float32([[-l/2, -l/2, 0],
#                                  [-l/2,  l/2, 0],
#                                  [l/2,  -l/2, 0],
#                                  [l/2,   l/2, 0]])
#         self.rvec = [999]*3
#         self.tvec = [999]*3
#         self.displacement = []
#         self.displacement_2d = []
#         
#     def __str__(self):
#         return "value: %s \n Location: %s \n Verts: %s" % \
#                 (self.value, self.centroid_location, self.points)
# 
#     @property
#     def tvec(self):
#         return self.__tvec
# 
#     @tvec.setter
#     def tvec(self, vec):
#         self.__tvec = vec
#         self.displacement_2d = sqrt(vec[0]**2 + vec[1]**2)
#         self.displacement = sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
# 
#     @property
#     def points(self):
#         return self.__points
# 
#     @points.setter
#     def points(self, value):
#         """Numpy array of corners of QR code. """
#         self.__points = np.float32(value)
#  
#     def set_centroid_location(self):
#         x_sum = 0
#         y_sum = 0
#         # calculate center from x and y of points
#         for item in self.symbol.location:
#             x_sum += item[0]
#             y_sum += item[1]
#         X = x_sum / 4
#         Y = y_sum /4
#         self.centroid_location =  (X, Y)
# 
# Path: bot/hardware/complex_hardware/QRCode2.py
# class QRCode2(object):
#     def __init__(self, tvec, value, top_right):
#         self.tvec = tvec
#         self.value = value
#         self.tr = top_right
# 
# Path: bot/hardware/complex_hardware/QRCode2.py
# class Block(object):
#     def __init__(self, size, color):
#         self.size = size
#         self.color = color


import numpy as np
import time
import subprocess
import zbar
import cv2
import cv2
import math
import bot.lib.lib as lib
from PIL import Image
from PIL import ImageEnhance
from threading import Thread
from bot.hardware.qr_code import QRCode
from bot.hardware.complex_hardware.QRCode2 import QRCode2
from bot.hardware.complex_hardware.QRCode2 import Block
from bot.hardware.complex_hardware.partial_qr import *
    @lib.api_call
    def get_current_frame(self):
        self.cam.grab()        
        self.cam.grab()        
        self.cam.grab()        
        self.cam.grab()       
        return self.cam.read()
 
    def get_zbar_im(self, qr_frame):
        cv2.imwrite('buffer.png', qr_frame)
        pil_im = Image.open('buffer.png').convert('L')
        width, height = pil_im.size
        raw = pil_im.tobytes()

        return zbar.Image(width, height, 'Y800', raw)

    @lib.api_call
    def get_qr_list(self, frame):
        """Recieves frame, assuming it's already been sanitizes, 
        and returns a list of all qr codes in it.
        """
 
        z_im = self.get_zbar_im(frame)
        self.scanner.scan(z_im)
        qr_list = []
        # Gather list of all QR objects
        for symbol in z_im:
            print "qr found"
            tl, bl, br, tr = [item for item in symbol.location]
            points = np.float32(np.float32(symbol.location))

# Path: bot/hardware/qr_code.py
# class QRCode(object):
#     """ Represents a QR code.
#     Locations are not accurate.
#     Useful properties:
#         self.value: the value of the code.
#         self.points: numpy 
#         self.X: X value of center
#         self.Y: Y value of center
# 
#         """
# 
#     def __init__(self, symbol, size):
# 
#         # Raw object from zbar.
#         self.symbol = symbol        
#         self.value = self.symbol.data
# 
#         # Begin Processing symbol
#         self.topLeft, self.bottomLeft, self.bottomRight, self.topRight = [item for item in self.symbol.location]
# 
#         self.points = np.float32(symbol.location)
#         self.set_centroid_location()
# 
#         l = size
#         self.verts = np.float32([[-l/2, -l/2, 0],
#                                  [-l/2,  l/2, 0],
#                                  [l/2,  -l/2, 0],
#                                  [l/2,   l/2, 0]])
#         self.rvec = [999]*3
#         self.tvec = [999]*3
#         self.displacement = []
#         self.displacement_2d = []
#         
#     def __str__(self):
#         return "value: %s \n Location: %s \n Verts: %s" % \
#                 (self.value, self.centroid_location, self.points)
# 
#     @property
#     def tvec(self):
#         return self.__tvec
# 
#     @tvec.setter
#     def tvec(self, vec):
#         self.__tvec = vec
#         self.displacement_2d = sqrt(vec[0]**2 + vec[1]**2)
#         self.displacement = sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
# 
#     @property
#     def points(self):
#         return self.__points
# 
#     @points.setter
#     def points(self, value):
#         """Numpy array of corners of QR code. """
#         self.__points = np.float32(value)
#  
#     def set_centroid_location(self):
#         x_sum = 0
#         y_sum = 0
#         # calculate center from x and y of points
#         for item in self.symbol.location:
#             x_sum += item[0]
#             y_sum += item[1]
#         X = x_sum / 4
#         Y = y_sum /4
#         self.centroid_location =  (X, Y)
# 
# Path: bot/hardware/complex_hardware/QRCode2.py
# class QRCode2(object):
#     def __init__(self, tvec, value, top_right):
#         self.tvec = tvec
#         self.value = value
#         self.tr = top_right
# 
# Path: bot/hardware/complex_hardware/QRCode2.py
# class Block(object):
#     def __init__(self, size, color):
#         self.size = size
#         self.color = color


# Path: bot/hardware/complex_hardware/camera_reader.py
import numpy as np
import time
import subprocess
import zbar
import cv2
import cv2
import math
import bot.lib.lib as lib
from PIL import Image
from PIL import ImageEnhance
from threading import Thread
from bot.hardware.qr_code import QRCode
from bot.hardware.complex_hardware.QRCode2 import QRCode2
from bot.hardware.complex_hardware.QRCode2 import Block
from bot.hardware.complex_hardware.partial_qr import *

    @lib.api_call
    def get_current_frame(self):
        self.cam.grab()        
        self.cam.grab()        
        self.cam.grab()        
        self.cam.grab()       
        return self.cam.read()
 
    def get_zbar_im(self, qr_frame):
        cv2.imwrite('buffer.png', qr_frame)
        pil_im = Image.open('buffer.png').convert('L')
        width, height = pil_im.size
        raw = pil_im.tobytes()

        return zbar.Image(width, height, 'Y800', raw)

    @lib.api_call
    def get_qr_list(self, frame):
        """Recieves frame, assuming it's already been sanitizes, 
        and returns a list of all qr codes in it.
        """
 
        z_im = self.get_zbar_im(frame)
        self.scanner.scan(z_im)
        qr_list = []
        # Gather list of all QR objects
        for symbol in z_im:
            print "qr found"
            tl, bl, br, tr = [item for item in symbol.location]
            points = np.float32(np.float32(symbol.location))

            qr_list.append(QRCode(symbol, self.L))