コード例 #1
0
def CreateQRMatrix(dataToEncode, errLevel=QRErrorCorrectLevel.L):
   dataLen = len(dataToEncode)
   baseSz = 4 if errLevel == QRErrorCorrectLevel.L else \
            5 if errLevel == QRErrorCorrectLevel.M else \
            6 if errLevel == QRErrorCorrectLevel.Q else \
            7 # errLevel = QRErrorCorrectLevel.H
   sz = baseSz if dataLen < 70 else  5 +  (dataLen - 70) / 30
   qrmtrx = [[]]
   while sz<20:
      try:
         errCorrectEnum = getattr(QRErrorCorrectLevel, errLevel.upper())
         qr = QRCode(sz, errCorrectEnum)
         qr.addData(dataToEncode)
         qr.make()
         success=True
         break
      except TypeError:
         sz += 1

   if not success:
      LOGERROR('Unsuccessful attempt to create QR code')
      LOGERROR('Data to encode: (Length: %s, isAscii: %s)', \
                     len(dataToEncode), isASCII(dataToEncode))
      return [[0]], 1

   qrmtrx = []
   modCt = qr.getModuleCount()
   for r in range(modCt):
      tempList = [0]*modCt
      for c in range(modCt):
         # The matrix is transposed by default, from what we normally expect
         tempList[c] = 1 if qr.isDark(c,r) else 0
      qrmtrx.append(tempList)

   return [qrmtrx, modCt]
コード例 #2
0
   def setAsciiData(self, newAscii, prefSize=160, errLevel=QRErrorCorrectLevel.L, repaint=True):
      if len(newAscii)==0:
         self.qrmtrx = [[0]]
         self.modCt  = 1
         self.pxScale= 1
         return

      self.theData = newAscii
      sz=3
      success=False
      while sz<20:
         try:
            self.qr = QRCode(sz, errLevel)
            self.qr.addData(self.theData)
            self.qr.make()
            success=True
            break
         except TypeError:
            sz += 1

      if not success:
         LOGERROR('Unsuccessful attempt to create QR code')
         self.qrmtrx = [[0]]
         return

      self.qrmtrx = []
      self.modCt = self.qr.getModuleCount()
      for r in range(self.modCt):
         tempList = [0]*self.modCt
         for c in range(self.modCt):
            tempList[c] = 1 if self.qr.isDark(r,c) else 0
         self.qrmtrx.append(tempList)

      self.setPreferredSize(prefSize)
コード例 #3
0
    def setAsciiData(self,
                     newAscii,
                     prefSize=160,
                     errLevel=QRErrorCorrectLevel.L,
                     repaint=True):
        if len(newAscii) == 0:
            self.qrmtrx = [[0]]
            self.modCt = 1
            self.pxScale = 1
            return

        self.theData = newAscii
        sz = 3
        success = False
        while sz < 20:
            try:
                self.qr = QRCode(sz, errLevel)
                self.qr.addData(self.theData)
                self.qr.make()
                success = True
                break
            except TypeError:
                sz += 1

        if not success:
            LOGERROR('Unsuccessful attempt to create QR code')
            self.qrmtrx = [[0]]
            return

        self.qrmtrx = []
        self.modCt = self.qr.getModuleCount()
        for r in range(self.modCt):
            tempList = [0] * self.modCt
            for c in range(self.modCt):
                tempList[c] = 1 if self.qr.isDark(r, c) else 0
            self.qrmtrx.append(tempList)

        self.setPreferredSize(prefSize)
コード例 #4
0
class QRCodeWidget(QWidget):

   def __init__(self, asciiToEncode='', prefSize=160, errLevel=QRErrorCorrectLevel.L, parent=None):
      super(QRCodeWidget, self).__init__()

      self.parent = parent
      self.qrmtrx = None
      self.setAsciiData(asciiToEncode, prefSize, errLevel, repaint=False)
      

   def setAsciiData(self, newAscii, prefSize=160, errLevel=QRErrorCorrectLevel.L, repaint=True):
      if len(newAscii)==0:
         self.qrmtrx = [[0]]
         self.modCt  = 1
         self.pxScale= 1
         return

      self.theData = newAscii
      sz=3
      success=False
      while sz<20:
         try:
            self.qr = QRCode(sz, errLevel)
            self.qr.addData(self.theData)
            self.qr.make()
            success=True
            break
         except TypeError:
            sz += 1

      if not success:
         LOGERROR('Unsuccessful attempt to create QR code')
         self.qrmtrx = [[0]]
         return

      self.qrmtrx = []
      self.modCt = self.qr.getModuleCount()
      for r in range(self.modCt):
         tempList = [0]*self.modCt
         for c in range(self.modCt):
            tempList[c] = 1 if self.qr.isDark(r,c) else 0
         self.qrmtrx.append(tempList)

      self.setPreferredSize(prefSize)


      
            
   def getModuleCount1D(self):
      return self.modCt


   def setPreferredSize(self, px, policy='Approx'):
      self.pxScale,rem = divmod(int(px), int(self.modCt))

      if policy.lower().startswith('approx'):
         if rem>self.modCt/2.0:
            self.pxScale += 1
      elif policy.lower().startswith('atleast'):
         if rem>0:
            self.pxScale += 1
      elif policy.lower().startswith('max'):
         pass
      else:
         LOGERROR('Bad size policy in set qr size')
         return self.pxScale*self.modCt

      return
      

   def getSize(self):
      return self.pxScale*self.modCt

       
   def sizeHint(self):
      sz1d = self.pxScale*self.modCt
      return QSize(sz1d, sz1d)


   def paintEvent(self, e):
      qp = QPainter()
      qp.begin(self)
      self.drawWidget(qp)
      qp.end()



   def drawWidget(self, qp):
      # In case this is not a white background, draw the white boxes
      qp.setPen(QColor(255,255,255))
      qp.setBrush(QColor(255,255,255))
      for r in range(self.modCt):
         for c in range(self.modCt):
            if not self.qrmtrx[c][r]:
               qp.drawRect(*[a*self.pxScale for a in [r,c,1,1]])

      # Draw the black tiles
      qp.setPen(QColor(0,0,0))
      qp.setBrush(QColor(0,0,0))
      for r in range(self.modCt):
         for c in range(self.modCt):
            if self.qrmtrx[c][r]:
               qp.drawRect(*[a*self.pxScale for a in [r,c,1,1]])


   def mouseDoubleClickEvent(self, *args):
      DlgInflatedQR(self.parent, self.theData).exec_()
コード例 #5
0
class QRCodeWidget(QWidget):
    def __init__(self,
                 asciiToEncode='',
                 prefSize=160,
                 errLevel=QRErrorCorrectLevel.L,
                 parent=None):
        super(QRCodeWidget, self).__init__()

        self.parent = parent
        self.qrmtrx = None
        self.setAsciiData(asciiToEncode, prefSize, errLevel, repaint=False)

    def setAsciiData(self,
                     newAscii,
                     prefSize=160,
                     errLevel=QRErrorCorrectLevel.L,
                     repaint=True):
        if len(newAscii) == 0:
            self.qrmtrx = [[0]]
            self.modCt = 1
            self.pxScale = 1
            return

        self.theData = newAscii
        sz = 3
        success = False
        while sz < 20:
            try:
                self.qr = QRCode(sz, errLevel)
                self.qr.addData(self.theData)
                self.qr.make()
                success = True
                break
            except TypeError:
                sz += 1

        if not success:
            LOGERROR('Unsuccessful attempt to create QR code')
            self.qrmtrx = [[0]]
            return

        self.qrmtrx = []
        self.modCt = self.qr.getModuleCount()
        for r in range(self.modCt):
            tempList = [0] * self.modCt
            for c in range(self.modCt):
                tempList[c] = 1 if self.qr.isDark(r, c) else 0
            self.qrmtrx.append(tempList)

        self.setPreferredSize(prefSize)

    def getModuleCount1D(self):
        return self.modCt

    def setPreferredSize(self, px, policy='Approx'):
        self.pxScale, rem = divmod(int(px), int(self.modCt))

        if policy.lower().startswith('approx'):
            if rem > self.modCt / 2.0:
                self.pxScale += 1
        elif policy.lower().startswith('atleast'):
            if rem > 0:
                self.pxScale += 1
        elif policy.lower().startswith('max'):
            pass
        else:
            LOGERROR('Bad size policy in set qr size')
            return self.pxScale * self.modCt

        return

    def getSize(self):
        return self.pxScale * self.modCt

    def sizeHint(self):
        sz1d = self.pxScale * self.modCt
        return QSize(sz1d, sz1d)

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        self.drawWidget(qp)
        qp.end()

    def drawWidget(self, qp):
        # In case this is not a white background, draw the white boxes
        qp.setPen(QColor(255, 255, 255))
        qp.setBrush(QColor(255, 255, 255))
        for r in range(self.modCt):
            for c in range(self.modCt):
                if not self.qrmtrx[c][r]:
                    qp.drawRect(*[a * self.pxScale for a in [r, c, 1, 1]])

        # Draw the black tiles
        qp.setPen(QColor(0, 0, 0))
        qp.setBrush(QColor(0, 0, 0))
        for r in range(self.modCt):
            for c in range(self.modCt):
                if self.qrmtrx[c][r]:
                    qp.drawRect(*[a * self.pxScale for a in [r, c, 1, 1]])

    def mouseDoubleClickEvent(self, *args):
        DlgInflatedQR(self.parent, self.theData).exec_()