mirror of
https://github.com/seiichiro0185/sailotp.git
synced 2024-11-21 15:29:42 +00:00
Version 1.0
Added QRCode reader Added QRCode export Tokens can be sorted
This commit is contained in:
parent
1e73388791
commit
7b9ad31d74
225 changed files with 26821 additions and 165 deletions
43
README.md
43
README.md
|
@ -1,47 +1,30 @@
|
|||
# SailOTP
|
||||
|
||||
SailOTP is a Sailfish Implementation of the Google-Authenticator algorithms,
|
||||
also known as TOPT (timer based) and HOTP (counter based) as described in RFC 6238 and 4226. A growing
|
||||
number of sites uses this algorithm for two-factor-authentication, including
|
||||
Github, Linode and several Google services.
|
||||
SailOTP is a Sailfish Implementation of the Google-Authenticator algorithms, also known as TOPT (timer based) and HOTP (counter based) as described in RFC 6238 and 4226. A growing number of sites uses this algorithm for two-factor-authentication, including Github, Linode and several Google services.
|
||||
|
||||
One can add new OTP-entries using the pulley-menu. The type of token can be selected. Title and the shared
|
||||
secret have to be provided. For counter based HOTP-tokens the counter value for the next update of the
|
||||
Token can be set. The default of 1 is the standard value for new HOTP-tokens and should not be changed.
|
||||
One can add new OTP-entries using the pulley-menu. Codes can be added using the integrated QR-Code-Reader or by manually typing in the token information.
|
||||
|
||||
The main view of the app will show a list off all entries and their current One-Time-Tokens.
|
||||
The entries will be regenerated every 30 seconds, the remaining time for the current tokens is shown
|
||||
through a progress bar at the top of the app. HOTP-type tokens are not updated automatically, instead
|
||||
a refresh button is shown on the right of the token to calculate the next value and increment the counter
|
||||
An entry can be edited or deleted by long-pressing on it.
|
||||
The main view of the app will show a list off all entries and their current One-Time-Tokens. The entries will be regenerated every 30 seconds, the remaining time for the current tokens is shown through a progress bar at the top of the app. HOTP-type tokens are not updated automatically, instead a refresh button is shown on the right of the token to calculate the next value and increment the counter. An entry can be edited, deleted or moved up and down in the list by long-pressing on it.
|
||||
|
||||
One entry can be stared by tapping the star icon on the left. the stared item will be shown
|
||||
on the ActiveCover. If the Token is timer based, it will be refreshed every 30 seconds. 5 seconds before the token changes it's
|
||||
color will change to red. For counter based tokens a cover action to calculate the next token is shown instead.
|
||||
The item can be unstared by tapping the star icon again on the main view.
|
||||
In edit-mode one can show a QR-Code of the entry (e.g. for importing it on another device) through the pulley menu.
|
||||
|
||||
One entry can be stared by tapping the star icon on the left. the stared item will be shown on the ActiveCover. If the Token is timer based, it will be refreshed every 30 seconds. 5 seconds before the token changes it's color will change to red. For counter based tokens a cover action to calculate the next token is shown instead. The item can be unstared by tapping the star icon again on the main view.
|
||||
|
||||
From the main view a token can be copied to the clipboard by tapping on it.
|
||||
|
||||
From the pulley menu the token database can be exported to a file for backup purposes. The backup is a AES-256-CBC encrypted and
|
||||
Base64 encoded file containing a JSON-representation of the database. It can be decrypted with openssl using the following command:
|
||||
|
||||
openssl enc -d -a -aes-256-cbc -in <file>
|
||||
|
||||
openssl enc -d -a -aes-256-cbc -in <file>
|
||||
|
||||
if you need the information outside of SailOTP.
|
||||
|
||||
Importing the file is also possible from the pulley menu. If a file contains tokens that are already in the database
|
||||
(title and secret of the token match an existing one), these tokens will not be added again.
|
||||
|
||||
## Known Limitations
|
||||
|
||||
At the moment the only way to insert new entries into the app is to insert the
|
||||
title and secret key by hand. It's not possible to use the QR-Codes some sites
|
||||
provide directly.
|
||||
Importing the file is also possible from the pulley menu. If a file contains tokens that are already in the database (title and secret of the token match an existing one), these tokens will not be added again.
|
||||
|
||||
## Contact and Issues
|
||||
|
||||
If you find any bugs or want to suggest a feature, feel free to use Githubs
|
||||
Issues feature.
|
||||
Issues feature or write an email to sailfish _AT_ seiichiro0185.org
|
||||
|
||||
## License
|
||||
|
||||
|
@ -61,3 +44,9 @@ The implementation of the TOTP-algorithm was inspired by:
|
|||
|
||||
<a href="http://blog.tinisles.com/2011/10/google-authenticator-one-time-password-algorithm-in-javascript/" target="_blank">http://blog.tinisles.com/2011/10/google-authenticator-one-time-password-algorithm-in-javascript/</a>
|
||||
|
||||
An adapted version of the QZXing-library is used for QRCode-decoding
|
||||
<a href="http://sourceforge.net/projects/qzxing/">http://sourceforge.net/projects/qzxing/</a>
|
||||
|
||||
The qrencode library is used for QRCode-encoding existing tokens for export to other devices.
|
||||
<a href="http://fukuchi.org/works/qrencode/">http://fukuchi.org/works/qrencode/</a>
|
||||
|
||||
|
|
|
@ -30,10 +30,11 @@ OTHER_FILES += qml/harbour-sailotp.qml \
|
|||
qml/sailotp.png \
|
||||
qml/pages/ExportPage.qml \
|
||||
qml/lib/gibberish-aes.js \
|
||||
qml/components/NotifyBanner.qml
|
||||
|
||||
HEADERS += \
|
||||
src/fileio.h
|
||||
qml/components/NotifyBanner.qml \
|
||||
qml/pages/ScanOTP.qml \
|
||||
qml/lib/urldecoder.js \
|
||||
qml/pages/QRPage.qml \
|
||||
rpm/harbour-sailotp.changes
|
||||
|
||||
i18n.files = i18n/*.qm
|
||||
i18n.path = /usr/share/$${TARGET}/i18n
|
||||
|
@ -50,3 +51,6 @@ lupdate_only {
|
|||
i18n/en.ts
|
||||
}
|
||||
|
||||
include(src/qzxing/QZXing.pri)
|
||||
include(src/FileIO/FileIO.pri)
|
||||
include(src/qqrencode/qqrencode.pri)
|
||||
|
|
158
i18n/de.ts
158
i18n/de.ts
|
@ -34,57 +34,67 @@ Lizenz: BSD (3-Klausel)</translation>
|
|||
<context>
|
||||
<name>AddOTP</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="57"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="66"/>
|
||||
<source>Show QR-Code</source>
|
||||
<translation>QR-Code anzeigen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="69"/>
|
||||
<source>Can't create QR-Code from incomplete settings!</source>
|
||||
<translation>Ein QR-Code kann nur mit vollständigen Einstellungen erzeugt werden!</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="82"/>
|
||||
<source>Save</source>
|
||||
<translation>Speichern</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="57"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="82"/>
|
||||
<source>Add</source>
|
||||
<translation>Hinzufügen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="62"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="87"/>
|
||||
<source>Type</source>
|
||||
<translation>Typ</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="64"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="89"/>
|
||||
<source>Time-based (TOTP)</source>
|
||||
<translation>Zeitbasiert (TOTP)</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="65"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="90"/>
|
||||
<source>Counter-based (HOTP)</source>
|
||||
<translation>Zählerbasiert (HOTP)</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="71"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="96"/>
|
||||
<source>Title</source>
|
||||
<translation>Titel</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="72"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="97"/>
|
||||
<source>Title for the OTP</source>
|
||||
<translation>Titel für das Token</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="80"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="109"/>
|
||||
<source>Secret (at least 16 characters)</source>
|
||||
<translation>Schlüssel (mindestens 16 Zeichen)</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="82"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="111"/>
|
||||
<source>Secret OTP Key</source>
|
||||
<translation>Geheimer Schlüssel</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="90"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="123"/>
|
||||
<source>Next Counter Value</source>
|
||||
<translation>Nächster Zählerwert</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="92"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="125"/>
|
||||
<source>Next Value of the Counter</source>
|
||||
<translation>Nächster Wert für den Zähler</translation>
|
||||
</message>
|
||||
|
@ -102,102 +112,104 @@ Lizenz: BSD (3-Klausel)</translation>
|
|||
<translation>Gewählte Datei existiert nicht!</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="92"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="89"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="105"/>
|
||||
<source>Export</source>
|
||||
<translation>Export</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="92"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="89"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="105"/>
|
||||
<source>Import</source>
|
||||
<translation>Import</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="99"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="112"/>
|
||||
<source>Filename</source>
|
||||
<translation>Dateiname</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="100"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="113"/>
|
||||
<source>File to import</source>
|
||||
<translation>Aus Datei importieren</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="100"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="113"/>
|
||||
<source>File to export</source>
|
||||
<translation>In Datei exportieren</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="109"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="126"/>
|
||||
<source>Overwrite existing</source>
|
||||
<translation>Existierende überschreiben</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="115"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="132"/>
|
||||
<source>Password</source>
|
||||
<translation>Passwort</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="116"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="133"/>
|
||||
<source>Password for the file</source>
|
||||
<translation>Passwort für die Datei</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="125"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="146"/>
|
||||
<source>Passwords don't match!</source>
|
||||
<translation>Passwörter nicht identisch!</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="125"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="146"/>
|
||||
<source>Passwords match!</source>
|
||||
<translation>Passwörter identisch!</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="126"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="147"/>
|
||||
<source>Repeated Password for the file</source>
|
||||
<translation>Passwort wiederholen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="146"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="171"/>
|
||||
<source>Here you can Import Tokens from a file. Put in the file location and the password you used on export. Pull left to start the import.</source>
|
||||
<translation>Hier können Tokens aus einer Datei importiert werden. Gib die Datei und das beim Export gewählte Passwort ein. Nach links ziehen um zu starten.</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="162"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="187"/>
|
||||
<source>Here you can export Tokens to a file. The exported file will be encrypted with AES-256-CBC and Base64 encoded. Choose a strong password, the file will contain the secrets used to generate the Tokens for your accounts. Pull left to start the export.</source>
|
||||
<translation>Hier können Tokens in eine Datei exportiert werden. Die Datei wird mit AES-256-CBC verschlüsselt und Base64-kodiert. Wähle ein starkes Passwort, die Datei enthält die geheimen Schlüssel zur Erzeugung der Tokens für deine Accounts. Nach links ziehen um zu starten.</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="184"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="209"/>
|
||||
<source>Error writing to file </source>
|
||||
<translation>Fehler beim Schreiben der Datei</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="186"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="211"/>
|
||||
<source>Token Database exported to </source>
|
||||
<translation>Datenbank exportiert nach </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="189"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="214"/>
|
||||
<source>Could not encrypt tokens. Error: </source>
|
||||
<translation>Fehler beim Verschlüsseln. Fehler: </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="192"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="217"/>
|
||||
<source>Could not read tokens from Database</source>
|
||||
<translation>Datenbank konnte nicht gelesen werden</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="203"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="228"/>
|
||||
<source>Tokens imported from </source>
|
||||
<translation>Tokens importiert aus </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="208"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="233"/>
|
||||
<source>Unable to decrypt file, did you use the right password?</source>
|
||||
<translation>Fehler beim entschlüsseln, falsches Passwort?</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="211"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="236"/>
|
||||
<source>Could not read from file </source>
|
||||
<translation>Datei konnte nicht gelesen werden</translation>
|
||||
</message>
|
||||
|
@ -205,59 +217,117 @@ Lizenz: BSD (3-Klausel)</translation>
|
|||
<context>
|
||||
<name>MainView</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="111"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="102"/>
|
||||
<source>About</source>
|
||||
<translation>Über</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="115"/>
|
||||
<source>Export Token-DB</source>
|
||||
<translation>Datenbank exportieren</translation>
|
||||
<translation type="vanished">Datenbank exportieren</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="119"/>
|
||||
<source>Import Token-DB</source>
|
||||
<translation>Datenbank importieren</translation>
|
||||
<translation type="vanished">Datenbank importieren</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="123"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="106"/>
|
||||
<source>Export / Import</source>
|
||||
<translation>Export / Import</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="110"/>
|
||||
<source>Add Token</source>
|
||||
<translation>Token hinzufügen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="149"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="136"/>
|
||||
<source>Nothing here</source>
|
||||
<translation>Hier ist nichts</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="150"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="137"/>
|
||||
<source>Pull down to add a OTP</source>
|
||||
<translation>Nach unten ziehen zum hinzufügen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="161"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="148"/>
|
||||
<source>Deleting</source>
|
||||
<translation>Lösche</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="166"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="164"/>
|
||||
<source>Token for </source>
|
||||
<translation>Token für </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="166"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="164"/>
|
||||
<source> copied to clipboard</source>
|
||||
<translation> kopiert</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="234"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="232"/>
|
||||
<source>Move up</source>
|
||||
<translation>Nach oben</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="237"/>
|
||||
<source>Move down</source>
|
||||
<translation>Nach unten</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="242"/>
|
||||
<source>Edit</source>
|
||||
<translation>Bearbeiten</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="240"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="248"/>
|
||||
<source>Delete</source>
|
||||
<translation>Löschen</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>QRPage</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/QRPage.qml" line="71"/>
|
||||
<source>Can't create QR-Code from incomplete settings!</source>
|
||||
<translation>Ein QR-Code kann nur mit vollständigen Einstellungen erzeugt werden!</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>ScanOTP</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="53"/>
|
||||
<source>Can't access temporary directory</source>
|
||||
<translation>Kein Zugriff auf temporäres Verzeichnis.</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="63"/>
|
||||
<source>Add manually</source>
|
||||
<translation>Manuell hinzufügen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="70"/>
|
||||
<source>Scan Code</source>
|
||||
<translation>Code scannen</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="70"/>
|
||||
<source>Scanning...</source>
|
||||
<translation>scanne...</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="99"/>
|
||||
<source>No valid Token data found.</source>
|
||||
<translation>Kein gültiges Token gefunden.</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="140"/>
|
||||
<source>Tap the picture to start / stop scanning. Pull down to add Token manually.</source>
|
||||
<translation>Vorschau antippen um den Scan zu starten / zu stoppen. Nach unten ziehen um manuell hinzu zu fügen.</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Tap the picture to start scanning. Pull down to add Token manually.</source>
|
||||
<translation type="vanished">Vorschau antippen um den Scan zu starten. Nach unten ziehen um manuell hinzu zu fügen.</translation>
|
||||
</message>
|
||||
</context>
|
||||
</TS>
|
||||
|
|
152
i18n/en.ts
152
i18n/en.ts
|
@ -24,57 +24,67 @@ License: BSD (3-clause)</source>
|
|||
<context>
|
||||
<name>AddOTP</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="57"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="66"/>
|
||||
<source>Show QR-Code</source>
|
||||
<translation>Show QR-Code</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="69"/>
|
||||
<source>Can't create QR-Code from incomplete settings!</source>
|
||||
<translation>Can't create QR-Code from incomplete settings!</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="82"/>
|
||||
<source>Save</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="57"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="82"/>
|
||||
<source>Add</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="62"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="87"/>
|
||||
<source>Type</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="64"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="89"/>
|
||||
<source>Time-based (TOTP)</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="65"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="90"/>
|
||||
<source>Counter-based (HOTP)</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="71"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="96"/>
|
||||
<source>Title</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="72"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="97"/>
|
||||
<source>Title for the OTP</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="80"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="109"/>
|
||||
<source>Secret (at least 16 characters)</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="82"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="111"/>
|
||||
<source>Secret OTP Key</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="90"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="123"/>
|
||||
<source>Next Counter Value</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="92"/>
|
||||
<location filename="../qml/pages/AddOTP.qml" line="125"/>
|
||||
<source>Next Value of the Counter</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
|
@ -92,102 +102,104 @@ License: BSD (3-clause)</source>
|
|||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="92"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="89"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="105"/>
|
||||
<source>Export</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="92"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="89"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="105"/>
|
||||
<source>Import</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="99"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="112"/>
|
||||
<source>Filename</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="100"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="113"/>
|
||||
<source>File to import</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="100"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="113"/>
|
||||
<source>File to export</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="109"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="126"/>
|
||||
<source>Overwrite existing</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="115"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="132"/>
|
||||
<source>Password</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="116"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="133"/>
|
||||
<source>Password for the file</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="125"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="146"/>
|
||||
<source>Passwords don't match!</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="125"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="146"/>
|
||||
<source>Passwords match!</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="126"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="147"/>
|
||||
<source>Repeated Password for the file</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="146"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="171"/>
|
||||
<source>Here you can Import Tokens from a file. Put in the file location and the password you used on export. Pull left to start the import.</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="162"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="187"/>
|
||||
<source>Here you can export Tokens to a file. The exported file will be encrypted with AES-256-CBC and Base64 encoded. Choose a strong password, the file will contain the secrets used to generate the Tokens for your accounts. Pull left to start the export.</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="184"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="209"/>
|
||||
<source>Error writing to file </source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="186"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="211"/>
|
||||
<source>Token Database exported to </source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="189"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="214"/>
|
||||
<source>Could not encrypt tokens. Error: </source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="192"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="217"/>
|
||||
<source>Could not read tokens from Database</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="203"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="228"/>
|
||||
<source>Tokens imported from </source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="208"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="233"/>
|
||||
<source>Unable to decrypt file, did you use the right password?</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="211"/>
|
||||
<location filename="../qml/pages/ExportPage.qml" line="236"/>
|
||||
<source>Could not read from file </source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
|
@ -195,59 +207,105 @@ License: BSD (3-clause)</source>
|
|||
<context>
|
||||
<name>MainView</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="111"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="102"/>
|
||||
<source>About</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="115"/>
|
||||
<source>Export Token-DB</source>
|
||||
<location filename="../qml/pages/MainView.qml" line="106"/>
|
||||
<source>Export / Import</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="119"/>
|
||||
<source>Import Token-DB</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="123"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="110"/>
|
||||
<source>Add Token</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="149"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="136"/>
|
||||
<source>Nothing here</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="150"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="137"/>
|
||||
<source>Pull down to add a OTP</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="161"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="148"/>
|
||||
<source>Deleting</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="166"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="164"/>
|
||||
<source>Token for </source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="166"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="164"/>
|
||||
<source> copied to clipboard</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="234"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="232"/>
|
||||
<source>Move up</source>
|
||||
<translation>Move up</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="237"/>
|
||||
<source>Move down</source>
|
||||
<translation>Move down</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="242"/>
|
||||
<source>Edit</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/MainView.qml" line="240"/>
|
||||
<location filename="../qml/pages/MainView.qml" line="248"/>
|
||||
<source>Delete</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>QRPage</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/QRPage.qml" line="71"/>
|
||||
<source>Can't create QR-Code from incomplete settings!</source>
|
||||
<translation>Can't create QR-Code from incomplete settings!</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>ScanOTP</name>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="53"/>
|
||||
<source>Can't access temporary directory</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="63"/>
|
||||
<source>Add manually</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="70"/>
|
||||
<source>Scan Code</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="70"/>
|
||||
<source>Scanning...</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="99"/>
|
||||
<source>No valid Token data found.</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../qml/pages/ScanOTP.qml" line="140"/>
|
||||
<source>Tap the picture to start / stop scanning. Pull down to add Token manually.</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
</context>
|
||||
</TS>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
|
|
@ -75,12 +75,17 @@ CoverBackground {
|
|||
|
||||
Column {
|
||||
anchors.top: logo.bottom
|
||||
width: parent.width
|
||||
anchors.topMargin: 48
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
|
||||
Label {
|
||||
text: appWin.coverTitle
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
width: parent.width - Theme.paddingMedium*2
|
||||
maximumLineCount: 1
|
||||
truncationMode: TruncationMode.Fade
|
||||
horizontalAlignment: contentWidth <= width ? Text.AlignHCenter : Text.AlignLeft
|
||||
}
|
||||
Label {
|
||||
id: lOTP
|
||||
|
|
|
@ -78,8 +78,6 @@ ApplicationWindow
|
|||
}
|
||||
}
|
||||
|
||||
NotifyBanner { id: notify }
|
||||
|
||||
initialPage: Component { MainView { } }
|
||||
cover: Qt.resolvedUrl("cover/CoverPage.qml")
|
||||
|
||||
|
|
|
@ -36,18 +36,25 @@ function getDB() {
|
|||
|
||||
if (db.version == "") {
|
||||
// Initialize an empty DB, Create the Table
|
||||
db.changeVersion("", "2",
|
||||
db.changeVersion("", "3",
|
||||
function(tx) {
|
||||
tx.executeSql("CREATE TABLE IF NOT EXISTS OTPStorage(title TEXT, secret TEXT, type TEXT DEFAULT 'TOPT', counter INTEGER DEFAULT 0, fav INTEGER DEFAULT 0);");
|
||||
tx.executeSql("CREATE TABLE IF NOT EXISTS OTPStorage(title TEXT, secret TEXT, type TEXT DEFAULT 'TOPT', counter INTEGER DEFAULT 0, fav INTEGER DEFAULT 0, sort INTEGER DEFAULT 0);");
|
||||
});
|
||||
} else if (db.version == "1.0") {
|
||||
// Upgrade DB Schema to Version 2
|
||||
db.changeVersion("1.0", "2",
|
||||
// Upgrade DB Schema to Version 3
|
||||
db.changeVersion("1.0", "3",
|
||||
function(tx) {
|
||||
tx.executeSql("ALTER TABLE OTPStorage ADD COLUMN type TEXT DEFAULT 'TOTP';");
|
||||
tx.executeSql("ALTER TABLE OTPStorage ADD COLUMN counter INTEGER DEFAULT 0;");
|
||||
tx.executeSql("ALTER TABLE OTPStorage ADD COLUMN fav INTEGER DEFAULT 0;");
|
||||
tx.executeSql("ALTER TABLE OTPStorage ADD COLUMN sort INTEGER DEFAULT 0;");
|
||||
});
|
||||
} else if (db.version == "2") {
|
||||
// Upgrade DB Schema to Version 3
|
||||
db.changeVersion("2", "3",
|
||||
function(tx) {
|
||||
tx.executeSql("ALTER TABLE OTPStorage ADD COLUMN sort INTEGER DEFAULT 0;");
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// DB Failed to open
|
||||
|
@ -63,9 +70,9 @@ function getOTP() {
|
|||
|
||||
db.transaction(
|
||||
function(tx) {
|
||||
var res = tx.executeSql("select * from OTPStorage;");
|
||||
var res = tx.executeSql("select * from OTPStorage order by sort;");
|
||||
for (var i=0; i < res.rows.length; i++) {
|
||||
appWin.appendOTP(res.rows.item(i).title, res.rows.item(i).secret, res.rows.item(i).type, res.rows.item(i).counter, res.rows.item(i).fav);
|
||||
appWin.appendOTP(res.rows.item(i).title, res.rows.item(i).secret, res.rows.item(i).type, res.rows.item(i).counter, res.rows.item(i).fav, res.rows.item(i).sort);
|
||||
if (res.rows.item(i).fav) appWin.setCover(i);
|
||||
}
|
||||
});
|
||||
|
@ -85,12 +92,13 @@ function db2json() {
|
|||
"secret": res.rows.item(i).secret,
|
||||
"type": res.rows.item(i).type,
|
||||
"counter": res.rows.item(i).counter,
|
||||
"sort": res.rows.item(i).sort,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (otpList.length > 0) {
|
||||
return(JSON.stringify({"app": "sailotp", "version": 1, "otplist": otpList}));
|
||||
return(JSON.stringify({"app": "sailotp", "version": 2, "otplist": otpList}));
|
||||
} else {
|
||||
return("")
|
||||
}
|
||||
|
@ -101,7 +109,7 @@ function json2db(jsonString, error) {
|
|||
var json = JSON.parse(jsonString);
|
||||
error = "";
|
||||
|
||||
if (json.version != "1" && json.app != "sailotp" ) {
|
||||
if ((json.version != "1" || json.version != "2") && json.app != "sailotp" ) {
|
||||
error = "Unrecognized format, file is not a SailOTP export";
|
||||
return(false);
|
||||
} else {
|
||||
|
@ -111,7 +119,11 @@ function json2db(jsonString, error) {
|
|||
while(otpList.length > 0) {
|
||||
var otpItem = otpList.shift();
|
||||
if (otpItem.title != "" & otpItem.secret.length >= 16) {
|
||||
addOTP(otpItem.title, otpItem.secret, otpItem.type, otpItem.counter);
|
||||
if (json.version == "1") {
|
||||
addOTP(otpItem.title, otpItem.secret, otpItem.type, otpItem.counter, 0);
|
||||
} else {
|
||||
addOTP(otpItem.title, otpItem.secret, otpItem.type, otpItem.counter, otpItem.sort);
|
||||
}
|
||||
}
|
||||
}
|
||||
parentPage.refreshOTPList();
|
||||
|
@ -124,7 +136,7 @@ function json2db(jsonString, error) {
|
|||
}
|
||||
|
||||
// Add a new OTP
|
||||
function addOTP(title, secret, type, counter) {
|
||||
function addOTP(title, secret, type, counter, sort) {
|
||||
var db = getDB();
|
||||
|
||||
db.transaction(
|
||||
|
@ -132,7 +144,7 @@ function addOTP(title, secret, type, counter) {
|
|||
if (checkOTP(title, secret)) {
|
||||
console.log("Token " + title + " is already in DB");
|
||||
} else {
|
||||
tx.executeSql("INSERT INTO OTPStorage VALUES(?, ?, ?, ?, ?);", [title, secret, type, counter, 0]);
|
||||
tx.executeSql("INSERT INTO OTPStorage VALUES(?, ?, ?, ?, ?, ?);", [title, secret, type, counter, 0, sort]);
|
||||
console.log("Token " + title + " added.");
|
||||
}
|
||||
});
|
||||
|
@ -197,6 +209,20 @@ function changeOTP(title, secret, type, counter, oldtitle, oldsecret) {
|
|||
});
|
||||
}
|
||||
|
||||
function changeOTPSort(title, secret, sort) {
|
||||
var db = getDB();
|
||||
|
||||
db.transaction(
|
||||
function(tx) {
|
||||
if (!checkOTP(title, secret)) {
|
||||
console.log("Token " + title + " is not in DB");
|
||||
} else {
|
||||
tx.executeSql("UPDATE OTPStorage SET sort=? WHERE title=? and secret=?;", [sort, title, secret]);
|
||||
console.log("Token " + title + " modified.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get the counter for a HOTP value, incerment the counter on request
|
||||
function getCounter(title, secret, increment) {
|
||||
var db = getDB();
|
||||
|
|
23
qml/lib/urldecoder.js
Normal file
23
qml/lib/urldecoder.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
|
||||
function decode(url) {
|
||||
// otpauth://totp/user@host.com?secret=JBSWY3DPEHPK3PXP
|
||||
// otpauth://totp/user@host.com?secret=JBSWY3DPEHPK3PXP
|
||||
|
||||
if (url.search(/^otpauth:\/\/[th]otp\/.*?.*/) != -1) {
|
||||
var ret = {"type": "", "title": "", "secret": "", "counter": ""};
|
||||
ret.type = url.slice(10,14).toUpperCase();
|
||||
ret.title = url.slice(15, url.indexOf("?"));
|
||||
var pstr = url.slice(url.indexOf("?")+1, url.length);
|
||||
var params = pstr.split("&");
|
||||
|
||||
for (var i = 0; i < params.length; ++i) {
|
||||
pstr = params[i];
|
||||
var tmp = pstr.split("=");
|
||||
if (tmp[0] == "secret") ret.secret = tmp[1];
|
||||
if (tmp[0] == "counter") ret.counter = tmp[1];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@ -37,12 +37,12 @@ Page {
|
|||
id: logo
|
||||
source: "../sailotp.png"
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
y: 150
|
||||
y: 140
|
||||
}
|
||||
Label {
|
||||
id: name
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
y: 270
|
||||
y: 260
|
||||
font.bold: true
|
||||
text: "SailOTP " + Qt.application.version
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ Page {
|
|||
font.pixelSize: Theme.fontSizeSmall
|
||||
horizontalAlignment: TextEdit.Center
|
||||
readOnly: true
|
||||
text: qsTr("SailOTP uses the following third party libs:")+"\n\nhttp://caligatio.github.io/jsSHA/\nhttps://github.com/mdp/gibberish-aes"
|
||||
text: qsTr("SailOTP uses the following third party libs:")+"\n\nhttp://caligatio.github.io/jsSHA/\nhttps://github.com/mdp/gibberish-aes/\nhttp://sourceforge.net/projects/qzxing/\nhttp://fukuchi.org/works/qrencode/"
|
||||
color: "white"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@ -44,17 +44,42 @@ Dialog {
|
|||
property string paramLabel: ""
|
||||
property string paramKey: ""
|
||||
property int paramCounter: 1 // New Counters start at 1
|
||||
property bool paramNew: false
|
||||
|
||||
function checkQR() {
|
||||
if (paramLabel != "" && paramKey != "" && !paramNew) {
|
||||
return(true);
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
SilicaFlickable {
|
||||
id: addOtpList
|
||||
anchors.fill: parent
|
||||
|
||||
|
||||
|
||||
PullDownMenu {
|
||||
visible: checkQR()
|
||||
MenuItem {
|
||||
text: qsTr("Show QR-Code")
|
||||
onClicked: {
|
||||
if ((paramType == "TOTP" && (otpLabel.text == "" || otpSecret.text == "")) || (paramType == "HOTP" && (otpLabel.text == "" || otpSecret.text == "" || otpCounter.text <= 0))) {
|
||||
notify.show(qsTr("Can't create QR-Code from incomplete settings!"), 4000);
|
||||
} else {
|
||||
pageStack.push(Qt.resolvedUrl("QRPage.qml"), {paramLabel: otpLabel.text, paramKey: otpSecret.text, paramType: paramType, paramCounter: otpCounter.text});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VerticalScrollDecorator {}
|
||||
|
||||
Column {
|
||||
anchors.fill: parent
|
||||
DialogHeader {
|
||||
acceptText: paramLabel != "" ? qsTr("Save") : qsTr("Add")
|
||||
acceptText: paramNew ? qsTr("Add") : qsTr("Save")
|
||||
}
|
||||
|
||||
ComboBox {
|
||||
|
@ -115,13 +140,13 @@ Dialog {
|
|||
// Save if page is Left with Add
|
||||
onDone: {
|
||||
if (result == DialogResult.Accepted) {
|
||||
// Save the entry to the Config DB
|
||||
if (paramLabel != "" && paramKey != "") {
|
||||
// Save the entry to the Config DB
|
||||
if (paramLabel != "" && paramKey != "" && !paramNew) {
|
||||
// Parameters where filled -> Change existing entry
|
||||
DB.changeOTP(otpLabel.text, otpSecret.text, paramType, otpCounter.text, paramLabel, paramKey)
|
||||
} else {
|
||||
// There were no parameters -> Add new entry
|
||||
DB.addOTP(otpLabel.text, otpSecret.text, paramType, otpCounter.text);
|
||||
DB.addOTP(otpLabel.text, otpSecret.text, paramType, otpCounter.text, appWin.listModel.count);
|
||||
}
|
||||
// Refresh the main Page
|
||||
parentPage.refreshOTPList();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@ -84,6 +84,19 @@ Dialog {
|
|||
id: exportFlickable
|
||||
anchors.fill: parent
|
||||
|
||||
PullDownMenu {
|
||||
MenuItem {
|
||||
text: mode == "export" ? qsTr("Import") : qsTr("Export")
|
||||
onClicked: {
|
||||
if (mode == "export") {
|
||||
mode = "import"
|
||||
} else {
|
||||
mode = "export"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VerticalScrollDecorator {}
|
||||
|
||||
Column {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@ -44,7 +44,7 @@ Page {
|
|||
appWin.coverTitle = title
|
||||
appWin.coverSecret = secret
|
||||
appWin.coverType = type
|
||||
if (secret = "") {
|
||||
if (secret == "") {
|
||||
appWin.coverOTP = "";
|
||||
} else if (type == "HOTP") {
|
||||
appWin.coverOTP = "------";
|
||||
|
@ -103,16 +103,12 @@ Page {
|
|||
onClicked: pageStack.push(Qt.resolvedUrl("About.qml"))
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Export Token-DB")
|
||||
text: qsTr("Export / Import")
|
||||
onClicked: pageStack.push(Qt.resolvedUrl("ExportPage.qml"), {parentPage: mainPage, mode: "export"})
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Import Token-DB")
|
||||
onClicked: pageStack.push(Qt.resolvedUrl("ExportPage.qml"), {parentPage: mainPage, mode: "import"})
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Add Token")
|
||||
onClicked: pageStack.push(Qt.resolvedUrl("AddOTP.qml"), {parentPage: mainPage})
|
||||
onClicked: pageStack.push(Qt.resolvedUrl("ScanOTP.qml"), {parentPage: mainPage})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,6 +148,17 @@ Page {
|
|||
remorseAction(qsTr("Deleting"), function() { DB.removeOTP(title, secret); appWin.listModel.remove(index) })
|
||||
}
|
||||
|
||||
function moveEntry(direction, index) {
|
||||
if (direction) {
|
||||
appWin.listModel.move(index, index-1, 1);
|
||||
} else {
|
||||
appWin.listModel.move(index, index+1, 1);
|
||||
}
|
||||
for (var i=0; i<appWin.listModel.count; i++) {
|
||||
DB.changeOTPSort(appWin.listModel.get(i).title, appWin.listModel.get(i).secret, i);
|
||||
}
|
||||
}
|
||||
|
||||
onClicked: {
|
||||
Clipboard.text = otp
|
||||
notify.show(qsTr("Token for ") + title + qsTr(" copied to clipboard"), 3000);
|
||||
|
@ -221,6 +228,16 @@ Page {
|
|||
Component {
|
||||
id: otpContextMenu
|
||||
ContextMenu {
|
||||
MenuItem {
|
||||
text: qsTr("Move up")
|
||||
visible: index > 0 ? true : false;
|
||||
onClicked: moveEntry(1, index);
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Move down")
|
||||
visible: index < appWin.listModel.count - 1 ? true : false;
|
||||
onClicked: moveEntry(0, index);
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Edit")
|
||||
onClicked: {
|
||||
|
|
74
qml/pages/QRPage.qml
Normal file
74
qml/pages/QRPage.qml
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this
|
||||
* list of conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. The names of the contributors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
import QtQuick 2.0
|
||||
import Sailfish.Silica 1.0
|
||||
|
||||
Page {
|
||||
id: qrpage
|
||||
|
||||
property string paramType: ""
|
||||
property string paramLabel: ""
|
||||
property string paramKey: ""
|
||||
property int paramCounter: 0
|
||||
|
||||
Label {
|
||||
id: qrLabel
|
||||
text: paramLabel
|
||||
color: Theme.highlightColor
|
||||
font.pixelSize: Theme.fontSizeLarge
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
y: 96
|
||||
}
|
||||
|
||||
Image {
|
||||
id: qrImage
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
y: 200
|
||||
cache: false
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
|
||||
var otpurl = "";
|
||||
if (paramType == "TOTP") {
|
||||
if (paramLabel != "" && paramKey != "")
|
||||
otpurl = "otpauth://totp/"+paramLabel+"?secret="+paramKey;
|
||||
|
||||
} else if (paramType == "HOTP") {
|
||||
if (paramLabel != "" && paramKey != "" && paramCounter > 0)
|
||||
otpurl = "otpauth://hotp/"+paramLabel+"?secret="+paramKey+"&counter="+paramCounter;
|
||||
}
|
||||
if (otpurl != "") {
|
||||
qrImage.source = "image://qqrencoder/"+otpurl;
|
||||
} else {
|
||||
notify.show(qsTr("Can't create QR-Code from incomplete settings!"), 4000);
|
||||
}
|
||||
}
|
||||
}
|
139
qml/pages/ScanOTP.qml
Normal file
139
qml/pages/ScanOTP.qml
Normal file
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this
|
||||
* list of conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. The names of the contributors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
import QtQuick 2.0
|
||||
import QtMultimedia 5.0
|
||||
import Sailfish.Silica 1.0
|
||||
import harbour.sailotp.QZXing 2.2
|
||||
import harbour.sailotp.FileIO 1.0
|
||||
import "../lib/urldecoder.js" as URL
|
||||
|
||||
Page {
|
||||
id: scanPage
|
||||
|
||||
property QtObject parentPage: null
|
||||
property bool scanning: false
|
||||
|
||||
Timer {
|
||||
id: scanTimer
|
||||
interval: 100
|
||||
running: false
|
||||
repeat: false
|
||||
onTriggered: {
|
||||
if (fileIO.mkpath(XDG_CACHE_DIR)) {
|
||||
cam.imageCapture.captureToLocation(XDG_CACHE_DIR + "/qrscan.jpg");
|
||||
} else {
|
||||
notify.show(qsTr("Can't access temporary directory"), 3000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SilicaFlickable {
|
||||
anchors.fill: parent
|
||||
|
||||
PullDownMenu {
|
||||
MenuItem {
|
||||
text: qsTr("Add manually")
|
||||
onClicked: pageStack.replace(Qt.resolvedUrl("AddOTP.qml"), {parentPage: parentPage, paramNew: true})
|
||||
}
|
||||
}
|
||||
|
||||
PageHeader {
|
||||
id: header
|
||||
title: scanning ? qsTr("Scanning...") : qsTr("Scan Code")
|
||||
}
|
||||
|
||||
Camera {
|
||||
id: cam
|
||||
|
||||
flash.mode: Camera.FlashOff
|
||||
captureMode: Camera.CaptureStillImage
|
||||
focus.focusMode: Camera.FocusContinuous
|
||||
|
||||
imageCapture {
|
||||
onImageSaved: {
|
||||
decoder.decodeImageFromFile(XDG_CACHE_DIR + "/qrscan.jpg");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QZXing {
|
||||
id: decoder
|
||||
|
||||
onTagFound: {
|
||||
var ret = URL.decode(tag);
|
||||
scanning = false
|
||||
if (ret && ret.type != "" && ret.title != "" && ret.secret != "" && (ret.counter != "" || ret.type == "TOTP")) {
|
||||
pageStack.replace(Qt.resolvedUrl("AddOTP.qml"), {parentPage: parentPage, paramLabel: ret.title, paramKey: ret.secret, paramType: ret.type, paramCounter: ret.counter, paramNew: true})
|
||||
} else {
|
||||
notify.show(qsTr("No valid Token data found."), 3000);
|
||||
}
|
||||
}
|
||||
|
||||
onDecodingFinished: if (succeeded==false && scanning) scanTimer.start();
|
||||
}
|
||||
|
||||
FileIO {
|
||||
id: fileIO
|
||||
}
|
||||
|
||||
VideoOutput {
|
||||
id: prev
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.top: header.bottom
|
||||
source: cam
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: {
|
||||
if (scanning) {
|
||||
scanning = false;
|
||||
} else {
|
||||
scanning = true;
|
||||
scanTimer.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Text {
|
||||
id: text
|
||||
|
||||
anchors.top: prev.bottom
|
||||
anchors.topMargin: 32
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
width: parent.width - 2*Theme.paddingLarge
|
||||
|
||||
wrapMode: Text.Wrap
|
||||
maximumLineCount: 4
|
||||
font.pixelSize: Theme.fontSizeSmall
|
||||
color: Theme.primaryColor
|
||||
text: qsTr("Tap the picture to start / stop scanning. Pull down to add Token manually.")
|
||||
}
|
||||
}
|
||||
}
|
2
rpm/harbour-sailotp.changes
Normal file
2
rpm/harbour-sailotp.changes
Normal file
|
@ -0,0 +1,2 @@
|
|||
* Tue Jul 01 2014 Stefan Brand <sailfish@seiichiro0185.org> 1.0-1
|
||||
- Added harbour-compatible QR-Reader
|
|
@ -1,6 +1,6 @@
|
|||
Name: harbour-sailotp
|
||||
Summary: SailOTP
|
||||
Version: 0.6
|
||||
Version: 1.0
|
||||
Release: 1
|
||||
Group: Security
|
||||
URL: https://github.com/seiichiro0185/sailotp/
|
||||
|
@ -18,20 +18,14 @@ PkgConfigBR:
|
|||
- Qt5Quick
|
||||
- Qt5Qml
|
||||
- Qt5Core
|
||||
- sailfishapp >= 0.0.10
|
||||
- Qt5Core
|
||||
- Qt5Qml
|
||||
- Qt5Quick
|
||||
- Qt5Multimedia
|
||||
- sailfishapp >= 1.0.2
|
||||
Requires:
|
||||
- sailfishsilica-qt5 >= 0.10.9
|
||||
Files:
|
||||
- '%{_bindir}'
|
||||
- '%{_datadir}/%{name}/qml'
|
||||
- '%{_datadir}/%{name}/i18n'
|
||||
- '%{_datadir}/applications/%{name}.desktop'
|
||||
- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png'
|
||||
- /usr/bin
|
||||
- /usr/share/harbour-sailotp
|
||||
- /usr/share/applications
|
||||
- /usr/share/icons/hicolor/86x86/apps
|
||||
- /usr/share/harbour-sailotp/i18n
|
||||
PkgBR: []
|
||||
|
|
7
src/FileIO/FileIO.pri
Normal file
7
src/FileIO/FileIO.pri
Normal file
|
@ -0,0 +1,7 @@
|
|||
QT += core quick
|
||||
|
||||
INCLUDEPATH += $$PWD/src
|
||||
|
||||
HEADERS += $$PWD/src/fileio.h
|
||||
|
||||
SOURCES += $$PWD/src/fileio.cpp
|
113
src/FileIO/src/fileio.cpp
Normal file
113
src/FileIO/src/fileio.cpp
Normal file
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this
|
||||
* list of conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. The names of the contributors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "fileio.h"
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QTextStream>
|
||||
|
||||
FileIO::FileIO(QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString FileIO::read()
|
||||
{
|
||||
if (mSource.isEmpty()){
|
||||
emit error("source is empty");
|
||||
return QString();
|
||||
}
|
||||
|
||||
QFile file(mSource);
|
||||
QString fileContent;
|
||||
if ( file.open(QIODevice::ReadOnly) ) {
|
||||
QString line;
|
||||
QTextStream t( &file );
|
||||
do {
|
||||
line = t.readLine();
|
||||
fileContent += line;
|
||||
} while (!line.isNull());
|
||||
|
||||
file.close();
|
||||
} else {
|
||||
emit error("Unable to open the file");
|
||||
return QString();
|
||||
}
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
bool FileIO::write(const QString& data)
|
||||
{
|
||||
if (mSource.isEmpty())
|
||||
return false;
|
||||
|
||||
QFile file(mSource);
|
||||
if (!file.open(QFile::WriteOnly | QFile::Truncate))
|
||||
return false;
|
||||
|
||||
QTextStream out(&file);
|
||||
out << data;
|
||||
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileIO::exists()
|
||||
{
|
||||
if (mSource.isEmpty()) {
|
||||
emit error("Source is empty!");
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile file(mSource);
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
bool FileIO::exists(const QString& filename)
|
||||
{
|
||||
if (filename.isEmpty()) {
|
||||
emit error("Source is empty!");
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile file(filename);
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
bool FileIO::mkpath(const QString& dirpath) {
|
||||
if (dirpath.isEmpty()) {
|
||||
emit error("Source is empty!");
|
||||
return false;
|
||||
}
|
||||
|
||||
QDir dir(dirpath);
|
||||
return dir.mkpath(dirpath);
|
||||
}
|
64
src/FileIO/src/fileio.h
Normal file
64
src/FileIO/src/fileio.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this
|
||||
* list of conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. The names of the contributors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef FILEIO_H
|
||||
#define FILEIO_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class FileIO : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
|
||||
|
||||
explicit FileIO(QObject *parent = 0);
|
||||
|
||||
Q_INVOKABLE QString read();
|
||||
Q_INVOKABLE bool write(const QString& data);
|
||||
Q_INVOKABLE bool exists();
|
||||
Q_INVOKABLE bool exists(const QString& filename);
|
||||
Q_INVOKABLE bool mkpath(const QString& dirpath);
|
||||
|
||||
QString source() { return mSource; };
|
||||
|
||||
public slots:
|
||||
void setSource(const QString& source) { mSource = source; };
|
||||
|
||||
signals:
|
||||
void sourceChanged(const QString& source);
|
||||
void error(const QString& msg);
|
||||
|
||||
private:
|
||||
QString mSource;
|
||||
};
|
||||
|
||||
#endif // FILEIO_H
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* Copyright (c) 2014, Stefan Brand <seiichiro@seiichiro0185.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
|
@ -31,6 +31,8 @@
|
|||
#include <sailfishapp.h>
|
||||
#include <QGuiApplication>
|
||||
#include "fileio.h"
|
||||
#include "qzxing.h"
|
||||
#include "qqrencode.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -38,6 +40,10 @@ int main(int argc, char *argv[])
|
|||
QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
|
||||
QScopedPointer<QQuickView> view(SailfishApp::createView());
|
||||
|
||||
// QZXing QR Decoder
|
||||
QZXing qrdecoder;
|
||||
qrdecoder.registerQMLTypes();
|
||||
|
||||
// Internationalization, Load the Language
|
||||
QString locale = QLocale::system().name();
|
||||
QTranslator translator;
|
||||
|
@ -56,6 +62,8 @@ int main(int argc, char *argv[])
|
|||
// Prepare the QML and set Homedir
|
||||
view->setSource(SailfishApp::pathTo("qml/harbour-sailotp.qml"));
|
||||
view->rootContext()->setContextProperty("XDG_HOME_DIR", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
|
||||
view->rootContext()->setContextProperty("XDG_CACHE_DIR", QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
view->engine()->addImageProvider("qqrencoder", new QQRencoder());
|
||||
view->show();
|
||||
|
||||
// Run the app
|
||||
|
|
43
src/qqrencode/qqrencode.cpp
Normal file
43
src/qqrencode/qqrencode.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <QPainter>
|
||||
#include "qqrencode.h"
|
||||
#include "qrencode.h"
|
||||
|
||||
QQRencoder::QQRencoder() : QQuickImageProvider(QQuickImageProvider::Image) {}
|
||||
|
||||
QImage QQRencoder::requestImage(const QString &id, QSize *size, const QSize &requestedSize) {
|
||||
// Inspired by https://code.google.com/p/livepro/source/browse/trunk/gfxengine/QRCodeQtUtil.cpp
|
||||
|
||||
int imgSize = 400;
|
||||
if (requestedSize.width() > 0) imgSize = requestedSize.width();
|
||||
|
||||
// Encode QR-Data
|
||||
QRcode *qrdata = QRcode_encodeString(qPrintable(id), 0, QR_ECLEVEL_M, QR_MODE_8, 1);
|
||||
|
||||
// Calculate qr-pixel size
|
||||
int datawidth = qrdata->width;
|
||||
int pixsize = static_cast<int>((imgSize - 64) / datawidth);
|
||||
|
||||
// allocate memory for the image
|
||||
QImage image(imgSize, imgSize, QImage::Format_Mono);
|
||||
memset(image.scanLine(0),0,image.byteCount());
|
||||
|
||||
// Draw Image from QR-Data
|
||||
QPainter painter(&image);
|
||||
painter.fillRect(image.rect(),Qt::white);
|
||||
for(int x=0;x<datawidth;x++) {
|
||||
for(int y=0;y<datawidth;y++) {
|
||||
if(1 & qrdata->data[y*datawidth+x]) painter.fillRect(QRect(x*pixsize+32, y*pixsize+32, pixsize, pixsize), Qt::black);
|
||||
}
|
||||
}
|
||||
|
||||
size->setHeight(image.height());
|
||||
size->setWidth(image.width());
|
||||
|
||||
// free up memory
|
||||
painter.end();
|
||||
QRcode_free(qrdata);
|
||||
|
||||
// return image data
|
||||
return image;
|
||||
}
|
||||
|
16
src/qqrencode/qqrencode.h
Normal file
16
src/qqrencode/qqrencode.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef QQRENCODE_H
|
||||
#define QQRENCODE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QImage>
|
||||
#include <QQuickImageProvider>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
class QQRencoder : public QQuickImageProvider {
|
||||
public:
|
||||
explicit QQRencoder();
|
||||
virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize);
|
||||
};
|
||||
|
||||
#endif // QQRENCODE_H
|
11
src/qqrencode/qqrencode.pri
Normal file
11
src/qqrencode/qqrencode.pri
Normal file
|
@ -0,0 +1,11 @@
|
|||
INCLUDEPATH += \
|
||||
$$PWD \
|
||||
$$PWD/qrencode
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/qrencode/*.h \
|
||||
$$PWD/qqrencode.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/qrencode/*.c \
|
||||
$$PWD/qqrencode.cpp
|
1164
src/qqrencode/qrenc.c
Normal file
1164
src/qqrencode/qrenc.c
Normal file
File diff suppressed because it is too large
Load diff
238
src/qqrencode/qrencode/bitstream.c
Normal file
238
src/qqrencode/qrencode/bitstream.c
Normal file
|
@ -0,0 +1,238 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Binary sequence class.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "bitstream.h"
|
||||
|
||||
BitStream *BitStream_new(void)
|
||||
{
|
||||
BitStream *bstream;
|
||||
|
||||
bstream = (BitStream *)malloc(sizeof(BitStream));
|
||||
if(bstream == NULL) return NULL;
|
||||
|
||||
bstream->length = 0;
|
||||
bstream->data = NULL;
|
||||
|
||||
return bstream;
|
||||
}
|
||||
|
||||
static int BitStream_allocate(BitStream *bstream, int length)
|
||||
{
|
||||
unsigned char *data;
|
||||
|
||||
if(bstream == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
data = (unsigned char *)malloc(length);
|
||||
if(data == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(bstream->data) {
|
||||
free(bstream->data);
|
||||
}
|
||||
bstream->length = length;
|
||||
bstream->data = data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static BitStream *BitStream_newFromNum(int bits, unsigned int num)
|
||||
{
|
||||
unsigned int mask;
|
||||
int i;
|
||||
unsigned char *p;
|
||||
BitStream *bstream;
|
||||
|
||||
bstream = BitStream_new();
|
||||
if(bstream == NULL) return NULL;
|
||||
|
||||
if(BitStream_allocate(bstream, bits)) {
|
||||
BitStream_free(bstream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = bstream->data;
|
||||
mask = 1 << (bits - 1);
|
||||
for(i=0; i<bits; i++) {
|
||||
if(num & mask) {
|
||||
*p = 1;
|
||||
} else {
|
||||
*p = 0;
|
||||
}
|
||||
p++;
|
||||
mask = mask >> 1;
|
||||
}
|
||||
|
||||
return bstream;
|
||||
}
|
||||
|
||||
static BitStream *BitStream_newFromBytes(int size, unsigned char *data)
|
||||
{
|
||||
unsigned char mask;
|
||||
int i, j;
|
||||
unsigned char *p;
|
||||
BitStream *bstream;
|
||||
|
||||
bstream = BitStream_new();
|
||||
if(bstream == NULL) return NULL;
|
||||
|
||||
if(BitStream_allocate(bstream, size * 8)) {
|
||||
BitStream_free(bstream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = bstream->data;
|
||||
for(i=0; i<size; i++) {
|
||||
mask = 0x80;
|
||||
for(j=0; j<8; j++) {
|
||||
if(data[i] & mask) {
|
||||
*p = 1;
|
||||
} else {
|
||||
*p = 0;
|
||||
}
|
||||
p++;
|
||||
mask = mask >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
return bstream;
|
||||
}
|
||||
|
||||
int BitStream_append(BitStream *bstream, BitStream *arg)
|
||||
{
|
||||
unsigned char *data;
|
||||
|
||||
if(arg == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if(arg->length == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(bstream->length == 0) {
|
||||
if(BitStream_allocate(bstream, arg->length)) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(bstream->data, arg->data, arg->length);
|
||||
return 0;
|
||||
}
|
||||
|
||||
data = (unsigned char *)malloc(bstream->length + arg->length);
|
||||
if(data == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(data, bstream->data, bstream->length);
|
||||
memcpy(data + bstream->length, arg->data, arg->length);
|
||||
|
||||
free(bstream->data);
|
||||
bstream->length += arg->length;
|
||||
bstream->data = data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BitStream_appendNum(BitStream *bstream, int bits, unsigned int num)
|
||||
{
|
||||
BitStream *b;
|
||||
int ret;
|
||||
|
||||
if(bits == 0) return 0;
|
||||
|
||||
b = BitStream_newFromNum(bits, num);
|
||||
if(b == NULL) return -1;
|
||||
|
||||
ret = BitStream_append(bstream, b);
|
||||
BitStream_free(b);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int BitStream_appendBytes(BitStream *bstream, int size, unsigned char *data)
|
||||
{
|
||||
BitStream *b;
|
||||
int ret;
|
||||
|
||||
if(size == 0) return 0;
|
||||
|
||||
b = BitStream_newFromBytes(size, data);
|
||||
if(b == NULL) return -1;
|
||||
|
||||
ret = BitStream_append(bstream, b);
|
||||
BitStream_free(b);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsigned char *BitStream_toByte(BitStream *bstream)
|
||||
{
|
||||
int i, j, size, bytes;
|
||||
unsigned char *data, v;
|
||||
unsigned char *p;
|
||||
|
||||
size = BitStream_size(bstream);
|
||||
if(size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
data = (unsigned char *)malloc((size + 7) / 8);
|
||||
if(data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bytes = size / 8;
|
||||
|
||||
p = bstream->data;
|
||||
for(i=0; i<bytes; i++) {
|
||||
v = 0;
|
||||
for(j=0; j<8; j++) {
|
||||
v = v << 1;
|
||||
v |= *p;
|
||||
p++;
|
||||
}
|
||||
data[i] = v;
|
||||
}
|
||||
if(size & 7) {
|
||||
v = 0;
|
||||
for(j=0; j<(size & 7); j++) {
|
||||
v = v << 1;
|
||||
v |= *p;
|
||||
p++;
|
||||
}
|
||||
data[bytes] = v;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void BitStream_free(BitStream *bstream)
|
||||
{
|
||||
if(bstream != NULL) {
|
||||
free(bstream->data);
|
||||
free(bstream);
|
||||
}
|
||||
}
|
38
src/qqrencode/qrencode/bitstream.h
Normal file
38
src/qqrencode/qrencode/bitstream.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Binary sequence class.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __BITSTREAM_H__
|
||||
#define __BITSTREAM_H__
|
||||
|
||||
typedef struct {
|
||||
int length;
|
||||
unsigned char *data;
|
||||
} BitStream;
|
||||
|
||||
extern BitStream *BitStream_new(void);
|
||||
extern int BitStream_append(BitStream *bstream, BitStream *arg);
|
||||
extern int BitStream_appendNum(BitStream *bstream, int bits, unsigned int num);
|
||||
extern int BitStream_appendBytes(BitStream *bstream, int size, unsigned char *data);
|
||||
#define BitStream_size(__bstream__) (__bstream__->length)
|
||||
extern unsigned char *BitStream_toByte(BitStream *bstream);
|
||||
extern void BitStream_free(BitStream *bstream);
|
||||
|
||||
#endif /* __BITSTREAM_H__ */
|
330
src/qqrencode/qrencode/mask.c
Normal file
330
src/qqrencode/qrencode/mask.c
Normal file
|
@ -0,0 +1,330 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Masking.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "qrencode.h"
|
||||
#include "qrspec.h"
|
||||
#include "mask.h"
|
||||
|
||||
static int Mask_writeFormatInformation(int width, unsigned char *frame, int mask, QRecLevel level)
|
||||
{
|
||||
unsigned int format;
|
||||
unsigned char v;
|
||||
int i;
|
||||
int blacks = 0;
|
||||
|
||||
format = QRspec_getFormatInfo(mask, level);
|
||||
|
||||
for(i=0; i<8; i++) {
|
||||
if(format & 1) {
|
||||
blacks += 2;
|
||||
v = 0x85;
|
||||
} else {
|
||||
v = 0x84;
|
||||
}
|
||||
frame[width * 8 + width - 1 - i] = v;
|
||||
if(i < 6) {
|
||||
frame[width * i + 8] = v;
|
||||
} else {
|
||||
frame[width * (i + 1) + 8] = v;
|
||||
}
|
||||
format= format >> 1;
|
||||
}
|
||||
for(i=0; i<7; i++) {
|
||||
if(format & 1) {
|
||||
blacks += 2;
|
||||
v = 0x85;
|
||||
} else {
|
||||
v = 0x84;
|
||||
}
|
||||
frame[width * (width - 7 + i) + 8] = v;
|
||||
if(i == 0) {
|
||||
frame[width * 8 + 7] = v;
|
||||
} else {
|
||||
frame[width * 8 + 6 - i] = v;
|
||||
}
|
||||
format= format >> 1;
|
||||
}
|
||||
|
||||
return blacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Demerit coefficients.
|
||||
* See Section 8.8.2, pp.45, JIS X0510:2004.
|
||||
*/
|
||||
#define N1 (3)
|
||||
#define N2 (3)
|
||||
#define N3 (40)
|
||||
#define N4 (10)
|
||||
|
||||
#define MASKMAKER(__exp__) \
|
||||
int x, y;\
|
||||
int b = 0;\
|
||||
\
|
||||
for(y=0; y<width; y++) {\
|
||||
for(x=0; x<width; x++) {\
|
||||
if(*s & 0x80) {\
|
||||
*d = *s;\
|
||||
} else {\
|
||||
*d = *s ^ ((__exp__) == 0);\
|
||||
}\
|
||||
b += (int)(*d & 1);\
|
||||
s++; d++;\
|
||||
}\
|
||||
}\
|
||||
return b;
|
||||
|
||||
static int Mask_mask0(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER((x+y)&1)
|
||||
}
|
||||
|
||||
static int Mask_mask1(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER(y&1)
|
||||
}
|
||||
|
||||
static int Mask_mask2(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER(x%3)
|
||||
}
|
||||
|
||||
static int Mask_mask3(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER((x+y)%3)
|
||||
}
|
||||
|
||||
static int Mask_mask4(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER(((y/2)+(x/3))&1)
|
||||
}
|
||||
|
||||
static int Mask_mask5(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER(((x*y)&1)+(x*y)%3)
|
||||
}
|
||||
|
||||
static int Mask_mask6(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER((((x*y)&1)+(x*y)%3)&1)
|
||||
}
|
||||
|
||||
static int Mask_mask7(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER((((x*y)%3)+((x+y)&1))&1)
|
||||
}
|
||||
|
||||
#define maskNum (8)
|
||||
typedef int MaskMaker(int, const unsigned char *, unsigned char *);
|
||||
static MaskMaker *maskMakers[maskNum] = {
|
||||
Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3,
|
||||
Mask_mask4, Mask_mask5, Mask_mask6, Mask_mask7
|
||||
};
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame, int mask)
|
||||
{
|
||||
unsigned char *masked;
|
||||
|
||||
masked = (unsigned char *)malloc(width * width);
|
||||
if(masked == NULL) return NULL;
|
||||
|
||||
maskMakers[mask](width, frame, masked);
|
||||
|
||||
return masked;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned char *Mask_makeMask(int width, unsigned char *frame, int mask, QRecLevel level)
|
||||
{
|
||||
unsigned char *masked;
|
||||
|
||||
if(mask < 0 || mask >= maskNum) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
masked = (unsigned char *)malloc(width * width);
|
||||
if(masked == NULL) return NULL;
|
||||
|
||||
maskMakers[mask](width, frame, masked);
|
||||
Mask_writeFormatInformation(width, masked, mask, level);
|
||||
|
||||
return masked;
|
||||
}
|
||||
|
||||
|
||||
//static int n1;
|
||||
//static int n2;
|
||||
//static int n3;
|
||||
//static int n4;
|
||||
|
||||
static int Mask_calcN1N3(int length, int *runLength)
|
||||
{
|
||||
int i;
|
||||
int demerit = 0;
|
||||
int fact;
|
||||
|
||||
for(i=0; i<length; i++) {
|
||||
if(runLength[i] >= 5) {
|
||||
demerit += N1 + (runLength[i] - 5);
|
||||
//n1 += N1 + (runLength[i] - 5);
|
||||
}
|
||||
if((i & 1)) {
|
||||
if(i >= 3 && i < length-2 && (runLength[i] % 3) == 0) {
|
||||
fact = runLength[i] / 3;
|
||||
if(runLength[i-2] == fact &&
|
||||
runLength[i-1] == fact &&
|
||||
runLength[i+1] == fact &&
|
||||
runLength[i+2] == fact) {
|
||||
if(i == 3 || runLength[i-3] >= 4 * fact) {
|
||||
demerit += N3;
|
||||
//n3 += N3;
|
||||
} else if(i+4 >= length || runLength[i+3] >= 4 * fact) {
|
||||
demerit += N3;
|
||||
//n3 += N3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return demerit;
|
||||
}
|
||||
|
||||
static int Mask_calcN2(int width, unsigned char *frame)
|
||||
{
|
||||
int x, y;
|
||||
unsigned char *p;
|
||||
unsigned char b22, w22;
|
||||
int demerit = 0;
|
||||
|
||||
p = frame + width + 1;
|
||||
for(y=1; y<width; y++) {
|
||||
for(x=1; x<width; x++) {
|
||||
b22 = p[0] & p[-1] & p[-width] & p [-width-1];
|
||||
w22 = p[0] | p[-1] | p[-width] | p [-width-1];
|
||||
if((b22 | (w22 ^ 1))&1) {
|
||||
demerit += N2;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
return demerit;
|
||||
}
|
||||
|
||||
static int Mask_calcRunLength(int width, unsigned char *frame, int dir, int *runLength)
|
||||
{
|
||||
int head;
|
||||
int i;
|
||||
unsigned char *p;
|
||||
int pitch;
|
||||
|
||||
pitch = (dir==0)?1:width;
|
||||
if(frame[0] & 1) {
|
||||
runLength[0] = -1;
|
||||
head = 1;
|
||||
} else {
|
||||
head = 0;
|
||||
}
|
||||
runLength[head] = 1;
|
||||
p = frame + pitch;
|
||||
|
||||
for(i=1; i<width; i++) {
|
||||
if((p[0] ^ p[-pitch]) & 1) {
|
||||
head++;
|
||||
runLength[head] = 1;
|
||||
} else {
|
||||
runLength[head]++;
|
||||
}
|
||||
p += pitch;
|
||||
}
|
||||
|
||||
return head + 1;
|
||||
}
|
||||
|
||||
static int Mask_evaluateSymbol(int width, unsigned char *frame)
|
||||
{
|
||||
int x, y;
|
||||
int demerit = 0;
|
||||
int runLength[QRSPEC_WIDTH_MAX + 1];
|
||||
int length;
|
||||
|
||||
demerit += Mask_calcN2(width, frame);
|
||||
|
||||
for(y=0; y<width; y++) {
|
||||
length = Mask_calcRunLength(width, frame + y * width, 0, runLength);
|
||||
demerit += Mask_calcN1N3(length, runLength);
|
||||
}
|
||||
|
||||
for(x=0; x<width; x++) {
|
||||
length = Mask_calcRunLength(width, frame + x, 1, runLength);
|
||||
demerit += Mask_calcN1N3(length, runLength);
|
||||
}
|
||||
|
||||
return demerit;
|
||||
}
|
||||
|
||||
unsigned char *Mask_mask(int width, unsigned char *frame, QRecLevel level)
|
||||
{
|
||||
int i;
|
||||
unsigned char *mask, *bestMask;
|
||||
int minDemerit = INT_MAX;
|
||||
int blacks;
|
||||
int bratio;
|
||||
int demerit;
|
||||
int w2 = width * width;
|
||||
|
||||
mask = (unsigned char *)malloc(w2);
|
||||
if(mask == NULL) return NULL;
|
||||
bestMask = NULL;
|
||||
|
||||
for(i=0; i<maskNum; i++) {
|
||||
// n1 = n2 = n3 = n4 = 0;
|
||||
demerit = 0;
|
||||
blacks = maskMakers[i](width, frame, mask);
|
||||
blacks += Mask_writeFormatInformation(width, mask, i, level);
|
||||
bratio = (200 * blacks + w2) / w2 / 2; /* (int)(100*blacks/w2+0.5) */
|
||||
demerit = (abs(bratio - 50) / 5) * N4;
|
||||
// n4 = demerit;
|
||||
demerit += Mask_evaluateSymbol(width, mask);
|
||||
// printf("(%d,%d,%d,%d)=%d\n", n1, n2, n3 ,n4, demerit);
|
||||
if(demerit < minDemerit) {
|
||||
minDemerit = demerit;
|
||||
free(bestMask);
|
||||
bestMask = mask;
|
||||
mask = (unsigned char *)malloc(w2);
|
||||
if(mask == NULL) break;
|
||||
}
|
||||
}
|
||||
free(mask);
|
||||
return bestMask;
|
||||
}
|
37
src/qqrencode/qrencode/mask.h
Normal file
37
src/qqrencode/qrencode/mask.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Masking.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __MASK_H__
|
||||
#define __MASK_H__
|
||||
|
||||
extern unsigned char *Mask_makeMask(int width, unsigned char *frame, int mask, QRecLevel level);
|
||||
extern unsigned char *Mask_mask(int width, unsigned char *frame, QRecLevel level);
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
extern int Mask_calcN2(int width, unsigned char *frame);
|
||||
extern int Mask_calcN1N3(int length, int *runLength);
|
||||
extern int Mask_calcRunLength(int width, unsigned char *frame, int dir, int *runLength);
|
||||
extern int Mask_evaluateSymbol(int width, unsigned char *frame);
|
||||
extern int Mask_writeFormatInformation(int width, unsigned char *frame, int mask, QRecLevel level);
|
||||
extern unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame, int mask);
|
||||
#endif
|
||||
|
||||
#endif /* __MASK_H__ */
|
177
src/qqrencode/qrencode/mmask.c
Normal file
177
src/qqrencode/qrencode/mmask.c
Normal file
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Masking for Micro QR Code.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "qrencode.h"
|
||||
#include "mqrspec.h"
|
||||
#include "mmask.h"
|
||||
|
||||
static void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level)
|
||||
{
|
||||
unsigned int format;
|
||||
unsigned char v;
|
||||
int i;
|
||||
|
||||
format = MQRspec_getFormatInfo(mask, version, level);
|
||||
|
||||
for(i=0; i<8; i++) {
|
||||
v = 0x84 | (format & 1);
|
||||
frame[width * (i + 1) + 8] = v;
|
||||
format = format >> 1;
|
||||
}
|
||||
for(i=0; i<7; i++) {
|
||||
v = 0x84 | (format & 1);
|
||||
frame[width * 8 + 7 - i] = v;
|
||||
format = format >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
#define MASKMAKER(__exp__) \
|
||||
int x, y;\
|
||||
\
|
||||
for(y=0; y<width; y++) {\
|
||||
for(x=0; x<width; x++) {\
|
||||
if(*s & 0x80) {\
|
||||
*d = *s;\
|
||||
} else {\
|
||||
*d = *s ^ ((__exp__) == 0);\
|
||||
}\
|
||||
s++; d++;\
|
||||
}\
|
||||
}
|
||||
|
||||
static void Mask_mask0(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER(y&1)
|
||||
}
|
||||
|
||||
static void Mask_mask1(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER(((y/2)+(x/3))&1)
|
||||
}
|
||||
|
||||
static void Mask_mask2(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER((((x*y)&1)+(x*y)%3)&1)
|
||||
}
|
||||
|
||||
static void Mask_mask3(int width, const unsigned char *s, unsigned char *d)
|
||||
{
|
||||
MASKMAKER((((x+y)&1)+((x*y)%3))&1)
|
||||
}
|
||||
|
||||
#define maskNum (4)
|
||||
typedef void MaskMaker(int, const unsigned char *, unsigned char *);
|
||||
static MaskMaker *maskMakers[maskNum] = {
|
||||
Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3
|
||||
};
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask)
|
||||
{
|
||||
unsigned char *masked;
|
||||
|
||||
masked = (unsigned char *)malloc(width * width);
|
||||
if(masked == NULL) return NULL;
|
||||
|
||||
maskMakers[mask](width, frame, masked);
|
||||
|
||||
return masked;
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level)
|
||||
{
|
||||
unsigned char *masked;
|
||||
int width;
|
||||
|
||||
if(mask < 0 || mask >= maskNum) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
width = MQRspec_getWidth(version);
|
||||
masked = (unsigned char *)malloc(width * width);
|
||||
if(masked == NULL) return NULL;
|
||||
|
||||
maskMakers[mask](width, frame, masked);
|
||||
MMask_writeFormatInformation(version, width, masked, mask, level);
|
||||
|
||||
return masked;
|
||||
}
|
||||
|
||||
static int MMask_evaluateSymbol(int width, unsigned char *frame)
|
||||
{
|
||||
int x, y;
|
||||
unsigned char *p;
|
||||
int sum1 = 0, sum2 = 0;
|
||||
|
||||
p = frame + width * (width - 1);
|
||||
for(x=1; x<width; x++) {
|
||||
sum1 += (p[x] & 1);
|
||||
}
|
||||
|
||||
p = frame + width * 2 - 1;
|
||||
for(y=1; y<width; y++) {
|
||||
sum2 += (*p & 1);
|
||||
p += width;
|
||||
}
|
||||
|
||||
return (sum1 <= sum2)?(sum1 * 16 + sum2):(sum2 * 16 + sum1);
|
||||
}
|
||||
|
||||
unsigned char *MMask_mask(int version, unsigned char *frame, QRecLevel level)
|
||||
{
|
||||
int i;
|
||||
unsigned char *mask, *bestMask;
|
||||
int maxScore = 0;
|
||||
int score;
|
||||
int width;
|
||||
|
||||
width = MQRspec_getWidth(version);
|
||||
|
||||
mask = (unsigned char *)malloc(width * width);
|
||||
if(mask == NULL) return NULL;
|
||||
bestMask = NULL;
|
||||
|
||||
for(i=0; i<maskNum; i++) {
|
||||
score = 0;
|
||||
maskMakers[i](width, frame, mask);
|
||||
MMask_writeFormatInformation(version, width, mask, i, level);
|
||||
score = MMask_evaluateSymbol(width, mask);
|
||||
if(score > maxScore) {
|
||||
maxScore = score;
|
||||
free(bestMask);
|
||||
bestMask = mask;
|
||||
mask = (unsigned char *)malloc(width * width);
|
||||
if(mask == NULL) break;
|
||||
}
|
||||
}
|
||||
free(mask);
|
||||
return bestMask;
|
||||
}
|
34
src/qqrencode/qrencode/mmask.h
Normal file
34
src/qqrencode/qrencode/mmask.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Masking for Micro QR Code.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __MMASK_H__
|
||||
#define __MMASK_H__
|
||||
|
||||
extern unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level);
|
||||
extern unsigned char *MMask_mask(int version, unsigned char *frame, QRecLevel level);
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
extern int MMask_evaluateSymbol(int width, unsigned char *frame);
|
||||
extern void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level);
|
||||
extern unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask);
|
||||
#endif
|
||||
|
||||
#endif /* __MMASK_H__ */
|
280
src/qqrencode/qrencode/mqrspec.c
Normal file
280
src/qqrencode/qrencode/mqrspec.c
Normal file
|
@ -0,0 +1,280 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Micor QR Code specification in convenient format.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* The following data / specifications are taken from
|
||||
* "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
|
||||
* or
|
||||
* "Automatic identification and data capture techniques --
|
||||
* QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "mqrspec.h"
|
||||
|
||||
/******************************************************************************
|
||||
* Version and capacity
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int width; //< Edge length of the symbol
|
||||
int ec[4]; //< Number of ECC code (bytes)
|
||||
} MQRspec_Capacity;
|
||||
|
||||
/**
|
||||
* Table of the capacity of symbols
|
||||
* See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.
|
||||
*/
|
||||
static const MQRspec_Capacity mqrspecCapacity[MQRSPEC_VERSION_MAX + 1] = {
|
||||
{ 0, {0, 0, 0, 0}},
|
||||
{ 11, {2, 0, 0, 0}},
|
||||
{ 13, {5, 6, 0, 0}},
|
||||
{ 15, {6, 8, 0, 0}},
|
||||
{ 17, {8, 10, 14, 0}}
|
||||
};
|
||||
|
||||
int MQRspec_getDataLengthBit(int version, QRecLevel level)
|
||||
{
|
||||
int w;
|
||||
int ecc;
|
||||
|
||||
w = mqrspecCapacity[version].width - 1;
|
||||
ecc = mqrspecCapacity[version].ec[level];
|
||||
if(ecc == 0) return 0;
|
||||
return w * w - 64 - ecc * 8;
|
||||
}
|
||||
|
||||
int MQRspec_getDataLength(int version, QRecLevel level)
|
||||
{
|
||||
return (MQRspec_getDataLengthBit(version, level) + 4) / 8;
|
||||
}
|
||||
|
||||
int MQRspec_getECCLength(int version, QRecLevel level)
|
||||
{
|
||||
return mqrspecCapacity[version].ec[level];
|
||||
}
|
||||
|
||||
int MQRspec_getWidth(int version)
|
||||
{
|
||||
return mqrspecCapacity[version].width;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Length indicator
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.
|
||||
*/
|
||||
static const int lengthTableBits[4][4] = {
|
||||
{ 3, 4, 5, 6},
|
||||
{ 0, 3, 4, 5},
|
||||
{ 0, 0, 4, 5},
|
||||
{ 0, 0, 3, 4}
|
||||
};
|
||||
|
||||
int MQRspec_lengthIndicator(QRencodeMode mode, int version)
|
||||
{
|
||||
return lengthTableBits[mode][version - 1];
|
||||
}
|
||||
|
||||
int MQRspec_maximumWords(QRencodeMode mode, int version)
|
||||
{
|
||||
int bits;
|
||||
int words;
|
||||
|
||||
bits = lengthTableBits[mode][version - 1];
|
||||
words = (1 << bits) - 1;
|
||||
if(mode == QR_MODE_KANJI) {
|
||||
words *= 2; // the number of bytes is required
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Format information
|
||||
*****************************************************************************/
|
||||
|
||||
/* See calcFormatInfo in tests/test_mqrspec.c */
|
||||
static const unsigned int formatInfo[4][8] = {
|
||||
{0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3},
|
||||
{0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4},
|
||||
{0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d},
|
||||
{0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba}
|
||||
};
|
||||
|
||||
/* See Table 10 of Appendix 1. (pp.115) */
|
||||
static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {
|
||||
{-1, -1, -1},
|
||||
{ 0, -1, -1},
|
||||
{ 1, 2, -1},
|
||||
{ 3, 4, -1},
|
||||
{ 5, 6, 7}
|
||||
};
|
||||
|
||||
unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
|
||||
{
|
||||
int type;
|
||||
|
||||
if(mask < 0 || mask > 3) return 0;
|
||||
if(version <= 0 || version > MQRSPEC_VERSION_MAX) return 0;
|
||||
if(level == QR_ECLEVEL_H) return 0;
|
||||
type = typeTable[version][level];
|
||||
if(type < 0) return 0;
|
||||
|
||||
return formatInfo[mask][type];
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Frame
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Cache of initial frames.
|
||||
*/
|
||||
/* C99 says that static storage shall be initialized to a null pointer
|
||||
* by compiler. */
|
||||
static unsigned char *frames[MQRSPEC_VERSION_MAX + 1];
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Put a finder pattern.
|
||||
* @param frame
|
||||
* @param width
|
||||
* @param ox,oy upper-left coordinate of the pattern
|
||||
*/
|
||||
static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
|
||||
{
|
||||
static const unsigned char finder[] = {
|
||||
0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
|
||||
0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
|
||||
0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
|
||||
};
|
||||
int x, y;
|
||||
const unsigned char *s;
|
||||
|
||||
frame += oy * width + ox;
|
||||
s = finder;
|
||||
for(y=0; y<7; y++) {
|
||||
for(x=0; x<7; x++) {
|
||||
frame[x] = s[x];
|
||||
}
|
||||
frame += width;
|
||||
s += 7;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned char *MQRspec_createFrame(int version)
|
||||
{
|
||||
unsigned char *frame, *p, *q;
|
||||
int width;
|
||||
int x, y;
|
||||
|
||||
width = mqrspecCapacity[version].width;
|
||||
frame = (unsigned char *)malloc(width * width);
|
||||
if(frame == NULL) return NULL;
|
||||
|
||||
memset(frame, 0, width * width);
|
||||
/* Finder pattern */
|
||||
putFinderPattern(frame, width, 0, 0);
|
||||
/* Separator */
|
||||
p = frame;
|
||||
for(y=0; y<7; y++) {
|
||||
p[7] = 0xc0;
|
||||
p += width;
|
||||
}
|
||||
memset(frame + width * 7, 0xc0, 8);
|
||||
/* Mask format information area */
|
||||
memset(frame + width * 8 + 1, 0x84, 8);
|
||||
p = frame + width + 8;
|
||||
for(y=0; y<7; y++) {
|
||||
*p = 0x84;
|
||||
p += width;
|
||||
}
|
||||
/* Timing pattern */
|
||||
p = frame + 8;
|
||||
q = frame + width * 8;
|
||||
for(x=1; x<width-7; x++) {
|
||||
*p = 0x90 | (x & 1);
|
||||
*q = 0x90 | (x & 1);
|
||||
p++;
|
||||
q += width;
|
||||
}
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
unsigned char *MQRspec_newFrame(int version)
|
||||
{
|
||||
unsigned char *frame;
|
||||
int width;
|
||||
|
||||
if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;
|
||||
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_lock(&frames_mutex);
|
||||
#endif
|
||||
if(frames[version] == NULL) {
|
||||
frames[version] = MQRspec_createFrame(version);
|
||||
}
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_unlock(&frames_mutex);
|
||||
#endif
|
||||
if(frames[version] == NULL) return NULL;
|
||||
|
||||
width = mqrspecCapacity[version].width;
|
||||
frame = (unsigned char *)malloc(width * width);
|
||||
if(frame == NULL) return NULL;
|
||||
memcpy(frame, frames[version], width * width);
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
void MQRspec_clearCache(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_lock(&frames_mutex);
|
||||
#endif
|
||||
for(i=1; i<=MQRSPEC_VERSION_MAX; i++) {
|
||||
free(frames[i]);
|
||||
frames[i] = NULL;
|
||||
}
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_unlock(&frames_mutex);
|
||||
#endif
|
||||
}
|
157
src/qqrencode/qrencode/mqrspec.h
Normal file
157
src/qqrencode/qrencode/mqrspec.h
Normal file
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Micro QR Code specification in convenient format.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __MQRSPEC_H__
|
||||
#define __MQRSPEC_H__
|
||||
|
||||
#include "qrencode.h"
|
||||
|
||||
/******************************************************************************
|
||||
* Version and capacity
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Maximum width of a symbol
|
||||
*/
|
||||
#define MQRSPEC_WIDTH_MAX 17
|
||||
|
||||
/**
|
||||
* Return maximum data code length (bits) for the version.
|
||||
* @param version
|
||||
* @param level
|
||||
* @return maximum size (bits)
|
||||
*/
|
||||
extern int MQRspec_getDataLengthBit(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return maximum data code length (bytes) for the version.
|
||||
* @param version
|
||||
* @param level
|
||||
* @return maximum size (bytes)
|
||||
*/
|
||||
extern int MQRspec_getDataLength(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return maximum error correction code length (bytes) for the version.
|
||||
* @param version
|
||||
* @param level
|
||||
* @return ECC size (bytes)
|
||||
*/
|
||||
extern int MQRspec_getECCLength(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return a version number that satisfies the input code length.
|
||||
* @param size input code length (byte)
|
||||
* @param level
|
||||
* @return version number
|
||||
*/
|
||||
extern int MQRspec_getMinimumVersion(int size, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return the width of the symbol for the version.
|
||||
* @param version
|
||||
* @return width
|
||||
*/
|
||||
extern int MQRspec_getWidth(int version);
|
||||
|
||||
/**
|
||||
* Return the numer of remainder bits.
|
||||
* @param version
|
||||
* @return number of remainder bits
|
||||
*/
|
||||
extern int MQRspec_getRemainder(int version);
|
||||
|
||||
/******************************************************************************
|
||||
* Length indicator
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return the size of lenght indicator for the mode and version.
|
||||
* @param mode
|
||||
* @param version
|
||||
* @return the size of the appropriate length indicator (bits).
|
||||
*/
|
||||
extern int MQRspec_lengthIndicator(QRencodeMode mode, int version);
|
||||
|
||||
/**
|
||||
* Return the maximum length for the mode and version.
|
||||
* @param mode
|
||||
* @param version
|
||||
* @return the maximum length (bytes)
|
||||
*/
|
||||
extern int MQRspec_maximumWords(QRencodeMode mode, int version);
|
||||
|
||||
/******************************************************************************
|
||||
* Version information pattern
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return BCH encoded version information pattern that is used for the symbol
|
||||
* of version 7 or greater. Use lower 18 bits.
|
||||
* @param version
|
||||
* @return BCH encoded version information pattern
|
||||
*/
|
||||
extern unsigned int MQRspec_getVersionPattern(int version);
|
||||
|
||||
/******************************************************************************
|
||||
* Format information
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return BCH encoded format information pattern.
|
||||
* @param mask
|
||||
* @param version
|
||||
* @param level
|
||||
* @return BCH encoded format information pattern
|
||||
*/
|
||||
extern unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level);
|
||||
|
||||
/******************************************************************************
|
||||
* Frame
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return a copy of initialized frame.
|
||||
* When the same version is requested twice or more, a copy of cached frame
|
||||
* is returned.
|
||||
* @param version
|
||||
* @return Array of unsigned char. You can free it by free().
|
||||
*/
|
||||
extern unsigned char *MQRspec_newFrame(int version);
|
||||
|
||||
/**
|
||||
* Clear the frame cache. Typically for debug.
|
||||
*/
|
||||
extern void MQRspec_clearCache(void);
|
||||
|
||||
/******************************************************************************
|
||||
* Mode indicator
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Mode indicator. See Table 2 in Appendix 1 of JIS X0510:2004, pp.107.
|
||||
*/
|
||||
#define MQRSPEC_MODEID_NUM 0
|
||||
#define MQRSPEC_MODEID_AN 1
|
||||
#define MQRSPEC_MODEID_8 2
|
||||
#define MQRSPEC_MODEID_KANJI 3
|
||||
|
||||
#endif /* __MQRSPEC_H__ */
|
929
src/qqrencode/qrencode/qrencode.c
Normal file
929
src/qqrencode/qrencode/qrencode.c
Normal file
|
@ -0,0 +1,929 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "qrencode.h"
|
||||
#include "qrspec.h"
|
||||
#include "mqrspec.h"
|
||||
#include "bitstream.h"
|
||||
#include "qrinput.h"
|
||||
#include "rscode.h"
|
||||
#include "split.h"
|
||||
#include "mask.h"
|
||||
#include "mmask.h"
|
||||
|
||||
/******************************************************************************
|
||||
* Raw code
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int dataLength;
|
||||
unsigned char *data;
|
||||
int eccLength;
|
||||
unsigned char *ecc;
|
||||
} RSblock;
|
||||
|
||||
typedef struct {
|
||||
int version;
|
||||
int dataLength;
|
||||
int eccLength;
|
||||
unsigned char *datacode;
|
||||
unsigned char *ecccode;
|
||||
int b1;
|
||||
int blocks;
|
||||
RSblock *rsblock;
|
||||
int count;
|
||||
} QRRawCode;
|
||||
|
||||
static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs)
|
||||
{
|
||||
block->dataLength = dl;
|
||||
block->data = data;
|
||||
block->eccLength = el;
|
||||
block->ecc = ecc;
|
||||
|
||||
encode_rs_char(rs, data, ecc);
|
||||
}
|
||||
|
||||
static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
|
||||
{
|
||||
int i;
|
||||
RSblock *block;
|
||||
unsigned char *dp, *ep;
|
||||
RS *rs;
|
||||
int el, dl;
|
||||
|
||||
dl = QRspec_rsDataCodes1(spec);
|
||||
el = QRspec_rsEccCodes1(spec);
|
||||
rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
|
||||
if(rs == NULL) return -1;
|
||||
|
||||
block = blocks;
|
||||
dp = data;
|
||||
ep = ecc;
|
||||
for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
|
||||
RSblock_initBlock(block, dl, dp, el, ep, rs);
|
||||
dp += dl;
|
||||
ep += el;
|
||||
block++;
|
||||
}
|
||||
|
||||
if(QRspec_rsBlockNum2(spec) == 0) return 0;
|
||||
|
||||
dl = QRspec_rsDataCodes2(spec);
|
||||
el = QRspec_rsEccCodes2(spec);
|
||||
rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
|
||||
if(rs == NULL) return -1;
|
||||
for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
|
||||
RSblock_initBlock(block, dl, dp, el, ep, rs);
|
||||
dp += dl;
|
||||
ep += el;
|
||||
block++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void QRraw_free(QRRawCode *raw);
|
||||
static QRRawCode *QRraw_new(QRinput *input)
|
||||
{
|
||||
QRRawCode *raw;
|
||||
int spec[5], ret;
|
||||
|
||||
raw = (QRRawCode *)malloc(sizeof(QRRawCode));
|
||||
if(raw == NULL) return NULL;
|
||||
|
||||
raw->datacode = QRinput_getByteStream(input);
|
||||
if(raw->datacode == NULL) {
|
||||
free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QRspec_getEccSpec(input->version, input->level, spec);
|
||||
|
||||
raw->version = input->version;
|
||||
raw->b1 = QRspec_rsBlockNum1(spec);
|
||||
raw->dataLength = QRspec_rsDataLength(spec);
|
||||
raw->eccLength = QRspec_rsEccLength(spec);
|
||||
raw->ecccode = (unsigned char *)malloc(raw->eccLength);
|
||||
if(raw->ecccode == NULL) {
|
||||
free(raw->datacode);
|
||||
free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
raw->blocks = QRspec_rsBlockNum(spec);
|
||||
raw->rsblock = (RSblock *)calloc(raw->blocks, sizeof(RSblock));
|
||||
if(raw->rsblock == NULL) {
|
||||
QRraw_free(raw);
|
||||
return NULL;
|
||||
}
|
||||
ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
|
||||
if(ret < 0) {
|
||||
QRraw_free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
raw->count = 0;
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a code (byte).
|
||||
* This function can be called iteratively.
|
||||
* @param raw raw code.
|
||||
* @return code
|
||||
*/
|
||||
static unsigned char QRraw_getCode(QRRawCode *raw)
|
||||
{
|
||||
int col, row;
|
||||
unsigned char ret;
|
||||
|
||||
if(raw->count < raw->dataLength) {
|
||||
row = raw->count % raw->blocks;
|
||||
col = raw->count / raw->blocks;
|
||||
if(col >= raw->rsblock[0].dataLength) {
|
||||
row += raw->b1;
|
||||
}
|
||||
ret = raw->rsblock[row].data[col];
|
||||
} else if(raw->count < raw->dataLength + raw->eccLength) {
|
||||
row = (raw->count - raw->dataLength) % raw->blocks;
|
||||
col = (raw->count - raw->dataLength) / raw->blocks;
|
||||
ret = raw->rsblock[row].ecc[col];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
raw->count++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void QRraw_free(QRRawCode *raw)
|
||||
{
|
||||
if(raw != NULL) {
|
||||
free(raw->datacode);
|
||||
free(raw->ecccode);
|
||||
free(raw->rsblock);
|
||||
free(raw);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Raw code for Micro QR Code
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int version;
|
||||
int dataLength;
|
||||
int eccLength;
|
||||
unsigned char *datacode;
|
||||
unsigned char *ecccode;
|
||||
RSblock *rsblock;
|
||||
int oddbits;
|
||||
int count;
|
||||
} MQRRawCode;
|
||||
|
||||
static void MQRraw_free(MQRRawCode *raw);
|
||||
static MQRRawCode *MQRraw_new(QRinput *input)
|
||||
{
|
||||
MQRRawCode *raw;
|
||||
RS *rs;
|
||||
|
||||
raw = (MQRRawCode *)malloc(sizeof(MQRRawCode));
|
||||
if(raw == NULL) return NULL;
|
||||
|
||||
raw->version = input->version;
|
||||
raw->dataLength = MQRspec_getDataLength(input->version, input->level);
|
||||
raw->eccLength = MQRspec_getECCLength(input->version, input->level);
|
||||
raw->oddbits = raw->dataLength * 8 - MQRspec_getDataLengthBit(input->version, input->level);
|
||||
raw->datacode = QRinput_getByteStream(input);
|
||||
if(raw->datacode == NULL) {
|
||||
free(raw);
|
||||
return NULL;
|
||||
}
|
||||
raw->ecccode = (unsigned char *)malloc(raw->eccLength);
|
||||
if(raw->ecccode == NULL) {
|
||||
free(raw->datacode);
|
||||
free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
raw->rsblock = (RSblock *)calloc(1, sizeof(RSblock));
|
||||
if(raw->rsblock == NULL) {
|
||||
MQRraw_free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rs = init_rs(8, 0x11d, 0, 1, raw->eccLength, 255 - raw->dataLength - raw->eccLength);
|
||||
if(rs == NULL) {
|
||||
MQRraw_free(raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RSblock_initBlock(raw->rsblock, raw->dataLength, raw->datacode, raw->eccLength, raw->ecccode, rs);
|
||||
|
||||
raw->count = 0;
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a code (byte).
|
||||
* This function can be called iteratively.
|
||||
* @param raw raw code.
|
||||
* @return code
|
||||
*/
|
||||
static unsigned char MQRraw_getCode(MQRRawCode *raw)
|
||||
{
|
||||
unsigned char ret;
|
||||
|
||||
if(raw->count < raw->dataLength) {
|
||||
ret = raw->datacode[raw->count];
|
||||
} else if(raw->count < raw->dataLength + raw->eccLength) {
|
||||
ret = raw->ecccode[raw->count - raw->dataLength];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
raw->count++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void MQRraw_free(MQRRawCode *raw)
|
||||
{
|
||||
if(raw != NULL) {
|
||||
free(raw->datacode);
|
||||
free(raw->ecccode);
|
||||
free(raw->rsblock);
|
||||
free(raw);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Frame filling
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int width;
|
||||
unsigned char *frame;
|
||||
int x, y;
|
||||
int dir;
|
||||
int bit;
|
||||
int mqr;
|
||||
} FrameFiller;
|
||||
|
||||
static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr)
|
||||
{
|
||||
FrameFiller *filler;
|
||||
|
||||
filler = (FrameFiller *)malloc(sizeof(FrameFiller));
|
||||
if(filler == NULL) return NULL;
|
||||
filler->width = width;
|
||||
filler->frame = frame;
|
||||
filler->x = width - 1;
|
||||
filler->y = width - 1;
|
||||
filler->dir = -1;
|
||||
filler->bit = -1;
|
||||
filler->mqr = mqr;
|
||||
|
||||
return filler;
|
||||
}
|
||||
|
||||
static unsigned char *FrameFiller_next(FrameFiller *filler)
|
||||
{
|
||||
unsigned char *p;
|
||||
int x, y, w;
|
||||
|
||||
if(filler->bit == -1) {
|
||||
filler->bit = 0;
|
||||
return filler->frame + filler->y * filler->width + filler->x;
|
||||
}
|
||||
|
||||
x = filler->x;
|
||||
y = filler->y;
|
||||
p = filler->frame;
|
||||
w = filler->width;
|
||||
|
||||
if(filler->bit == 0) {
|
||||
x--;
|
||||
filler->bit++;
|
||||
} else {
|
||||
x++;
|
||||
y += filler->dir;
|
||||
filler->bit--;
|
||||
}
|
||||
|
||||
if(filler->dir < 0) {
|
||||
if(y < 0) {
|
||||
y = 0;
|
||||
x -= 2;
|
||||
filler->dir = 1;
|
||||
if(!filler->mqr && x == 6) {
|
||||
x--;
|
||||
y = 9;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(y == w) {
|
||||
y = w - 1;
|
||||
x -= 2;
|
||||
filler->dir = -1;
|
||||
if(!filler->mqr && x == 6) {
|
||||
x--;
|
||||
y -= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(x < 0 || y < 0) return NULL;
|
||||
|
||||
filler->x = x;
|
||||
filler->y = y;
|
||||
|
||||
if(p[y * w + x] & 0x80) {
|
||||
// This tail recursion could be optimized.
|
||||
return FrameFiller_next(filler);
|
||||
}
|
||||
return &p[y * w + x];
|
||||
}
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
extern unsigned char *FrameFiller_test(int version)
|
||||
{
|
||||
int width;
|
||||
unsigned char *frame, *p;
|
||||
FrameFiller *filler;
|
||||
int i, length;
|
||||
|
||||
width = QRspec_getWidth(version);
|
||||
frame = QRspec_newFrame(version);
|
||||
if(frame == NULL) return NULL;
|
||||
filler = FrameFiller_new(width, frame, 0);
|
||||
if(filler == NULL) {
|
||||
free(frame);
|
||||
return NULL;
|
||||
}
|
||||
length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8
|
||||
+ QRspec_getECCLength(version, QR_ECLEVEL_L) * 8
|
||||
+ QRspec_getRemainder(version);
|
||||
for(i=0; i<length; i++) {
|
||||
p = FrameFiller_next(filler);
|
||||
if(p == NULL) {
|
||||
free(filler);
|
||||
free(frame);
|
||||
return NULL;
|
||||
}
|
||||
*p = (unsigned char)(i & 0x7f) | 0x80;
|
||||
}
|
||||
free(filler);
|
||||
return frame;
|
||||
}
|
||||
|
||||
extern unsigned char *FrameFiller_testMQR(int version)
|
||||
{
|
||||
int width;
|
||||
unsigned char *frame, *p;
|
||||
FrameFiller *filler;
|
||||
int i, length;
|
||||
|
||||
width = MQRspec_getWidth(version);
|
||||
frame = MQRspec_newFrame(version);
|
||||
if(frame == NULL) return NULL;
|
||||
filler = FrameFiller_new(width, frame, 1);
|
||||
if(filler == NULL) {
|
||||
free(frame);
|
||||
return NULL;
|
||||
}
|
||||
length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L)
|
||||
+ MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8;
|
||||
for(i=0; i<length; i++) {
|
||||
p = FrameFiller_next(filler);
|
||||
if(p == NULL) {
|
||||
fprintf(stderr, "Frame filler run over the frame!\n");
|
||||
free(filler);
|
||||
return frame;
|
||||
}
|
||||
*p = (unsigned char)(i & 0x7f) | 0x80;
|
||||
}
|
||||
free(filler);
|
||||
return frame;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* QR-code encoding
|
||||
*****************************************************************************/
|
||||
|
||||
static QRcode *QRcode_new(int version, int width, unsigned char *data)
|
||||
{
|
||||
QRcode *qrcode;
|
||||
|
||||
qrcode = (QRcode *)malloc(sizeof(QRcode));
|
||||
if(qrcode == NULL) return NULL;
|
||||
|
||||
qrcode->version = version;
|
||||
qrcode->width = width;
|
||||
qrcode->data = data;
|
||||
|
||||
return qrcode;
|
||||
}
|
||||
|
||||
void QRcode_free(QRcode *qrcode)
|
||||
{
|
||||
if(qrcode != NULL) {
|
||||
free(qrcode->data);
|
||||
free(qrcode);
|
||||
}
|
||||
}
|
||||
|
||||
static QRcode *QRcode_encodeMask(QRinput *input, int mask)
|
||||
{
|
||||
int width, version;
|
||||
QRRawCode *raw;
|
||||
unsigned char *frame, *masked, *p, code, bit;
|
||||
FrameFiller *filler;
|
||||
int i, j;
|
||||
QRcode *qrcode = NULL;
|
||||
|
||||
if(input->mqr) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if(input->level > QR_ECLEVEL_H) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
raw = QRraw_new(input);
|
||||
if(raw == NULL) return NULL;
|
||||
|
||||
version = raw->version;
|
||||
width = QRspec_getWidth(version);
|
||||
frame = QRspec_newFrame(version);
|
||||
if(frame == NULL) {
|
||||
QRraw_free(raw);
|
||||
return NULL;
|
||||
}
|
||||
filler = FrameFiller_new(width, frame, 0);
|
||||
if(filler == NULL) {
|
||||
QRraw_free(raw);
|
||||
free(frame);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* inteleaved data and ecc codes */
|
||||
for(i=0; i<raw->dataLength + raw->eccLength; i++) {
|
||||
code = QRraw_getCode(raw);
|
||||
bit = 0x80;
|
||||
for(j=0; j<8; j++) {
|
||||
p = FrameFiller_next(filler);
|
||||
if(p == NULL) goto EXIT;
|
||||
*p = 0x02 | ((bit & code) != 0);
|
||||
bit = bit >> 1;
|
||||
}
|
||||
}
|
||||
QRraw_free(raw);
|
||||
raw = NULL;
|
||||
/* remainder bits */
|
||||
j = QRspec_getRemainder(version);
|
||||
for(i=0; i<j; i++) {
|
||||
p = FrameFiller_next(filler);
|
||||
if(p == NULL) goto EXIT;
|
||||
*p = 0x02;
|
||||
}
|
||||
|
||||
/* masking */
|
||||
if(mask == -2) { // just for debug purpose
|
||||
masked = (unsigned char *)malloc(width * width);
|
||||
memcpy(masked, frame, width * width);
|
||||
} else if(mask < 0) {
|
||||
masked = Mask_mask(width, frame, input->level);
|
||||
} else {
|
||||
masked = Mask_makeMask(width, frame, mask, input->level);
|
||||
}
|
||||
if(masked == NULL) {
|
||||
goto EXIT;
|
||||
}
|
||||
qrcode = QRcode_new(version, width, masked);
|
||||
|
||||
EXIT:
|
||||
QRraw_free(raw);
|
||||
free(filler);
|
||||
free(frame);
|
||||
return qrcode;
|
||||
}
|
||||
|
||||
static QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)
|
||||
{
|
||||
int width, version;
|
||||
MQRRawCode *raw;
|
||||
unsigned char *frame, *masked, *p, code, bit;
|
||||
FrameFiller *filler;
|
||||
int i, j;
|
||||
QRcode *qrcode = NULL;
|
||||
|
||||
if(!input->mqr) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if(input->level > QR_ECLEVEL_Q) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
raw = MQRraw_new(input);
|
||||
if(raw == NULL) return NULL;
|
||||
|
||||
version = raw->version;
|
||||
width = MQRspec_getWidth(version);
|
||||
frame = MQRspec_newFrame(version);
|
||||
if(frame == NULL) {
|
||||
MQRraw_free(raw);
|
||||
return NULL;
|
||||
}
|
||||
filler = FrameFiller_new(width, frame, 1);
|
||||
if(filler == NULL) {
|
||||
MQRraw_free(raw);
|
||||
free(frame);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* inteleaved data and ecc codes */
|
||||
for(i=0; i<raw->dataLength + raw->eccLength; i++) {
|
||||
code = MQRraw_getCode(raw);
|
||||
if(raw->oddbits && i == raw->dataLength - 1) {
|
||||
bit = 1 << (raw->oddbits - 1);
|
||||
for(j=0; j<raw->oddbits; j++) {
|
||||
p = FrameFiller_next(filler);
|
||||
if(p == NULL) goto EXIT;
|
||||
*p = 0x02 | ((bit & code) != 0);
|
||||
bit = bit >> 1;
|
||||
}
|
||||
} else {
|
||||
bit = 0x80;
|
||||
for(j=0; j<8; j++) {
|
||||
p = FrameFiller_next(filler);
|
||||
if(p == NULL) goto EXIT;
|
||||
*p = 0x02 | ((bit & code) != 0);
|
||||
bit = bit >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
MQRraw_free(raw);
|
||||
raw = NULL;
|
||||
|
||||
/* masking */
|
||||
if(mask < 0) {
|
||||
masked = MMask_mask(version, frame, input->level);
|
||||
} else {
|
||||
masked = MMask_makeMask(version, frame, mask, input->level);
|
||||
}
|
||||
if(masked == NULL) {
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
qrcode = QRcode_new(version, width, masked);
|
||||
|
||||
EXIT:
|
||||
MQRraw_free(raw);
|
||||
free(filler);
|
||||
free(frame);
|
||||
return qrcode;
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeInput(QRinput *input)
|
||||
{
|
||||
if(input->mqr) {
|
||||
return QRcode_encodeMaskMQR(input, -1);
|
||||
} else {
|
||||
return QRcode_encodeMask(input, -1);
|
||||
}
|
||||
}
|
||||
|
||||
static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int mqr, QRencodeMode hint, int casesensitive)
|
||||
{
|
||||
QRinput *input;
|
||||
QRcode *code;
|
||||
int ret;
|
||||
|
||||
if(string == NULL) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(mqr) {
|
||||
input = QRinput_newMQR(version, level);
|
||||
} else {
|
||||
input = QRinput_new2(version, level);
|
||||
}
|
||||
if(input == NULL) return NULL;
|
||||
|
||||
ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
|
||||
if(ret < 0) {
|
||||
QRinput_free(input);
|
||||
return NULL;
|
||||
}
|
||||
code = QRcode_encodeInput(input);
|
||||
QRinput_free(input);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
|
||||
{
|
||||
return QRcode_encodeStringReal(string, version, level, 0, hint, casesensitive);
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
|
||||
{
|
||||
return QRcode_encodeStringReal(string, version, level, 1, hint, casesensitive);
|
||||
}
|
||||
|
||||
static QRcode *QRcode_encodeDataReal(const unsigned char *data, int length, int version, QRecLevel level, int mqr)
|
||||
{
|
||||
QRinput *input;
|
||||
QRcode *code;
|
||||
int ret;
|
||||
|
||||
if(data == NULL || length == 0) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(mqr) {
|
||||
input = QRinput_newMQR(version, level);
|
||||
} else {
|
||||
input = QRinput_new2(version, level);
|
||||
}
|
||||
if(input == NULL) return NULL;
|
||||
|
||||
ret = QRinput_append(input, QR_MODE_8, length, data);
|
||||
if(ret < 0) {
|
||||
QRinput_free(input);
|
||||
return NULL;
|
||||
}
|
||||
code = QRcode_encodeInput(input);
|
||||
QRinput_free(input);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level)
|
||||
{
|
||||
return QRcode_encodeDataReal(data, size, version, level, 0);
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
|
||||
{
|
||||
if(string == NULL) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 0);
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level)
|
||||
{
|
||||
return QRcode_encodeDataReal(data, size, version, level, 1);
|
||||
}
|
||||
|
||||
QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level)
|
||||
{
|
||||
if(string == NULL) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 1);
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Structured QR-code encoding
|
||||
*****************************************************************************/
|
||||
|
||||
static QRcode_List *QRcode_List_newEntry(void)
|
||||
{
|
||||
QRcode_List *entry;
|
||||
|
||||
entry = (QRcode_List *)malloc(sizeof(QRcode_List));
|
||||
if(entry == NULL) return NULL;
|
||||
|
||||
entry->next = NULL;
|
||||
entry->code = NULL;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static void QRcode_List_freeEntry(QRcode_List *entry)
|
||||
{
|
||||
if(entry != NULL) {
|
||||
QRcode_free(entry->code);
|
||||
free(entry);
|
||||
}
|
||||
}
|
||||
|
||||
void QRcode_List_free(QRcode_List *qrlist)
|
||||
{
|
||||
QRcode_List *list = qrlist, *next;
|
||||
|
||||
while(list != NULL) {
|
||||
next = list->next;
|
||||
QRcode_List_freeEntry(list);
|
||||
list = next;
|
||||
}
|
||||
}
|
||||
|
||||
int QRcode_List_size(QRcode_List *qrlist)
|
||||
{
|
||||
QRcode_List *list = qrlist;
|
||||
int size = 0;
|
||||
|
||||
while(list != NULL) {
|
||||
size++;
|
||||
list = list->next;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static unsigned char QRcode_parity(const char *str, int size)
|
||||
{
|
||||
unsigned char parity = 0;
|
||||
int i;
|
||||
|
||||
for(i=0; i<size; i++) {
|
||||
parity ^= str[i];
|
||||
}
|
||||
|
||||
return parity;
|
||||
}
|
||||
#endif
|
||||
|
||||
QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s)
|
||||
{
|
||||
QRcode_List *head = NULL;
|
||||
QRcode_List *tail = NULL;
|
||||
QRcode_List *entry;
|
||||
QRinput_InputList *list = s->head;
|
||||
|
||||
while(list != NULL) {
|
||||
if(head == NULL) {
|
||||
entry = QRcode_List_newEntry();
|
||||
if(entry == NULL) goto ABORT;
|
||||
head = entry;
|
||||
tail = head;
|
||||
} else {
|
||||
entry = QRcode_List_newEntry();
|
||||
if(entry == NULL) goto ABORT;
|
||||
tail->next = entry;
|
||||
tail = tail->next;
|
||||
}
|
||||
tail->code = QRcode_encodeInput(list->input);
|
||||
if(tail->code == NULL) {
|
||||
goto ABORT;
|
||||
}
|
||||
list = list->next;
|
||||
}
|
||||
|
||||
return head;
|
||||
ABORT:
|
||||
QRcode_List_free(head);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
|
||||
{
|
||||
QRinput_Struct *s;
|
||||
QRcode_List *codes;
|
||||
|
||||
s = QRinput_splitQRinputToStruct(input);
|
||||
if(s == NULL) return NULL;
|
||||
|
||||
codes = QRcode_encodeInputStructured(s);
|
||||
QRinput_Struct_free(s);
|
||||
|
||||
return codes;
|
||||
}
|
||||
|
||||
static QRcode_List *QRcode_encodeDataStructuredReal(
|
||||
int size, const unsigned char *data,
|
||||
int version, QRecLevel level,
|
||||
int eightbit, QRencodeMode hint, int casesensitive)
|
||||
{
|
||||
QRinput *input;
|
||||
QRcode_List *codes;
|
||||
int ret;
|
||||
|
||||
if(version <= 0) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
input = QRinput_new2(version, level);
|
||||
if(input == NULL) return NULL;
|
||||
|
||||
if(eightbit) {
|
||||
ret = QRinput_append(input, QR_MODE_8, size, data);
|
||||
} else {
|
||||
ret = Split_splitStringToQRinput((char *)data, input, hint, casesensitive);
|
||||
}
|
||||
if(ret < 0) {
|
||||
QRinput_free(input);
|
||||
return NULL;
|
||||
}
|
||||
codes = QRcode_encodeInputToStructured(input);
|
||||
QRinput_free(input);
|
||||
|
||||
return codes;
|
||||
}
|
||||
|
||||
QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level) {
|
||||
return QRcode_encodeDataStructuredReal(size, data, version, level, 1, QR_MODE_NUL, 0);
|
||||
}
|
||||
|
||||
QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level) {
|
||||
if(string == NULL) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
return QRcode_encodeDataStructured(strlen(string), (unsigned char *)string, version, level);
|
||||
}
|
||||
|
||||
QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
|
||||
{
|
||||
if(string == NULL) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
return QRcode_encodeDataStructuredReal(strlen(string), (unsigned char *)string, version, level, 0, hint, casesensitive);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* System utilities
|
||||
*****************************************************************************/
|
||||
|
||||
void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version)
|
||||
{
|
||||
if(major_version != NULL) {
|
||||
*major_version = 3;
|
||||
}
|
||||
if(minor_version != NULL) {
|
||||
*minor_version = 4;
|
||||
}
|
||||
if(micro_version != NULL) {
|
||||
*micro_version = 3;
|
||||
}
|
||||
}
|
||||
|
||||
char *QRcode_APIVersionString(void)
|
||||
{
|
||||
return "3.4.3";
|
||||
}
|
||||
|
||||
void QRcode_clearCache(void)
|
||||
{
|
||||
QRspec_clearCache();
|
||||
MQRspec_clearCache();
|
||||
free_rs_cache();
|
||||
}
|
566
src/qqrencode/qrencode/qrencode.h
Normal file
566
src/qqrencode/qrencode/qrencode.h
Normal file
|
@ -0,0 +1,566 @@
|
|||
/**
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/** \mainpage
|
||||
* Libqrencode is a library for encoding data in a QR Code symbol, a kind of 2D
|
||||
* symbology.
|
||||
*
|
||||
* \section encoding Encoding
|
||||
*
|
||||
* There are two methods to encode data: <b>encoding a string/data</b> or
|
||||
* <b>encoding a structured data</b>.
|
||||
*
|
||||
* \subsection encoding-string Encoding a string/data
|
||||
* You can encode a string by calling QRcode_encodeString().
|
||||
* The given string is parsed automatically and encoded. If you want to encode
|
||||
* data that can be represented as a C string style (NUL terminated), you can
|
||||
* simply use this way.
|
||||
*
|
||||
* If the input data contains Kanji (Shift-JIS) characters and you want to
|
||||
* encode them as Kanji in QR Code, you should give QR_MODE_KANJI as a hint.
|
||||
* Otherwise, all of non-alphanumeric characters are encoded as 8 bit data.
|
||||
* If you want to encode a whole string in 8 bit mode, you can use
|
||||
* QRcode_encodeString8bit() instead.
|
||||
*
|
||||
* Please note that a C string can not contain NUL characters. If your data
|
||||
* contains NUL, you must use QRcode_encodeData().
|
||||
*
|
||||
* \subsection encoding-input Encoding a structured data
|
||||
* You can construct a structured input data manually. If the structure of the
|
||||
* input data is known, you can use this way.
|
||||
* At first, create a ::QRinput object by QRinput_new(). Then add input data
|
||||
* to the QRinput object by QRinput_append(). Finally call QRcode_encodeInput()
|
||||
* to encode the QRinput data.
|
||||
* You can reuse the QRinput data again to encode it in other symbols with
|
||||
* different parameters.
|
||||
*
|
||||
* \section result Result
|
||||
* The encoded symbol is resulted as a ::QRcode object. It will contain
|
||||
* its version number, width of the symbol and an array represents the symbol.
|
||||
* See ::QRcode for the details. You can free the object by QRcode_free().
|
||||
*
|
||||
* Please note that the version of the result may be larger than specified.
|
||||
* In such cases, the input data would be too large to be encoded in a
|
||||
* symbol of the specified version.
|
||||
*
|
||||
* \section structured Structured append
|
||||
* Libqrencode can generate "Structured-appended" symbols that enables to split
|
||||
* a large data set into mulitple QR codes. A QR code reader concatenates
|
||||
* multiple QR code symbols into a string.
|
||||
* Just like QRcode_encodeString(), you can use QRcode_encodeStringStructured()
|
||||
* to generate structured-appended symbols. This functions returns an instance
|
||||
* of ::QRcode_List. The returned list is a singly-linked list of QRcode: you
|
||||
* can retrieve each QR code in this way:
|
||||
*
|
||||
* \code
|
||||
* QRcode_List *qrcodes;
|
||||
* QRcode_List *entry;
|
||||
* QRcode *qrcode;
|
||||
*
|
||||
* qrcodes = QRcode_encodeStringStructured(...);
|
||||
* entry = qrcodes;
|
||||
* while(entry != NULL) {
|
||||
* qrcode = entry->code;
|
||||
* // do something
|
||||
* entry = entry->next;
|
||||
* }
|
||||
* QRcode_List_free(entry);
|
||||
* \endcode
|
||||
*
|
||||
* Instead of using auto-parsing functions, you can construct your own
|
||||
* structured input. At first, instantiate an object of ::QRinput_Struct
|
||||
* by calling QRinput_Struct_new(). This object can hold multiple ::QRinput,
|
||||
* and one QR code is generated for a ::QRinput.
|
||||
* QRinput_Struct_appendInput() appends a ::QRinput to a ::QRinput_Struct
|
||||
* object. In order to generate structured-appended symbols, it is required to
|
||||
* embed headers to each symbol. You can use
|
||||
* QRinput_Struct_insertStructuredAppendHeaders() to insert appropriate
|
||||
* headers to each symbol. You should call this function just once before
|
||||
* encoding symbols.
|
||||
*/
|
||||
|
||||
#ifndef __QRENCODE_H__
|
||||
#define __QRENCODE_H__
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Encoding mode.
|
||||
*/
|
||||
typedef enum {
|
||||
QR_MODE_NUL = -1, ///< Terminator (NUL character). Internal use only
|
||||
QR_MODE_NUM = 0, ///< Numeric mode
|
||||
QR_MODE_AN, ///< Alphabet-numeric mode
|
||||
QR_MODE_8, ///< 8-bit data mode
|
||||
QR_MODE_KANJI, ///< Kanji (shift-jis) mode
|
||||
QR_MODE_STRUCTURE, ///< Internal use only
|
||||
QR_MODE_ECI, ///< ECI mode
|
||||
QR_MODE_FNC1FIRST, ///< FNC1, first position
|
||||
QR_MODE_FNC1SECOND, ///< FNC1, second position
|
||||
} QRencodeMode;
|
||||
|
||||
/**
|
||||
* Level of error correction.
|
||||
*/
|
||||
typedef enum {
|
||||
QR_ECLEVEL_L = 0, ///< lowest
|
||||
QR_ECLEVEL_M,
|
||||
QR_ECLEVEL_Q,
|
||||
QR_ECLEVEL_H ///< highest
|
||||
} QRecLevel;
|
||||
|
||||
/**
|
||||
* Maximum version (size) of QR-code symbol.
|
||||
*/
|
||||
#define QRSPEC_VERSION_MAX 40
|
||||
|
||||
/**
|
||||
* Maximum version (size) of QR-code symbol.
|
||||
*/
|
||||
#define MQRSPEC_VERSION_MAX 4
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Input data (qrinput.c)
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Singly linked list to contain input strings. An instance of this class
|
||||
* contains its version and error correction level too. It is required to
|
||||
* set them by QRinput_setVersion() and QRinput_setErrorCorrectionLevel(),
|
||||
* or use QRinput_new2() to instantiate an object.
|
||||
*/
|
||||
typedef struct _QRinput QRinput;
|
||||
|
||||
/**
|
||||
* Instantiate an input data object. The version is set to 0 (auto-select)
|
||||
* and the error correction level is set to QR_ECLEVEL_L.
|
||||
* @return an input object (initialized). On error, NULL is returned and errno
|
||||
* is set to indicate the error.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
*/
|
||||
extern QRinput *QRinput_new(void);
|
||||
|
||||
/**
|
||||
* Instantiate an input data object.
|
||||
* @param version version number.
|
||||
* @param level Error correction level.
|
||||
* @return an input object (initialized). On error, NULL is returned and errno
|
||||
* is set to indicate the error.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
* @throw EINVAL invalid arguments.
|
||||
*/
|
||||
extern QRinput *QRinput_new2(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Instantiate an input data object. Object's Micro QR Code flag is set.
|
||||
* Unlike with full-sized QR Code, version number must be specified (>0).
|
||||
* @param version version number (1--4).
|
||||
* @param level Error correction level.
|
||||
* @return an input object (initialized). On error, NULL is returned and errno
|
||||
* is set to indicate the error.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
* @throw EINVAL invalid arguments.
|
||||
*/
|
||||
extern QRinput *QRinput_newMQR(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Append data to an input object.
|
||||
* The data is copied and appended to the input object.
|
||||
* @param input input object.
|
||||
* @param mode encoding mode.
|
||||
* @param size size of data (byte).
|
||||
* @param data a pointer to the memory area of the input data.
|
||||
* @retval 0 success.
|
||||
* @retval -1 an error occurred and errno is set to indeicate the error.
|
||||
* See Execptions for the details.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
* @throw EINVAL input data is invalid.
|
||||
*
|
||||
*/
|
||||
extern int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data);
|
||||
|
||||
/**
|
||||
* Append ECI header.
|
||||
* @param input input object.
|
||||
* @param ecinum ECI indicator number (0 - 999999)
|
||||
* @retval 0 success.
|
||||
* @retval -1 an error occurred and errno is set to indeicate the error.
|
||||
* See Execptions for the details.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
* @throw EINVAL input data is invalid.
|
||||
*
|
||||
*/
|
||||
extern int QRinput_appendECIheader(QRinput *input, unsigned int ecinum);
|
||||
|
||||
/**
|
||||
* Get current version.
|
||||
* @param input input object.
|
||||
* @return current version.
|
||||
*/
|
||||
extern int QRinput_getVersion(QRinput *input);
|
||||
|
||||
/**
|
||||
* Set version of the QR code that is to be encoded.
|
||||
* This function cannot be applied to Micro QR Code.
|
||||
* @param input input object.
|
||||
* @param version version number (0 = auto)
|
||||
* @retval 0 success.
|
||||
* @retval -1 invalid argument.
|
||||
*/
|
||||
extern int QRinput_setVersion(QRinput *input, int version);
|
||||
|
||||
/**
|
||||
* Get current error correction level.
|
||||
* @param input input object.
|
||||
* @return Current error correcntion level.
|
||||
*/
|
||||
extern QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input);
|
||||
|
||||
/**
|
||||
* Set error correction level of the QR code that is to be encoded.
|
||||
* This function cannot be applied to Micro QR Code.
|
||||
* @param input input object.
|
||||
* @param level Error correction level.
|
||||
* @retval 0 success.
|
||||
* @retval -1 invalid argument.
|
||||
*/
|
||||
extern int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Set version and error correction level of the QR code at once.
|
||||
* This function is recommened for Micro QR Code.
|
||||
* @param input input object.
|
||||
* @param version version number (0 = auto)
|
||||
* @param level Error correction level.
|
||||
* @retval 0 success.
|
||||
* @retval -1 invalid argument.
|
||||
*/
|
||||
extern int QRinput_setVersionAndErrorCorrectionLevel(QRinput *input, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Free the input object.
|
||||
* All of data chunks in the input object are freed too.
|
||||
* @param input input object.
|
||||
*/
|
||||
extern void QRinput_free(QRinput *input);
|
||||
|
||||
/**
|
||||
* Validate the input data.
|
||||
* @param mode encoding mode.
|
||||
* @param size size of data (byte).
|
||||
* @param data a pointer to the memory area of the input data.
|
||||
* @retval 0 success.
|
||||
* @retval -1 invalid arguments.
|
||||
*/
|
||||
extern int QRinput_check(QRencodeMode mode, int size, const unsigned char *data);
|
||||
|
||||
/**
|
||||
* Set of QRinput for structured symbols.
|
||||
*/
|
||||
typedef struct _QRinput_Struct QRinput_Struct;
|
||||
|
||||
/**
|
||||
* Instantiate a set of input data object.
|
||||
* @return an instance of QRinput_Struct. On error, NULL is returned and errno
|
||||
* is set to indicate the error.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
*/
|
||||
extern QRinput_Struct *QRinput_Struct_new(void);
|
||||
|
||||
/**
|
||||
* Set parity of structured symbols.
|
||||
* @param s structured input object.
|
||||
* @param parity parity of s.
|
||||
*/
|
||||
extern void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity);
|
||||
|
||||
/**
|
||||
* Append a QRinput object to the set. QRinput created by QRinput_newMQR()
|
||||
* will be rejected.
|
||||
* @warning never append the same QRinput object twice or more.
|
||||
* @param s structured input object.
|
||||
* @param input an input object.
|
||||
* @retval >0 number of input objects in the structure.
|
||||
* @retval -1 an error occurred. See Exceptions for the details.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
* @throw EINVAL invalid arguments.
|
||||
*/
|
||||
extern int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input);
|
||||
|
||||
/**
|
||||
* Free all of QRinput in the set.
|
||||
* @param s a structured input object.
|
||||
*/
|
||||
extern void QRinput_Struct_free(QRinput_Struct *s);
|
||||
|
||||
/**
|
||||
* Split a QRinput to QRinput_Struct. It calculates a parity, set it, then
|
||||
* insert structured-append headers. QRinput created by QRinput_newMQR() will
|
||||
* be rejected.
|
||||
* @param input input object. Version number and error correction level must be
|
||||
* set.
|
||||
* @return a set of input data. On error, NULL is returned, and errno is set
|
||||
* to indicate the error. See Exceptions for the details.
|
||||
* @throw ERANGE input data is too large.
|
||||
* @throw EINVAL invalid input data.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
*/
|
||||
extern QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input);
|
||||
|
||||
/**
|
||||
* Insert structured-append headers to the input structure. It calculates
|
||||
* a parity and set it if the parity is not set yet.
|
||||
* @param s input structure
|
||||
* @retval 0 success.
|
||||
* @retval -1 an error occurred and errno is set to indeicate the error.
|
||||
* See Execptions for the details.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory.
|
||||
*/
|
||||
extern int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s);
|
||||
|
||||
/**
|
||||
* Set FNC1-1st position flag.
|
||||
*/
|
||||
extern int QRinput_setFNC1First(QRinput *input);
|
||||
|
||||
/**
|
||||
* Set FNC1-2nd position flag and application identifier.
|
||||
*/
|
||||
extern int QRinput_setFNC1Second(QRinput *input, unsigned char appid);
|
||||
|
||||
/******************************************************************************
|
||||
* QRcode output (qrencode.c)
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* QRcode class.
|
||||
* Symbol data is represented as an array contains width*width uchars.
|
||||
* Each uchar represents a module (dot). If the less significant bit of
|
||||
* the uchar is 1, the corresponding module is black. The other bits are
|
||||
* meaningless for usual applications, but here its specification is described.
|
||||
*
|
||||
* <pre>
|
||||
* MSB 76543210 LSB
|
||||
* |||||||`- 1=black/0=white
|
||||
* ||||||`-- data and ecc code area
|
||||
* |||||`--- format information
|
||||
* ||||`---- version information
|
||||
* |||`----- timing pattern
|
||||
* ||`------ alignment pattern
|
||||
* |`------- finder pattern and separator
|
||||
* `-------- non-data modules (format, timing, etc.)
|
||||
* </pre>
|
||||
*/
|
||||
typedef struct {
|
||||
int version; ///< version of the symbol
|
||||
int width; ///< width of the symbol
|
||||
unsigned char *data; ///< symbol data
|
||||
} QRcode;
|
||||
|
||||
/**
|
||||
* Singly-linked list of QRcode. Used to represent a structured symbols.
|
||||
* A list is terminated with NULL.
|
||||
*/
|
||||
typedef struct _QRcode_List {
|
||||
QRcode *code;
|
||||
struct _QRcode_List *next;
|
||||
} QRcode_List;
|
||||
|
||||
/**
|
||||
* Create a symbol from the input data.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
* @param input input data.
|
||||
* @return an instance of QRcode class. The version of the result QRcode may
|
||||
* be larger than the designated version. On error, NULL is returned,
|
||||
* and errno is set to indicate the error. See Exceptions for the
|
||||
* details.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeInput(QRinput *input);
|
||||
|
||||
/**
|
||||
* Create a symbol from the string. The library automatically parses the input
|
||||
* string and encodes in a QR Code symbol.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
* @param string input string. It must be NUL terminated.
|
||||
* @param version version of the symbol. If 0, the library chooses the minimum
|
||||
* version for the given input data.
|
||||
* @param level error correction level.
|
||||
* @param hint tell the library how Japanese Kanji characters should be
|
||||
* encoded. If QR_MODE_KANJI is given, the library assumes that the
|
||||
* given string contains Shift-JIS characters and encodes them in
|
||||
* Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical
|
||||
* characters will be encoded as is. If you want to embed UTF-8
|
||||
* string, choose this. Other mode will cause EINVAL error.
|
||||
* @param casesensitive case-sensitive(1) or not(0).
|
||||
* @return an instance of QRcode class. The version of the result QRcode may
|
||||
* be larger than the designated version. On error, NULL is returned,
|
||||
* and errno is set to indicate the error. See Exceptions for the
|
||||
* details.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
* @throw ERANGE input data is too large.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
|
||||
|
||||
/**
|
||||
* Same to QRcode_encodeString(), but encode whole data in 8-bit mode.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Micro QR Code version of QRcode_encodeString().
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
|
||||
|
||||
/**
|
||||
* Micro QR Code version of QRcode_encodeString8bit().
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Encode byte stream (may include '\0') in 8-bit mode.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
* @param size size of the input data.
|
||||
* @param data input data.
|
||||
* @param version version of the symbol. If 0, the library chooses the minimum
|
||||
* version for the given input data.
|
||||
* @param level error correction level.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
* @throw ERANGE input data is too large.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Micro QR Code version of QRcode_encodeData().
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
*/
|
||||
extern QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Free the instance of QRcode class.
|
||||
* @param qrcode an instance of QRcode class.
|
||||
*/
|
||||
extern void QRcode_free(QRcode *qrcode);
|
||||
|
||||
/**
|
||||
* Create structured symbols from the input data.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
* @param s
|
||||
* @return a singly-linked list of QRcode.
|
||||
*/
|
||||
extern QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s);
|
||||
|
||||
/**
|
||||
* Create structured symbols from the string. The library automatically parses
|
||||
* the input string and encodes in a QR Code symbol.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
* @param string input string. It must be NUL terminated.
|
||||
* @param version version of the symbol.
|
||||
* @param level error correction level.
|
||||
* @param hint tell the library how Japanese Kanji characters should be
|
||||
* encoded. If QR_MODE_KANJI is given, the library assumes that the
|
||||
* given string contains Shift-JIS characters and encodes them in
|
||||
* Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical
|
||||
* characters will be encoded as is. If you want to embed UTF-8
|
||||
* string, choose this. Other mode will cause EINVAL error.
|
||||
* @param casesensitive case-sensitive(1) or not(0).
|
||||
* @return a singly-linked list of QRcode. On error, NULL is returned, and
|
||||
* errno is set to indicate the error. See Exceptions for the details.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
*/
|
||||
extern QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
|
||||
|
||||
/**
|
||||
* Same to QRcode_encodeStringStructured(), but encode whole data in 8-bit mode.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
*/
|
||||
extern QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Create structured symbols from byte stream (may include '\0'). Wholde data
|
||||
* are encoded in 8-bit mode.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
* @param size size of the input data.
|
||||
* @param data input dat.
|
||||
* @param version version of the symbol.
|
||||
* @param level error correction level.
|
||||
* @return a singly-linked list of QRcode. On error, NULL is returned, and
|
||||
* errno is set to indicate the error. See Exceptions for the details.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
*/
|
||||
extern QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return the number of symbols included in a QRcode_List.
|
||||
* @param qrlist a head entry of a QRcode_List.
|
||||
* @return number of symbols in the list.
|
||||
*/
|
||||
extern int QRcode_List_size(QRcode_List *qrlist);
|
||||
|
||||
/**
|
||||
* Free the QRcode_List.
|
||||
* @param qrlist a head entry of a QRcode_List.
|
||||
*/
|
||||
extern void QRcode_List_free(QRcode_List *qrlist);
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* System utilities
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return a string that identifies the library version.
|
||||
* @param major_version
|
||||
* @param minor_version
|
||||
* @param micro_version
|
||||
*/
|
||||
extern void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version);
|
||||
|
||||
/**
|
||||
* Return a string that identifies the library version.
|
||||
* @return a string identifies the library version. The string is held by the
|
||||
* library. Do NOT free it.
|
||||
*/
|
||||
extern char *QRcode_APIVersionString(void);
|
||||
|
||||
/**
|
||||
* Clear all caches. This is only for debug purpose. If you are attacking a
|
||||
* complicated memory leak bug, try this to reduce the reachable blocks record.
|
||||
* @warning This function is THREAD UNSAFE when pthread is disabled.
|
||||
*/
|
||||
extern void QRcode_clearCache(void);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __QRENCODE_H__ */
|
88
src/qqrencode/qrencode/qrencode_inner.h
Normal file
88
src/qqrencode/qrencode/qrencode_inner.h
Normal file
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Header for test use
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __QRENCODE_INNER_H__
|
||||
#define __QRENCODE_INNER_H__
|
||||
|
||||
/**
|
||||
* This header file includes definitions for test use.
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
* Raw code
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int dataLength;
|
||||
unsigned char *data;
|
||||
int eccLength;
|
||||
unsigned char *ecc;
|
||||
} RSblock;
|
||||
|
||||
typedef struct {
|
||||
int version;
|
||||
int dataLength;
|
||||
int eccLength;
|
||||
unsigned char *datacode;
|
||||
unsigned char *ecccode;
|
||||
int b1;
|
||||
int blocks;
|
||||
RSblock *rsblock;
|
||||
int count;
|
||||
} QRRawCode;
|
||||
|
||||
extern QRRawCode *QRraw_new(QRinput *input);
|
||||
extern unsigned char QRraw_getCode(QRRawCode *raw);
|
||||
extern void QRraw_free(QRRawCode *raw);
|
||||
|
||||
/******************************************************************************
|
||||
* Raw code for Micro QR Code
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int version;
|
||||
int dataLength;
|
||||
int eccLength;
|
||||
unsigned char *datacode;
|
||||
unsigned char *ecccode;
|
||||
RSblock *rsblock;
|
||||
int oddbits;
|
||||
int count;
|
||||
} MQRRawCode;
|
||||
|
||||
extern MQRRawCode *MQRraw_new(QRinput *input);
|
||||
extern unsigned char MQRraw_getCode(MQRRawCode *raw);
|
||||
extern void MQRraw_free(MQRRawCode *raw);
|
||||
|
||||
/******************************************************************************
|
||||
* Frame filling
|
||||
*****************************************************************************/
|
||||
extern unsigned char *FrameFiller_test(int version);
|
||||
extern unsigned char *FrameFiller_testMQR(int version);
|
||||
|
||||
/******************************************************************************
|
||||
* QR-code encoding
|
||||
*****************************************************************************/
|
||||
extern QRcode *QRcode_encodeMask(QRinput *input, int mask);
|
||||
extern QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask);
|
||||
extern QRcode *QRcode_new(int version, int width, unsigned char *data);
|
||||
|
||||
#endif /* __QRENCODE_INNER_H__ */
|
1729
src/qqrencode/qrencode/qrinput.c
Normal file
1729
src/qqrencode/qrencode/qrinput.c
Normal file
File diff suppressed because it is too large
Load diff
123
src/qqrencode/qrencode/qrinput.h
Normal file
123
src/qqrencode/qrencode/qrinput.h
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Input data chunk class
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __QRINPUT_H__
|
||||
#define __QRINPUT_H__
|
||||
|
||||
#include "qrencode.h"
|
||||
#include "bitstream.h"
|
||||
|
||||
int QRinput_isSplittableMode(QRencodeMode mode);
|
||||
|
||||
/******************************************************************************
|
||||
* Entry of input data
|
||||
*****************************************************************************/
|
||||
typedef struct _QRinput_List QRinput_List;
|
||||
|
||||
struct _QRinput_List {
|
||||
QRencodeMode mode;
|
||||
int size; ///< Size of data chunk (byte).
|
||||
unsigned char *data; ///< Data chunk.
|
||||
BitStream *bstream;
|
||||
QRinput_List *next;
|
||||
};
|
||||
|
||||
/******************************************************************************
|
||||
* Input Data
|
||||
*****************************************************************************/
|
||||
struct _QRinput {
|
||||
int version;
|
||||
QRecLevel level;
|
||||
QRinput_List *head;
|
||||
QRinput_List *tail;
|
||||
int mqr;
|
||||
int fnc1;
|
||||
unsigned char appid;
|
||||
};
|
||||
|
||||
/******************************************************************************
|
||||
* Structured append input data
|
||||
*****************************************************************************/
|
||||
typedef struct _QRinput_InputList QRinput_InputList;
|
||||
|
||||
struct _QRinput_InputList {
|
||||
QRinput *input;
|
||||
QRinput_InputList *next;
|
||||
};
|
||||
|
||||
struct _QRinput_Struct {
|
||||
int size; ///< number of structured symbols
|
||||
int parity;
|
||||
QRinput_InputList *head;
|
||||
QRinput_InputList *tail;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pack all bit streams padding bits into a byte array.
|
||||
* @param input input data.
|
||||
* @return padded merged byte stream
|
||||
*/
|
||||
extern unsigned char *QRinput_getByteStream(QRinput *input);
|
||||
|
||||
|
||||
extern int QRinput_estimateBitsModeNum(int size);
|
||||
extern int QRinput_estimateBitsModeAn(int size);
|
||||
extern int QRinput_estimateBitsMode8(int size);
|
||||
extern int QRinput_estimateBitsModeKanji(int size);
|
||||
|
||||
extern QRinput *QRinput_dup(QRinput *input);
|
||||
|
||||
extern const signed char QRinput_anTable[128];
|
||||
|
||||
/**
|
||||
* Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
|
||||
* @param __c__ character
|
||||
* @return value
|
||||
*/
|
||||
#define QRinput_lookAnTable(__c__) \
|
||||
((__c__ & 0x80)?-1:QRinput_anTable[(int)__c__])
|
||||
|
||||
/**
|
||||
* Length of a standard mode indicator in bits.
|
||||
*/
|
||||
|
||||
#define MODE_INDICATOR_SIZE 4
|
||||
|
||||
/**
|
||||
* Length of a segment of structured-append header.
|
||||
*/
|
||||
#define STRUCTURE_HEADER_SIZE 20
|
||||
|
||||
/**
|
||||
* Maximum number of symbols in a set of structured-appended symbols.
|
||||
*/
|
||||
#define MAX_STRUCTURED_SYMBOLS 16
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
extern BitStream *QRinput_mergeBitStream(QRinput *input);
|
||||
extern BitStream *QRinput_getBitStream(QRinput *input);
|
||||
extern int QRinput_estimateBitStreamSize(QRinput *input, int version);
|
||||
extern int QRinput_splitEntry(QRinput_List *entry, int bytes);
|
||||
extern int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits);
|
||||
extern int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int index, unsigned char parity);
|
||||
#endif
|
||||
|
||||
#endif /* __QRINPUT_H__ */
|
562
src/qqrencode/qrencode/qrspec.c
Normal file
562
src/qqrencode/qrencode/qrspec.c
Normal file
|
@ -0,0 +1,562 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* QR Code specification in convenient format.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* The following data / specifications are taken from
|
||||
* "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
|
||||
* or
|
||||
* "Automatic identification and data capture techniques --
|
||||
* QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "qrspec.h"
|
||||
#include "qrinput.h"
|
||||
|
||||
/******************************************************************************
|
||||
* Version and capacity
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int width; //< Edge length of the symbol
|
||||
int words; //< Data capacity (bytes)
|
||||
int remainder; //< Remainder bit (bits)
|
||||
int ec[4]; //< Number of ECC code (bytes)
|
||||
} QRspec_Capacity;
|
||||
|
||||
/**
|
||||
* Table of the capacity of symbols
|
||||
* See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
|
||||
*/
|
||||
static const QRspec_Capacity qrspecCapacity[QRSPEC_VERSION_MAX + 1] = {
|
||||
{ 0, 0, 0, { 0, 0, 0, 0}},
|
||||
{ 21, 26, 0, { 7, 10, 13, 17}}, // 1
|
||||
{ 25, 44, 7, { 10, 16, 22, 28}},
|
||||
{ 29, 70, 7, { 15, 26, 36, 44}},
|
||||
{ 33, 100, 7, { 20, 36, 52, 64}},
|
||||
{ 37, 134, 7, { 26, 48, 72, 88}}, // 5
|
||||
{ 41, 172, 7, { 36, 64, 96, 112}},
|
||||
{ 45, 196, 0, { 40, 72, 108, 130}},
|
||||
{ 49, 242, 0, { 48, 88, 132, 156}},
|
||||
{ 53, 292, 0, { 60, 110, 160, 192}},
|
||||
{ 57, 346, 0, { 72, 130, 192, 224}}, //10
|
||||
{ 61, 404, 0, { 80, 150, 224, 264}},
|
||||
{ 65, 466, 0, { 96, 176, 260, 308}},
|
||||
{ 69, 532, 0, { 104, 198, 288, 352}},
|
||||
{ 73, 581, 3, { 120, 216, 320, 384}},
|
||||
{ 77, 655, 3, { 132, 240, 360, 432}}, //15
|
||||
{ 81, 733, 3, { 144, 280, 408, 480}},
|
||||
{ 85, 815, 3, { 168, 308, 448, 532}},
|
||||
{ 89, 901, 3, { 180, 338, 504, 588}},
|
||||
{ 93, 991, 3, { 196, 364, 546, 650}},
|
||||
{ 97, 1085, 3, { 224, 416, 600, 700}}, //20
|
||||
{101, 1156, 4, { 224, 442, 644, 750}},
|
||||
{105, 1258, 4, { 252, 476, 690, 816}},
|
||||
{109, 1364, 4, { 270, 504, 750, 900}},
|
||||
{113, 1474, 4, { 300, 560, 810, 960}},
|
||||
{117, 1588, 4, { 312, 588, 870, 1050}}, //25
|
||||
{121, 1706, 4, { 336, 644, 952, 1110}},
|
||||
{125, 1828, 4, { 360, 700, 1020, 1200}},
|
||||
{129, 1921, 3, { 390, 728, 1050, 1260}},
|
||||
{133, 2051, 3, { 420, 784, 1140, 1350}},
|
||||
{137, 2185, 3, { 450, 812, 1200, 1440}}, //30
|
||||
{141, 2323, 3, { 480, 868, 1290, 1530}},
|
||||
{145, 2465, 3, { 510, 924, 1350, 1620}},
|
||||
{149, 2611, 3, { 540, 980, 1440, 1710}},
|
||||
{153, 2761, 3, { 570, 1036, 1530, 1800}},
|
||||
{157, 2876, 0, { 570, 1064, 1590, 1890}}, //35
|
||||
{161, 3034, 0, { 600, 1120, 1680, 1980}},
|
||||
{165, 3196, 0, { 630, 1204, 1770, 2100}},
|
||||
{169, 3362, 0, { 660, 1260, 1860, 2220}},
|
||||
{173, 3532, 0, { 720, 1316, 1950, 2310}},
|
||||
{177, 3706, 0, { 750, 1372, 2040, 2430}} //40
|
||||
};
|
||||
|
||||
int QRspec_getDataLength(int version, QRecLevel level)
|
||||
{
|
||||
return qrspecCapacity[version].words - qrspecCapacity[version].ec[level];
|
||||
}
|
||||
|
||||
int QRspec_getECCLength(int version, QRecLevel level)
|
||||
{
|
||||
return qrspecCapacity[version].ec[level];
|
||||
}
|
||||
|
||||
int QRspec_getMinimumVersion(int size, QRecLevel level)
|
||||
{
|
||||
int i;
|
||||
int words;
|
||||
|
||||
for(i=1; i<= QRSPEC_VERSION_MAX; i++) {
|
||||
words = qrspecCapacity[i].words - qrspecCapacity[i].ec[level];
|
||||
if(words >= size) return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int QRspec_getWidth(int version)
|
||||
{
|
||||
return qrspecCapacity[version].width;
|
||||
}
|
||||
|
||||
int QRspec_getRemainder(int version)
|
||||
{
|
||||
return qrspecCapacity[version].remainder;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Length indicator
|
||||
*****************************************************************************/
|
||||
|
||||
static const int lengthTableBits[4][3] = {
|
||||
{10, 12, 14},
|
||||
{ 9, 11, 13},
|
||||
{ 8, 16, 16},
|
||||
{ 8, 10, 12}
|
||||
};
|
||||
|
||||
int QRspec_lengthIndicator(QRencodeMode mode, int version)
|
||||
{
|
||||
int l;
|
||||
|
||||
if(!QRinput_isSplittableMode(mode)) return 0;
|
||||
if(version <= 9) {
|
||||
l = 0;
|
||||
} else if(version <= 26) {
|
||||
l = 1;
|
||||
} else {
|
||||
l = 2;
|
||||
}
|
||||
|
||||
return lengthTableBits[mode][l];
|
||||
}
|
||||
|
||||
int QRspec_maximumWords(QRencodeMode mode, int version)
|
||||
{
|
||||
int l;
|
||||
int bits;
|
||||
int words;
|
||||
|
||||
if(!QRinput_isSplittableMode(mode)) return 0;
|
||||
if(version <= 9) {
|
||||
l = 0;
|
||||
} else if(version <= 26) {
|
||||
l = 1;
|
||||
} else {
|
||||
l = 2;
|
||||
}
|
||||
|
||||
bits = lengthTableBits[mode][l];
|
||||
words = (1 << bits) - 1;
|
||||
if(mode == QR_MODE_KANJI) {
|
||||
words *= 2; // the number of bytes is required
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Error correction code
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Table of the error correction code (Reed-Solomon block)
|
||||
* See Table 12-16 (pp.30-36), JIS X0510:2004.
|
||||
*/
|
||||
static const int eccTable[QRSPEC_VERSION_MAX+1][4][2] = {
|
||||
{{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0}},
|
||||
{{ 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}}, // 1
|
||||
{{ 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}},
|
||||
{{ 1, 0}, { 1, 0}, { 2, 0}, { 2, 0}},
|
||||
{{ 1, 0}, { 2, 0}, { 2, 0}, { 4, 0}},
|
||||
{{ 1, 0}, { 2, 0}, { 2, 2}, { 2, 2}}, // 5
|
||||
{{ 2, 0}, { 4, 0}, { 4, 0}, { 4, 0}},
|
||||
{{ 2, 0}, { 4, 0}, { 2, 4}, { 4, 1}},
|
||||
{{ 2, 0}, { 2, 2}, { 4, 2}, { 4, 2}},
|
||||
{{ 2, 0}, { 3, 2}, { 4, 4}, { 4, 4}},
|
||||
{{ 2, 2}, { 4, 1}, { 6, 2}, { 6, 2}}, //10
|
||||
{{ 4, 0}, { 1, 4}, { 4, 4}, { 3, 8}},
|
||||
{{ 2, 2}, { 6, 2}, { 4, 6}, { 7, 4}},
|
||||
{{ 4, 0}, { 8, 1}, { 8, 4}, {12, 4}},
|
||||
{{ 3, 1}, { 4, 5}, {11, 5}, {11, 5}},
|
||||
{{ 5, 1}, { 5, 5}, { 5, 7}, {11, 7}}, //15
|
||||
{{ 5, 1}, { 7, 3}, {15, 2}, { 3, 13}},
|
||||
{{ 1, 5}, {10, 1}, { 1, 15}, { 2, 17}},
|
||||
{{ 5, 1}, { 9, 4}, {17, 1}, { 2, 19}},
|
||||
{{ 3, 4}, { 3, 11}, {17, 4}, { 9, 16}},
|
||||
{{ 3, 5}, { 3, 13}, {15, 5}, {15, 10}}, //20
|
||||
{{ 4, 4}, {17, 0}, {17, 6}, {19, 6}},
|
||||
{{ 2, 7}, {17, 0}, { 7, 16}, {34, 0}},
|
||||
{{ 4, 5}, { 4, 14}, {11, 14}, {16, 14}},
|
||||
{{ 6, 4}, { 6, 14}, {11, 16}, {30, 2}},
|
||||
{{ 8, 4}, { 8, 13}, { 7, 22}, {22, 13}}, //25
|
||||
{{10, 2}, {19, 4}, {28, 6}, {33, 4}},
|
||||
{{ 8, 4}, {22, 3}, { 8, 26}, {12, 28}},
|
||||
{{ 3, 10}, { 3, 23}, { 4, 31}, {11, 31}},
|
||||
{{ 7, 7}, {21, 7}, { 1, 37}, {19, 26}},
|
||||
{{ 5, 10}, {19, 10}, {15, 25}, {23, 25}}, //30
|
||||
{{13, 3}, { 2, 29}, {42, 1}, {23, 28}},
|
||||
{{17, 0}, {10, 23}, {10, 35}, {19, 35}},
|
||||
{{17, 1}, {14, 21}, {29, 19}, {11, 46}},
|
||||
{{13, 6}, {14, 23}, {44, 7}, {59, 1}},
|
||||
{{12, 7}, {12, 26}, {39, 14}, {22, 41}}, //35
|
||||
{{ 6, 14}, { 6, 34}, {46, 10}, { 2, 64}},
|
||||
{{17, 4}, {29, 14}, {49, 10}, {24, 46}},
|
||||
{{ 4, 18}, {13, 32}, {48, 14}, {42, 32}},
|
||||
{{20, 4}, {40, 7}, {43, 22}, {10, 67}},
|
||||
{{19, 6}, {18, 31}, {34, 34}, {20, 61}},//40
|
||||
};
|
||||
|
||||
void QRspec_getEccSpec(int version, QRecLevel level, int spec[5])
|
||||
{
|
||||
int b1, b2;
|
||||
int data, ecc;
|
||||
|
||||
b1 = eccTable[version][level][0];
|
||||
b2 = eccTable[version][level][1];
|
||||
data = QRspec_getDataLength(version, level);
|
||||
ecc = QRspec_getECCLength(version, level);
|
||||
|
||||
if(b2 == 0) {
|
||||
spec[0] = b1;
|
||||
spec[1] = data / b1;
|
||||
spec[2] = ecc / b1;
|
||||
spec[3] = spec[4] = 0;
|
||||
} else {
|
||||
spec[0] = b1;
|
||||
spec[1] = data / (b1 + b2);
|
||||
spec[2] = ecc / (b1 + b2);
|
||||
spec[3] = b2;
|
||||
spec[4] = spec[1] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Alignment pattern
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Positions of alignment patterns.
|
||||
* This array includes only the second and the third position of the alignment
|
||||
* patterns. Rest of them can be calculated from the distance between them.
|
||||
*
|
||||
* See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
|
||||
*/
|
||||
static const int alignmentPattern[QRSPEC_VERSION_MAX+1][2] = {
|
||||
{ 0, 0},
|
||||
{ 0, 0}, {18, 0}, {22, 0}, {26, 0}, {30, 0}, // 1- 5
|
||||
{34, 0}, {22, 38}, {24, 42}, {26, 46}, {28, 50}, // 6-10
|
||||
{30, 54}, {32, 58}, {34, 62}, {26, 46}, {26, 48}, //11-15
|
||||
{26, 50}, {30, 54}, {30, 56}, {30, 58}, {34, 62}, //16-20
|
||||
{28, 50}, {26, 50}, {30, 54}, {28, 54}, {32, 58}, //21-25
|
||||
{30, 58}, {34, 62}, {26, 50}, {30, 54}, {26, 52}, //26-30
|
||||
{30, 56}, {34, 60}, {30, 58}, {34, 62}, {30, 54}, //31-35
|
||||
{24, 50}, {28, 54}, {32, 58}, {26, 54}, {30, 58}, //35-40
|
||||
};
|
||||
|
||||
/**
|
||||
* Put an alignment marker.
|
||||
* @param frame
|
||||
* @param width
|
||||
* @param ox,oy center coordinate of the pattern
|
||||
*/
|
||||
static void QRspec_putAlignmentMarker(unsigned char *frame, int width, int ox, int oy)
|
||||
{
|
||||
static const unsigned char finder[] = {
|
||||
0xa1, 0xa1, 0xa1, 0xa1, 0xa1,
|
||||
0xa1, 0xa0, 0xa0, 0xa0, 0xa1,
|
||||
0xa1, 0xa0, 0xa1, 0xa0, 0xa1,
|
||||
0xa1, 0xa0, 0xa0, 0xa0, 0xa1,
|
||||
0xa1, 0xa1, 0xa1, 0xa1, 0xa1,
|
||||
};
|
||||
int x, y;
|
||||
const unsigned char *s;
|
||||
|
||||
frame += (oy - 2) * width + ox - 2;
|
||||
s = finder;
|
||||
for(y=0; y<5; y++) {
|
||||
for(x=0; x<5; x++) {
|
||||
frame[x] = s[x];
|
||||
}
|
||||
frame += width;
|
||||
s += 5;
|
||||
}
|
||||
}
|
||||
|
||||
static void QRspec_putAlignmentPattern(int version, unsigned char *frame, int width)
|
||||
{
|
||||
int d, w, x, y, cx, cy;
|
||||
|
||||
if(version < 2) return;
|
||||
|
||||
d = alignmentPattern[version][1] - alignmentPattern[version][0];
|
||||
if(d < 0) {
|
||||
w = 2;
|
||||
} else {
|
||||
w = (width - alignmentPattern[version][0]) / d + 2;
|
||||
}
|
||||
|
||||
if(w * w - 3 == 1) {
|
||||
x = alignmentPattern[version][0];
|
||||
y = alignmentPattern[version][0];
|
||||
QRspec_putAlignmentMarker(frame, width, x, y);
|
||||
return;
|
||||
}
|
||||
|
||||
cx = alignmentPattern[version][0];
|
||||
for(x=1; x<w - 1; x++) {
|
||||
QRspec_putAlignmentMarker(frame, width, 6, cx);
|
||||
QRspec_putAlignmentMarker(frame, width, cx, 6);
|
||||
cx += d;
|
||||
}
|
||||
|
||||
cy = alignmentPattern[version][0];
|
||||
for(y=0; y<w-1; y++) {
|
||||
cx = alignmentPattern[version][0];
|
||||
for(x=0; x<w-1; x++) {
|
||||
QRspec_putAlignmentMarker(frame, width, cx, cy);
|
||||
cx += d;
|
||||
}
|
||||
cy += d;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Version information pattern
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Version information pattern (BCH coded).
|
||||
* See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
|
||||
*/
|
||||
static const unsigned int versionPattern[QRSPEC_VERSION_MAX - 6] = {
|
||||
0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
|
||||
0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
|
||||
0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
|
||||
0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
|
||||
0x27541, 0x28c69
|
||||
};
|
||||
|
||||
unsigned int QRspec_getVersionPattern(int version)
|
||||
{
|
||||
if(version < 7 || version > QRSPEC_VERSION_MAX) return 0;
|
||||
|
||||
return versionPattern[version - 7];
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Format information
|
||||
*****************************************************************************/
|
||||
|
||||
/* See calcFormatInfo in tests/test_qrspec.c */
|
||||
static const unsigned int formatInfo[4][8] = {
|
||||
{0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976},
|
||||
{0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0},
|
||||
{0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed},
|
||||
{0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b}
|
||||
};
|
||||
|
||||
unsigned int QRspec_getFormatInfo(int mask, QRecLevel level)
|
||||
{
|
||||
if(mask < 0 || mask > 7) return 0;
|
||||
|
||||
return formatInfo[level][mask];
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Frame
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Cache of initial frames.
|
||||
*/
|
||||
/* C99 says that static storage shall be initialized to a null pointer
|
||||
* by compiler. */
|
||||
static unsigned char *frames[QRSPEC_VERSION_MAX + 1];
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Put a finder pattern.
|
||||
* @param frame
|
||||
* @param width
|
||||
* @param ox,oy upper-left coordinate of the pattern
|
||||
*/
|
||||
static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
|
||||
{
|
||||
static const unsigned char finder[] = {
|
||||
0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
|
||||
0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
|
||||
0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
|
||||
0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
|
||||
};
|
||||
int x, y;
|
||||
const unsigned char *s;
|
||||
|
||||
frame += oy * width + ox;
|
||||
s = finder;
|
||||
for(y=0; y<7; y++) {
|
||||
for(x=0; x<7; x++) {
|
||||
frame[x] = s[x];
|
||||
}
|
||||
frame += width;
|
||||
s += 7;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static unsigned char *QRspec_createFrame(int version)
|
||||
{
|
||||
unsigned char *frame, *p, *q;
|
||||
int width;
|
||||
int x, y;
|
||||
unsigned int verinfo, v;
|
||||
|
||||
width = qrspecCapacity[version].width;
|
||||
frame = (unsigned char *)malloc(width * width);
|
||||
if(frame == NULL) return NULL;
|
||||
|
||||
memset(frame, 0, width * width);
|
||||
/* Finder pattern */
|
||||
putFinderPattern(frame, width, 0, 0);
|
||||
putFinderPattern(frame, width, width - 7, 0);
|
||||
putFinderPattern(frame, width, 0, width - 7);
|
||||
/* Separator */
|
||||
p = frame;
|
||||
q = frame + width * (width - 7);
|
||||
for(y=0; y<7; y++) {
|
||||
p[7] = 0xc0;
|
||||
p[width - 8] = 0xc0;
|
||||
q[7] = 0xc0;
|
||||
p += width;
|
||||
q += width;
|
||||
}
|
||||
memset(frame + width * 7, 0xc0, 8);
|
||||
memset(frame + width * 8 - 8, 0xc0, 8);
|
||||
memset(frame + width * (width - 8), 0xc0, 8);
|
||||
/* Mask format information area */
|
||||
memset(frame + width * 8, 0x84, 9);
|
||||
memset(frame + width * 9 - 8, 0x84, 8);
|
||||
p = frame + 8;
|
||||
for(y=0; y<8; y++) {
|
||||
*p = 0x84;
|
||||
p += width;
|
||||
}
|
||||
p = frame + width * (width - 7) + 8;
|
||||
for(y=0; y<7; y++) {
|
||||
*p = 0x84;
|
||||
p += width;
|
||||
}
|
||||
/* Timing pattern */
|
||||
p = frame + width * 6 + 8;
|
||||
q = frame + width * 8 + 6;
|
||||
for(x=1; x<width-15; x++) {
|
||||
*p = 0x90 | (x & 1);
|
||||
*q = 0x90 | (x & 1);
|
||||
p++;
|
||||
q += width;
|
||||
}
|
||||
/* Alignment pattern */
|
||||
QRspec_putAlignmentPattern(version, frame, width);
|
||||
|
||||
/* Version information */
|
||||
if(version >= 7) {
|
||||
verinfo = QRspec_getVersionPattern(version);
|
||||
|
||||
p = frame + width * (width - 11);
|
||||
v = verinfo;
|
||||
for(x=0; x<6; x++) {
|
||||
for(y=0; y<3; y++) {
|
||||
p[width * y + x] = 0x88 | (v & 1);
|
||||
v = v >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
p = frame + width - 11;
|
||||
v = verinfo;
|
||||
for(y=0; y<6; y++) {
|
||||
for(x=0; x<3; x++) {
|
||||
p[x] = 0x88 | (v & 1);
|
||||
v = v >> 1;
|
||||
}
|
||||
p += width;
|
||||
}
|
||||
}
|
||||
/* and a little bit... */
|
||||
frame[width * (width - 8) + 8] = 0x81;
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
unsigned char *QRspec_newFrame(int version)
|
||||
{
|
||||
unsigned char *frame;
|
||||
int width;
|
||||
|
||||
if(version < 1 || version > QRSPEC_VERSION_MAX) return NULL;
|
||||
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_lock(&frames_mutex);
|
||||
#endif
|
||||
if(frames[version] == NULL) {
|
||||
frames[version] = QRspec_createFrame(version);
|
||||
}
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_unlock(&frames_mutex);
|
||||
#endif
|
||||
if(frames[version] == NULL) return NULL;
|
||||
|
||||
width = qrspecCapacity[version].width;
|
||||
frame = (unsigned char *)malloc(width * width);
|
||||
if(frame == NULL) return NULL;
|
||||
memcpy(frame, frames[version], width * width);
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
void QRspec_clearCache(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_lock(&frames_mutex);
|
||||
#endif
|
||||
for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
|
||||
free(frames[i]);
|
||||
frames[i] = NULL;
|
||||
}
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_unlock(&frames_mutex);
|
||||
#endif
|
||||
}
|
181
src/qqrencode/qrencode/qrspec.h
Normal file
181
src/qqrencode/qrencode/qrspec.h
Normal file
|
@ -0,0 +1,181 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* QR Code specification in convenient format.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __QRSPEC_H__
|
||||
#define __QRSPEC_H__
|
||||
|
||||
#include "qrencode.h"
|
||||
|
||||
/******************************************************************************
|
||||
* Version and capacity
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Maximum width of a symbol
|
||||
*/
|
||||
#define QRSPEC_WIDTH_MAX 177
|
||||
|
||||
/**
|
||||
* Return maximum data code length (bytes) for the version.
|
||||
* @param version
|
||||
* @param level
|
||||
* @return maximum size (bytes)
|
||||
*/
|
||||
extern int QRspec_getDataLength(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return maximum error correction code length (bytes) for the version.
|
||||
* @param version
|
||||
* @param level
|
||||
* @return ECC size (bytes)
|
||||
*/
|
||||
extern int QRspec_getECCLength(int version, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return a version number that satisfies the input code length.
|
||||
* @param size input code length (byte)
|
||||
* @param level
|
||||
* @return version number
|
||||
*/
|
||||
extern int QRspec_getMinimumVersion(int size, QRecLevel level);
|
||||
|
||||
/**
|
||||
* Return the width of the symbol for the version.
|
||||
* @param version
|
||||
* @return width
|
||||
*/
|
||||
extern int QRspec_getWidth(int version);
|
||||
|
||||
/**
|
||||
* Return the numer of remainder bits.
|
||||
* @param version
|
||||
* @return number of remainder bits
|
||||
*/
|
||||
extern int QRspec_getRemainder(int version);
|
||||
|
||||
/******************************************************************************
|
||||
* Length indicator
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return the size of lenght indicator for the mode and version.
|
||||
* @param mode
|
||||
* @param version
|
||||
* @return the size of the appropriate length indicator (bits).
|
||||
*/
|
||||
extern int QRspec_lengthIndicator(QRencodeMode mode, int version);
|
||||
|
||||
/**
|
||||
* Return the maximum length for the mode and version.
|
||||
* @param mode
|
||||
* @param version
|
||||
* @return the maximum length (bytes)
|
||||
*/
|
||||
extern int QRspec_maximumWords(QRencodeMode mode, int version);
|
||||
|
||||
/******************************************************************************
|
||||
* Error correction code
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return an array of ECC specification.
|
||||
* @param version
|
||||
* @param level
|
||||
* @param spec an array of ECC specification contains as following:
|
||||
* {# of type1 blocks, # of data code, # of ecc code,
|
||||
* # of type2 blocks, # of data code}
|
||||
*/
|
||||
void QRspec_getEccSpec(int version, QRecLevel level, int spec[5]);
|
||||
|
||||
#define QRspec_rsBlockNum(__spec__) (__spec__[0] + __spec__[3])
|
||||
#define QRspec_rsBlockNum1(__spec__) (__spec__[0])
|
||||
#define QRspec_rsDataCodes1(__spec__) (__spec__[1])
|
||||
#define QRspec_rsEccCodes1(__spec__) (__spec__[2])
|
||||
#define QRspec_rsBlockNum2(__spec__) (__spec__[3])
|
||||
#define QRspec_rsDataCodes2(__spec__) (__spec__[4])
|
||||
#define QRspec_rsEccCodes2(__spec__) (__spec__[2])
|
||||
|
||||
#define QRspec_rsDataLength(__spec__) \
|
||||
((QRspec_rsBlockNum1(__spec__) * QRspec_rsDataCodes1(__spec__)) + \
|
||||
(QRspec_rsBlockNum2(__spec__) * QRspec_rsDataCodes2(__spec__)))
|
||||
#define QRspec_rsEccLength(__spec__) \
|
||||
(QRspec_rsBlockNum(__spec__) * QRspec_rsEccCodes1(__spec__))
|
||||
|
||||
/******************************************************************************
|
||||
* Version information pattern
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return BCH encoded version information pattern that is used for the symbol
|
||||
* of version 7 or greater. Use lower 18 bits.
|
||||
* @param version
|
||||
* @return BCH encoded version information pattern
|
||||
*/
|
||||
extern unsigned int QRspec_getVersionPattern(int version);
|
||||
|
||||
/******************************************************************************
|
||||
* Format information
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return BCH encoded format information pattern.
|
||||
* @param mask
|
||||
* @param level
|
||||
* @return BCH encoded format information pattern
|
||||
*/
|
||||
extern unsigned int QRspec_getFormatInfo(int mask, QRecLevel level);
|
||||
|
||||
/******************************************************************************
|
||||
* Frame
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Return a copy of initialized frame.
|
||||
* When the same version is requested twice or more, a copy of cached frame
|
||||
* is returned.
|
||||
* @param version
|
||||
* @return Array of unsigned char. You can free it by free().
|
||||
*/
|
||||
extern unsigned char *QRspec_newFrame(int version);
|
||||
|
||||
/**
|
||||
* Clear the frame cache. Typically for debug.
|
||||
*/
|
||||
extern void QRspec_clearCache(void);
|
||||
|
||||
/******************************************************************************
|
||||
* Mode indicator
|
||||
*****************************************************************************/
|
||||
|
||||
/**
|
||||
* Mode indicator. See Table 2 of JIS X0510:2004, pp.16.
|
||||
*/
|
||||
#define QRSPEC_MODEID_ECI 7
|
||||
#define QRSPEC_MODEID_NUM 1
|
||||
#define QRSPEC_MODEID_AN 2
|
||||
#define QRSPEC_MODEID_8 4
|
||||
#define QRSPEC_MODEID_KANJI 8
|
||||
#define QRSPEC_MODEID_FNC1FIRST 5
|
||||
#define QRSPEC_MODEID_FNC1SECOND 9
|
||||
#define QRSPEC_MODEID_STRUCTURE 3
|
||||
#define QRSPEC_MODEID_TERMINATOR 0
|
||||
|
||||
#endif /* __QRSPEC_H__ */
|
327
src/qqrencode/qrencode/rscode.c
Normal file
327
src/qqrencode/qrencode/rscode.c
Normal file
|
@ -0,0 +1,327 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Reed solomon encoder. This code is taken from Phil Karn's libfec then
|
||||
* editted and packed into a pair of .c and .h files.
|
||||
*
|
||||
* Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
|
||||
* (libfec is released under the GNU Lesser General Public License.)
|
||||
*
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
# include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "rscode.h"
|
||||
|
||||
/* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
|
||||
*
|
||||
*/
|
||||
typedef unsigned char data_t;
|
||||
|
||||
|
||||
/**
|
||||
* Reed-Solomon codec control block
|
||||
*/
|
||||
struct _RS {
|
||||
int mm; /* Bits per symbol */
|
||||
int nn; /* Symbols per block (= (1<<mm)-1) */
|
||||
data_t *alpha_to; /* log lookup table */
|
||||
data_t *index_of; /* Antilog lookup table */
|
||||
data_t *genpoly; /* Generator polynomial */
|
||||
int nroots; /* Number of generator roots = number of parity symbols */
|
||||
int fcr; /* First consecutive root, index form */
|
||||
int prim; /* Primitive element, index form */
|
||||
int iprim; /* prim-th root of 1, index form */
|
||||
int pad; /* Padding bytes in shortened block */
|
||||
int gfpoly;
|
||||
struct _RS *next;
|
||||
};
|
||||
|
||||
static RS *rslist = NULL;
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
static pthread_mutex_t rslist_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
|
||||
static inline int modnn(RS *rs, int x){
|
||||
while (x >= rs->nn) {
|
||||
x -= rs->nn;
|
||||
x = (x >> rs->mm) + (x & rs->nn);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
#define MODNN(x) modnn(rs,x)
|
||||
|
||||
#define MM (rs->mm)
|
||||
#define NN (rs->nn)
|
||||
#define ALPHA_TO (rs->alpha_to)
|
||||
#define INDEX_OF (rs->index_of)
|
||||
#define GENPOLY (rs->genpoly)
|
||||
#define NROOTS (rs->nroots)
|
||||
#define FCR (rs->fcr)
|
||||
#define PRIM (rs->prim)
|
||||
#define IPRIM (rs->iprim)
|
||||
#define PAD (rs->pad)
|
||||
#define A0 (NN)
|
||||
|
||||
|
||||
/* Initialize a Reed-Solomon codec
|
||||
* symsize = symbol size, bits
|
||||
* gfpoly = Field generator polynomial coefficients
|
||||
* fcr = first root of RS code generator polynomial, index form
|
||||
* prim = primitive element to generate polynomial roots
|
||||
* nroots = RS code generator polynomial degree (number of roots)
|
||||
* pad = padding bytes at front of shortened block
|
||||
*/
|
||||
static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
|
||||
{
|
||||
RS *rs;
|
||||
|
||||
|
||||
/* Common code for intializing a Reed-Solomon control block (char or int symbols)
|
||||
* Copyright 2004 Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
//#undef NULL
|
||||
//#define NULL ((void *)0)
|
||||
|
||||
int i, j, sr,root,iprim;
|
||||
|
||||
rs = NULL;
|
||||
/* Check parameter ranges */
|
||||
if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(fcr < 0 || fcr >= (1<<symsize))
|
||||
goto done;
|
||||
if(prim <= 0 || prim >= (1<<symsize))
|
||||
goto done;
|
||||
if(nroots < 0 || nroots >= (1<<symsize))
|
||||
goto done; /* Can't have more roots than symbol values! */
|
||||
if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
|
||||
goto done; /* Too much padding */
|
||||
|
||||
rs = (RS *)calloc(1,sizeof(RS));
|
||||
if(rs == NULL)
|
||||
goto done;
|
||||
|
||||
rs->mm = symsize;
|
||||
rs->nn = (1<<symsize)-1;
|
||||
rs->pad = pad;
|
||||
|
||||
rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
|
||||
if(rs->alpha_to == NULL){
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
|
||||
if(rs->index_of == NULL){
|
||||
free(rs->alpha_to);
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Generate Galois field lookup tables */
|
||||
rs->index_of[0] = A0; /* log(zero) = -inf */
|
||||
rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
|
||||
sr = 1;
|
||||
for(i=0;i<rs->nn;i++){
|
||||
rs->index_of[sr] = i;
|
||||
rs->alpha_to[i] = sr;
|
||||
sr <<= 1;
|
||||
if(sr & (1<<symsize))
|
||||
sr ^= gfpoly;
|
||||
sr &= rs->nn;
|
||||
}
|
||||
if(sr != 1){
|
||||
/* field generator polynomial is not primitive! */
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Form RS code generator polynomial from its roots */
|
||||
rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
|
||||
if(rs->genpoly == NULL){
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
rs->fcr = fcr;
|
||||
rs->prim = prim;
|
||||
rs->nroots = nroots;
|
||||
rs->gfpoly = gfpoly;
|
||||
|
||||
/* Find prim-th root of 1, used in decoding */
|
||||
for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
|
||||
;
|
||||
rs->iprim = iprim / prim;
|
||||
|
||||
rs->genpoly[0] = 1;
|
||||
for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
|
||||
rs->genpoly[i+1] = 1;
|
||||
|
||||
/* Multiply rs->genpoly[] by @**(root + x) */
|
||||
for (j = i; j > 0; j--){
|
||||
if (rs->genpoly[j] != 0)
|
||||
rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
|
||||
else
|
||||
rs->genpoly[j] = rs->genpoly[j-1];
|
||||
}
|
||||
/* rs->genpoly[0] can never be zero */
|
||||
rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
|
||||
}
|
||||
/* convert rs->genpoly[] to index form for quicker encoding */
|
||||
for (i = 0; i <= nroots; i++)
|
||||
rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
|
||||
done:;
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
|
||||
{
|
||||
RS *rs;
|
||||
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_lock(&rslist_mutex);
|
||||
#endif
|
||||
for(rs = rslist; rs != NULL; rs = rs->next) {
|
||||
if(rs->pad != pad) continue;
|
||||
if(rs->nroots != nroots) continue;
|
||||
if(rs->mm != symsize) continue;
|
||||
if(rs->gfpoly != gfpoly) continue;
|
||||
if(rs->fcr != fcr) continue;
|
||||
if(rs->prim != prim) continue;
|
||||
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
rs = init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad);
|
||||
if(rs == NULL) goto DONE;
|
||||
rs->next = rslist;
|
||||
rslist = rs;
|
||||
|
||||
DONE:
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_unlock(&rslist_mutex);
|
||||
#endif
|
||||
return rs;
|
||||
}
|
||||
|
||||
|
||||
void free_rs_char(RS *rs)
|
||||
{
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs->genpoly);
|
||||
free(rs);
|
||||
}
|
||||
|
||||
void free_rs_cache(void)
|
||||
{
|
||||
RS *rs, *next;
|
||||
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_lock(&rslist_mutex);
|
||||
#endif
|
||||
rs = rslist;
|
||||
while(rs != NULL) {
|
||||
next = rs->next;
|
||||
free_rs_char(rs);
|
||||
rs = next;
|
||||
}
|
||||
rslist = NULL;
|
||||
#ifdef HAVE_LIBPTHREAD
|
||||
pthread_mutex_unlock(&rslist_mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* The guts of the Reed-Solomon encoder, meant to be #included
|
||||
* into a function body with the following typedefs, macros and variables supplied
|
||||
* according to the code parameters:
|
||||
|
||||
* data_t - a typedef for the data symbol
|
||||
* data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
|
||||
* data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
|
||||
* NROOTS - the number of roots in the RS code generator polynomial,
|
||||
* which is the same as the number of parity symbols in a block.
|
||||
Integer variable or literal.
|
||||
*
|
||||
* NN - the total number of symbols in a RS block. Integer variable or literal.
|
||||
* PAD - the number of pad symbols in a block. Integer variable or literal.
|
||||
* ALPHA_TO - The address of an array of NN elements to convert Galois field
|
||||
* elements in index (log) form to polynomial form. Read only.
|
||||
* INDEX_OF - The address of an array of NN elements to convert Galois field
|
||||
* elements in polynomial form to index (log) form. Read only.
|
||||
* MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
|
||||
* GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
|
||||
|
||||
* The memset() and memmove() functions are used. The appropriate header
|
||||
* file declaring these functions (usually <string.h>) must be included by the calling
|
||||
* program.
|
||||
|
||||
* Copyright 2004, Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
|
||||
#undef A0
|
||||
#define A0 (NN) /* Special reserved value encoding zero in index form */
|
||||
|
||||
void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
|
||||
{
|
||||
int i, j;
|
||||
data_t feedback;
|
||||
|
||||
memset(parity,0,NROOTS*sizeof(data_t));
|
||||
|
||||
for(i=0;i<NN-NROOTS-PAD;i++){
|
||||
feedback = INDEX_OF[data[i] ^ parity[0]];
|
||||
if(feedback != A0){ /* feedback term is non-zero */
|
||||
#ifdef UNNORMALIZED
|
||||
/* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
|
||||
* always be for the polynomials constructed by init_rs()
|
||||
*/
|
||||
feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
|
||||
#endif
|
||||
for(j=1;j<NROOTS;j++)
|
||||
parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
|
||||
}
|
||||
/* Shift */
|
||||
memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
|
||||
if(feedback != A0)
|
||||
parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
|
||||
else
|
||||
parity[NROOTS-1] = 0;
|
||||
}
|
||||
}
|
41
src/qqrencode/qrencode/rscode.h
Normal file
41
src/qqrencode/qrencode/rscode.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Reed solomon encoder. This code is taken from Phil Karn's libfec then
|
||||
* editted and packed into a pair of .c and .h files.
|
||||
*
|
||||
* Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
|
||||
* (libfec is released under the GNU Lesser General Public License.)
|
||||
*
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __RSCODE_H__
|
||||
#define __RSCODE_H__
|
||||
|
||||
/*
|
||||
* General purpose RS codec, 8-bit symbols.
|
||||
*/
|
||||
|
||||
typedef struct _RS RS;
|
||||
|
||||
extern RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad);
|
||||
extern void encode_rs_char(RS *rs, const unsigned char *data, unsigned char *parity);
|
||||
extern void free_rs_char(RS *rs);
|
||||
extern void free_rs_cache(void);
|
||||
|
||||
#endif /* __RSCODE_H__ */
|
326
src/qqrencode/qrencode/split.c
Normal file
326
src/qqrencode/qrencode/split.c
Normal file
|
@ -0,0 +1,326 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Input data splitter.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* The following data / specifications are taken from
|
||||
* "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
|
||||
* or
|
||||
* "Automatic identification and data capture techniques --
|
||||
* QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "qrencode.h"
|
||||
#include "qrinput.h"
|
||||
#include "qrspec.h"
|
||||
#include "split.h"
|
||||
|
||||
#define isdigit(__c__) ((unsigned char)((signed char)(__c__) - '0') < 10)
|
||||
#define isalnum(__c__) (QRinput_lookAnTable(__c__) >= 0)
|
||||
|
||||
#if !HAVE_STRDUP
|
||||
#undef strdup
|
||||
char *strdup(const char *s)
|
||||
{
|
||||
size_t len = strlen(s) + 1;
|
||||
void *new = malloc(len);
|
||||
if(new == NULL) return NULL;
|
||||
return (char *)memcpy(new, s, len);
|
||||
}
|
||||
#endif
|
||||
|
||||
static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
|
||||
{
|
||||
unsigned char c, d;
|
||||
unsigned int word;
|
||||
|
||||
c = string[0];
|
||||
|
||||
if(c == '\0') return QR_MODE_NUL;
|
||||
if(isdigit(c)) {
|
||||
return QR_MODE_NUM;
|
||||
} else if(isalnum(c)) {
|
||||
return QR_MODE_AN;
|
||||
} else if(hint == QR_MODE_KANJI) {
|
||||
d = string[1];
|
||||
if(d != '\0') {
|
||||
word = ((unsigned int)c << 8) | d;
|
||||
if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) {
|
||||
return QR_MODE_KANJI;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QR_MODE_8;
|
||||
}
|
||||
|
||||
static int Split_eatNum(const char *string, QRinput *input, QRencodeMode hint);
|
||||
static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint);
|
||||
static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint);
|
||||
static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint);
|
||||
|
||||
static int Split_eatNum(const char *string, QRinput *input,QRencodeMode hint)
|
||||
{
|
||||
const char *p;
|
||||
int ret;
|
||||
int run;
|
||||
int dif;
|
||||
int ln;
|
||||
QRencodeMode mode;
|
||||
|
||||
ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
|
||||
|
||||
p = string;
|
||||
while(isdigit(*p)) {
|
||||
p++;
|
||||
}
|
||||
run = p - string;
|
||||
mode = Split_identifyMode(p, hint);
|
||||
if(mode == QR_MODE_8) {
|
||||
dif = QRinput_estimateBitsModeNum(run) + 4 + ln
|
||||
+ QRinput_estimateBitsMode8(1) /* + 4 + l8 */
|
||||
- QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
|
||||
if(dif > 0) {
|
||||
return Split_eat8(string, input, hint);
|
||||
}
|
||||
}
|
||||
if(mode == QR_MODE_AN) {
|
||||
dif = QRinput_estimateBitsModeNum(run) + 4 + ln
|
||||
+ QRinput_estimateBitsModeAn(1) /* + 4 + la */
|
||||
- QRinput_estimateBitsModeAn(run + 1) /* - 4 - la */;
|
||||
if(dif > 0) {
|
||||
return Split_eatAn(string, input, hint);
|
||||
}
|
||||
}
|
||||
|
||||
ret = QRinput_append(input, QR_MODE_NUM, run, (unsigned char *)string);
|
||||
if(ret < 0) return -1;
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
|
||||
{
|
||||
const char *p, *q;
|
||||
int ret;
|
||||
int run;
|
||||
int dif;
|
||||
int la, ln;
|
||||
|
||||
la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
|
||||
ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
|
||||
|
||||
p = string;
|
||||
while(isalnum(*p)) {
|
||||
if(isdigit(*p)) {
|
||||
q = p;
|
||||
while(isdigit(*q)) {
|
||||
q++;
|
||||
}
|
||||
dif = QRinput_estimateBitsModeAn(p - string) /* + 4 + la */
|
||||
+ QRinput_estimateBitsModeNum(q - p) + 4 + ln
|
||||
+ (isalnum(*q)?(4 + ln):0)
|
||||
- QRinput_estimateBitsModeAn(q - string) /* - 4 - la */;
|
||||
if(dif < 0) {
|
||||
break;
|
||||
} else {
|
||||
p = q;
|
||||
}
|
||||
} else {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
run = p - string;
|
||||
|
||||
if(*p && !isalnum(*p)) {
|
||||
dif = QRinput_estimateBitsModeAn(run) + 4 + la
|
||||
+ QRinput_estimateBitsMode8(1) /* + 4 + l8 */
|
||||
- QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
|
||||
if(dif > 0) {
|
||||
return Split_eat8(string, input, hint);
|
||||
}
|
||||
}
|
||||
|
||||
ret = QRinput_append(input, QR_MODE_AN, run, (unsigned char *)string);
|
||||
if(ret < 0) return -1;
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
|
||||
{
|
||||
const char *p;
|
||||
int ret;
|
||||
int run;
|
||||
|
||||
p = string;
|
||||
while(Split_identifyMode(p, hint) == QR_MODE_KANJI) {
|
||||
p += 2;
|
||||
}
|
||||
run = p - string;
|
||||
ret = QRinput_append(input, QR_MODE_KANJI, run, (unsigned char *)string);
|
||||
if(ret < 0) return -1;
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
|
||||
{
|
||||
const char *p, *q;
|
||||
QRencodeMode mode;
|
||||
int ret;
|
||||
int run;
|
||||
int dif;
|
||||
int la, ln, l8;
|
||||
int swcost;
|
||||
|
||||
la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
|
||||
ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
|
||||
l8 = QRspec_lengthIndicator(QR_MODE_8, input->version);
|
||||
|
||||
p = string + 1;
|
||||
while(*p != '\0') {
|
||||
mode = Split_identifyMode(p, hint);
|
||||
if(mode == QR_MODE_KANJI) {
|
||||
break;
|
||||
}
|
||||
if(mode == QR_MODE_NUM) {
|
||||
q = p;
|
||||
while(isdigit(*q)) {
|
||||
q++;
|
||||
}
|
||||
if(Split_identifyMode(q, hint) == QR_MODE_8) {
|
||||
swcost = 4 + l8;
|
||||
} else {
|
||||
swcost = 0;
|
||||
}
|
||||
dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
|
||||
+ QRinput_estimateBitsModeNum(q - p) + 4 + ln
|
||||
+ swcost
|
||||
- QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
|
||||
if(dif < 0) {
|
||||
break;
|
||||
} else {
|
||||
p = q;
|
||||
}
|
||||
} else if(mode == QR_MODE_AN) {
|
||||
q = p;
|
||||
while(isalnum(*q)) {
|
||||
q++;
|
||||
}
|
||||
if(Split_identifyMode(q, hint) == QR_MODE_8) {
|
||||
swcost = 4 + l8;
|
||||
} else {
|
||||
swcost = 0;
|
||||
}
|
||||
dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
|
||||
+ QRinput_estimateBitsModeAn(q - p) + 4 + la
|
||||
+ swcost
|
||||
- QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
|
||||
if(dif < 0) {
|
||||
break;
|
||||
} else {
|
||||
p = q;
|
||||
}
|
||||
} else {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
run = p - string;
|
||||
ret = QRinput_append(input, QR_MODE_8, run, (unsigned char *)string);
|
||||
if(ret < 0) return -1;
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
static int Split_splitString(const char *string, QRinput *input,
|
||||
QRencodeMode hint)
|
||||
{
|
||||
int length;
|
||||
QRencodeMode mode;
|
||||
|
||||
if(*string == '\0') return 0;
|
||||
|
||||
mode = Split_identifyMode(string, hint);
|
||||
if(mode == QR_MODE_NUM) {
|
||||
length = Split_eatNum(string, input, hint);
|
||||
} else if(mode == QR_MODE_AN) {
|
||||
length = Split_eatAn(string, input, hint);
|
||||
} else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) {
|
||||
length = Split_eatKanji(string, input, hint);
|
||||
} else {
|
||||
length = Split_eat8(string, input, hint);
|
||||
}
|
||||
if(length == 0) return 0;
|
||||
if(length < 0) return -1;
|
||||
return Split_splitString(&string[length], input, hint);
|
||||
}
|
||||
|
||||
static char *dupAndToUpper(const char *str, QRencodeMode hint)
|
||||
{
|
||||
char *newstr, *p;
|
||||
QRencodeMode mode;
|
||||
|
||||
newstr = strdup(str);
|
||||
if(newstr == NULL) return NULL;
|
||||
|
||||
p = newstr;
|
||||
while(*p != '\0') {
|
||||
mode = Split_identifyMode(p, hint);
|
||||
if(mode == QR_MODE_KANJI) {
|
||||
p += 2;
|
||||
} else {
|
||||
if (*p >= 'a' && *p <= 'z') {
|
||||
*p = (char)((int)*p - 32);
|
||||
}
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
return newstr;
|
||||
}
|
||||
|
||||
int Split_splitStringToQRinput(const char *string, QRinput *input,
|
||||
QRencodeMode hint, int casesensitive)
|
||||
{
|
||||
char *newstr;
|
||||
int ret;
|
||||
|
||||
if(string == NULL || *string == '\0') {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if(!casesensitive) {
|
||||
newstr = dupAndToUpper(string, hint);
|
||||
if(newstr == NULL) return -1;
|
||||
ret = Split_splitString(newstr, input, hint);
|
||||
free(newstr);
|
||||
} else {
|
||||
ret = Split_splitString(string, input, hint);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
47
src/qqrencode/qrencode/split.h
Normal file
47
src/qqrencode/qrencode/split.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* qrencode - QR Code encoder
|
||||
*
|
||||
* Input data splitter.
|
||||
* Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
|
||||
*
|
||||
* The following data / specifications are taken from
|
||||
* "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
|
||||
* or
|
||||
* "Automatic identification and data capture techniques --
|
||||
* QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __SPLIT_H__
|
||||
#define __SPLIT_H__
|
||||
|
||||
#include "qrencode.h"
|
||||
|
||||
/**
|
||||
* Split the input string (null terminated) into QRinput.
|
||||
* @param string input string
|
||||
* @param hint give QR_MODE_KANJI if the input string contains Kanji character encoded in Shift-JIS. If not, give QR_MODE_8.
|
||||
* @param casesensitive 0 for case-insensitive encoding (all alphabet characters are replaced to UPPER-CASE CHARACTERS.
|
||||
* @retval 0 success.
|
||||
* @retval -1 an error occurred. errno is set to indicate the error. See
|
||||
* Exceptions for the details.
|
||||
* @throw EINVAL invalid input object.
|
||||
* @throw ENOMEM unable to allocate memory for input objects.
|
||||
*/
|
||||
extern int Split_splitStringToQRinput(const char *string, QRinput *input,
|
||||
QRencodeMode hint, int casesensitive);
|
||||
|
||||
#endif /* __SPLIT_H__ */
|
144
src/qzxing/CameraImageWrapper.cpp
Normal file
144
src/qzxing/CameraImageWrapper.cpp
Normal file
|
@ -0,0 +1,144 @@
|
|||
#include "CameraImageWrapper.h"
|
||||
#include <QColor>
|
||||
|
||||
CameraImageWrapper::CameraImageWrapper() : LuminanceSource(), isSmoothTransformationEnabled(false)
|
||||
{
|
||||
}
|
||||
|
||||
CameraImageWrapper::CameraImageWrapper(QImage& image) : LuminanceSource() , isSmoothTransformationEnabled(false)
|
||||
{
|
||||
setImage(image);
|
||||
}
|
||||
|
||||
CameraImageWrapper::CameraImageWrapper(CameraImageWrapper& otherInstance) : LuminanceSource() , isSmoothTransformationEnabled(false)
|
||||
{
|
||||
image = otherInstance.getOriginalImage().copy();
|
||||
}
|
||||
|
||||
CameraImageWrapper::~CameraImageWrapper()
|
||||
{
|
||||
}
|
||||
|
||||
int CameraImageWrapper::getWidth() const
|
||||
{
|
||||
return image.width();
|
||||
}
|
||||
|
||||
int CameraImageWrapper::getHeight() const
|
||||
{
|
||||
return image.height();
|
||||
}
|
||||
|
||||
unsigned char CameraImageWrapper::getPixel(int x, int y) const
|
||||
{
|
||||
QRgb pixel = image.pixel(x,y);
|
||||
|
||||
return qGray(pixel);//((qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3);
|
||||
}
|
||||
|
||||
unsigned char* CameraImageWrapper::copyMatrix() const
|
||||
{
|
||||
unsigned char* newMatrix = (unsigned char*)malloc(image.width()*image.height()*sizeof(unsigned char));
|
||||
|
||||
int cnt = 0;
|
||||
for(int i=0; i<image.width(); i++)
|
||||
{
|
||||
for(int j=0; j<image.height(); j++)
|
||||
{
|
||||
newMatrix[cnt++] = getPixel(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
return newMatrix;
|
||||
}
|
||||
|
||||
bool CameraImageWrapper::setImage(QString fileName)
|
||||
{
|
||||
bool isLoaded = image.load(fileName);
|
||||
|
||||
if(!isLoaded)
|
||||
return false;
|
||||
|
||||
// if(image.width() > QApplication::desktop()->width())
|
||||
// image = image.scaled(QApplication::desktop()->width(), image.height(), Qt::IgnoreAspectRatio);
|
||||
|
||||
// if(image.height() > QApplication::desktop()->height())
|
||||
// image = image.scaled(image.width(), QApplication::desktop()->height(), Qt::IgnoreAspectRatio);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CameraImageWrapper::setImage(QImage newImage)
|
||||
{
|
||||
if(newImage.isNull())
|
||||
return false;
|
||||
|
||||
image = newImage.copy();
|
||||
|
||||
if(image.width() > 640)
|
||||
image = image.scaled(640, image.height(), Qt::KeepAspectRatio, isSmoothTransformationEnabled ? Qt::SmoothTransformation : Qt::FastTransformation);
|
||||
return true;
|
||||
}
|
||||
|
||||
QImage CameraImageWrapper::grayScaleImage(QImage::Format f)
|
||||
{
|
||||
QImage tmp(image.width(), image.height(), f);
|
||||
for(int i=0; i<image.width(); i++)
|
||||
{
|
||||
for(int j=0; j<image.height(); j++)
|
||||
{
|
||||
int pix = (int)getPixel(i,j);
|
||||
tmp.setPixel(i,j, qRgb(pix ,pix,pix));
|
||||
}
|
||||
}
|
||||
|
||||
return tmp;
|
||||
|
||||
//return image.convertToFormat(f);
|
||||
}
|
||||
|
||||
QImage CameraImageWrapper::getOriginalImage()
|
||||
{
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
unsigned char* CameraImageWrapper::getRow(int y, unsigned char* row)
|
||||
{
|
||||
int width = getWidth();
|
||||
|
||||
if (row == NULL)
|
||||
{
|
||||
row = new unsigned char[width];
|
||||
pRow = row;
|
||||
}
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
row[x] = getPixel(x,y);
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
unsigned char* CameraImageWrapper::getMatrix()
|
||||
{
|
||||
int width = getWidth();
|
||||
int height = getHeight();
|
||||
unsigned char* matrix = new unsigned char[width*height];
|
||||
unsigned char* m = matrix;
|
||||
|
||||
for(int y=0; y<height; y++)
|
||||
{
|
||||
unsigned char* tmpRow;
|
||||
memcpy(m, tmpRow = getRow(y, NULL), width);
|
||||
m += width * sizeof(unsigned char);
|
||||
|
||||
delete tmpRow;
|
||||
}
|
||||
|
||||
pMatrix = matrix;
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void CameraImageWrapper::setSmoothTransformation(bool enable)
|
||||
{
|
||||
isSmoothTransformationEnabled = enable;
|
||||
}
|
47
src/qzxing/CameraImageWrapper.h
Normal file
47
src/qzxing/CameraImageWrapper.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
#ifndef CAMERAIMAGE_H
|
||||
#define CAMERAIMAGE_H
|
||||
|
||||
#include <QImage>
|
||||
#include <QString>
|
||||
#include <zxing/LuminanceSource.h>
|
||||
|
||||
using namespace zxing;
|
||||
|
||||
class CameraImageWrapper : public LuminanceSource
|
||||
{
|
||||
public:
|
||||
CameraImageWrapper();
|
||||
CameraImageWrapper(QImage& image);
|
||||
CameraImageWrapper(CameraImageWrapper& otherInstance);
|
||||
~CameraImageWrapper();
|
||||
|
||||
int getWidth() const;
|
||||
int getHeight() const;
|
||||
|
||||
unsigned char getPixel(int x, int y) const;
|
||||
unsigned char* copyMatrix() const;
|
||||
|
||||
/**
|
||||
* Set the source of the image. If it fails, returns false.
|
||||
*/
|
||||
bool setImage(QString fileName);
|
||||
bool setImage(QImage newImage);
|
||||
|
||||
QImage grayScaleImage(QImage::Format f);
|
||||
QImage getOriginalImage();
|
||||
|
||||
|
||||
// Callers take ownership of the returned memory and must call delete [] on it themselves.
|
||||
unsigned char* getRow(int y, unsigned char* row);
|
||||
unsigned char* getMatrix();
|
||||
|
||||
void setSmoothTransformation(bool enable);
|
||||
|
||||
private:
|
||||
QImage image;
|
||||
unsigned char* pRow;
|
||||
unsigned char* pMatrix;
|
||||
bool isSmoothTransformationEnabled;
|
||||
};
|
||||
|
||||
#endif //CAMERAIMAGE_H
|
191
src/qzxing/QZXing.pri
Normal file
191
src/qzxing/QZXing.pri
Normal file
|
@ -0,0 +1,191 @@
|
|||
QT += core gui quick
|
||||
|
||||
INCLUDEPATH += \
|
||||
$$PWD \
|
||||
$$PWD/zxing
|
||||
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/CameraImageWrapper.h \
|
||||
$$PWD/qzxing.h \
|
||||
$$PWD/qzxing_global.h \
|
||||
$$PWD/zxing/ResultPointCallback.h \
|
||||
$$PWD/zxing/ResultPoint.h \
|
||||
$$PWD/zxing/Result.h \
|
||||
$$PWD/zxing/ReaderException.h \
|
||||
$$PWD/zxing/Reader.h \
|
||||
$$PWD/zxing/NotFoundException.h \
|
||||
$$PWD/zxing/MultiFormatReader.h \
|
||||
$$PWD/zxing/LuminanceSource.h \
|
||||
$$PWD/zxing/FormatException.h \
|
||||
$$PWD/zxing/Exception.h \
|
||||
$$PWD/zxing/DecodeHints.h \
|
||||
$$PWD/zxing/BinaryBitmap.h \
|
||||
$$PWD/zxing/Binarizer.h \
|
||||
$$PWD/zxing/BarcodeFormat.h \
|
||||
$$PWD/zxing/aztec/AztecReader.h \
|
||||
$$PWD/zxing/aztec/AztecDetectorResult.h \
|
||||
$$PWD/zxing/aztec/decoder/Decoder.h \
|
||||
$$PWD/zxing/aztec/detector/Detector.h \
|
||||
$$PWD/zxing/common/StringUtils.h \
|
||||
$$PWD/zxing/common/Str.h \
|
||||
$$PWD/zxing/common/Point.h \
|
||||
$$PWD/zxing/common/PerspectiveTransform.h \
|
||||
$$PWD/zxing/common/IllegalArgumentException.h \
|
||||
$$PWD/zxing/common/HybridBinarizer.h \
|
||||
$$PWD/zxing/common/GridSampler.h \
|
||||
$$PWD/zxing/common/GreyscaleRotatedLuminanceSource.h \
|
||||
$$PWD/zxing/common/GreyscaleLuminanceSource.h \
|
||||
$$PWD/zxing/common/GlobalHistogramBinarizer.h \
|
||||
$$PWD/zxing/common/EdgeDetector.h \
|
||||
$$PWD/zxing/common/DetectorResult.h \
|
||||
$$PWD/zxing/common/DecoderResult.h \
|
||||
$$PWD/zxing/common/Counted.h \
|
||||
$$PWD/zxing/common/CharacterSetECI.h \
|
||||
$$PWD/zxing/common/BitSource.h \
|
||||
$$PWD/zxing/common/BitMatrix.h \
|
||||
$$PWD/zxing/common/BitArray.h \
|
||||
$$PWD/zxing/common/Array.h \
|
||||
$$PWD/zxing/common/detector/WhiteRectangleDetector.h \
|
||||
$$PWD/zxing/common/detector/MonochromeRectangleDetector.h \
|
||||
$$PWD/zxing/common/reedsolomon/ReedSolomonException.h \
|
||||
$$PWD/zxing/common/reedsolomon/ReedSolomonDecoder.h \
|
||||
$$PWD/zxing/common/reedsolomon/GenericGFPoly.h \
|
||||
$$PWD/zxing/common/reedsolomon/GenericGF.h \
|
||||
$$PWD/zxing/datamatrix/Version.h \
|
||||
$$PWD/zxing/datamatrix/DataMatrixReader.h \
|
||||
$$PWD/zxing/datamatrix/decoder/Decoder.h \
|
||||
$$PWD/zxing/datamatrix/decoder/DecodedBitStreamParser.h \
|
||||
$$PWD/zxing/datamatrix/decoder/DataBlock.h \
|
||||
$$PWD/zxing/datamatrix/decoder/BitMatrixParser.h \
|
||||
$$PWD/zxing/datamatrix/detector/MonochromeRectangleDetector.h \
|
||||
$$PWD/zxing/datamatrix/detector/DetectorException.h \
|
||||
$$PWD/zxing/datamatrix/detector/Detector.h \
|
||||
$$PWD/zxing/datamatrix/detector/CornerPoint.h \
|
||||
$$PWD/zxing/oned/UPCEReader.h \
|
||||
$$PWD/zxing/oned/UPCEANReader.h \
|
||||
$$PWD/zxing/oned/UPCAReader.h \
|
||||
$$PWD/zxing/oned/OneDResultPoint.h \
|
||||
$$PWD/zxing/oned/OneDReader.h \
|
||||
$$PWD/zxing/oned/MultiFormatUPCEANReader.h \
|
||||
$$PWD/zxing/oned/MultiFormatOneDReader.h \
|
||||
$$PWD/zxing/oned/ITFReader.h \
|
||||
$$PWD/zxing/oned/EAN13Reader.h \
|
||||
$$PWD/zxing/oned/EAN8Reader.h \
|
||||
$$PWD/zxing/oned/Code128Reader.h \
|
||||
$$PWD/zxing/oned/Code39Reader.h \
|
||||
$$PWD/zxing/qrcode/Version.h \
|
||||
$$PWD/zxing/qrcode/QRCodeReader.h \
|
||||
$$PWD/zxing/qrcode/FormatInformation.h \
|
||||
$$PWD/zxing/qrcode/ErrorCorrectionLevel.h \
|
||||
$$PWD/zxing/qrcode/decoder/Mode.h \
|
||||
$$PWD/zxing/qrcode/decoder/Decoder.h \
|
||||
$$PWD/zxing/qrcode/decoder/DecodedBitStreamParser.h \
|
||||
$$PWD/zxing/qrcode/decoder/DataMask.h \
|
||||
$$PWD/zxing/qrcode/decoder/DataBlock.h \
|
||||
$$PWD/zxing/qrcode/decoder/BitMatrixParser.h \
|
||||
$$PWD/zxing/qrcode/detector/QREdgeDetector.h \
|
||||
$$PWD/zxing/qrcode/detector/FinderPatternInfo.h \
|
||||
$$PWD/zxing/qrcode/detector/FinderPatternFinder.h \
|
||||
$$PWD/zxing/qrcode/detector/FinderPattern.h \
|
||||
$$PWD/zxing/qrcode/detector/Detector.h \
|
||||
$$PWD/zxing/qrcode/detector/AlignmentPatternFinder.h \
|
||||
$$PWD/zxing/qrcode/detector/AlignmentPattern.h \
|
||||
$$PWD/zxing/multi/MultipleBarcodeReader.h \
|
||||
$$PWD/zxing/multi/GenericMultipleBarcodeReader.h \
|
||||
$$PWD/zxing/multi/ByQuadrantReader.h \
|
||||
$$PWD/zxing/multi/qrcode/QRCodeMultiReader.h \
|
||||
$$PWD/zxing/multi/qrcode/detector/MultiFinderPatternFinder.h \
|
||||
$$PWD/zxing/multi/qrcode/detector/MultiDetector.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/CameraImageWrapper.cpp \
|
||||
$$PWD/qzxing.cpp \
|
||||
$$PWD/zxing/ResultPointCallback.cpp \
|
||||
$$PWD/zxing/ResultPoint.cpp \
|
||||
$$PWD/zxing/Result.cpp \
|
||||
$$PWD/zxing/ReaderException.cpp \
|
||||
$$PWD/zxing/Reader.cpp \
|
||||
$$PWD/zxing/NotFoundException.cpp \
|
||||
$$PWD/zxing/MultiFormatReader.cpp \
|
||||
$$PWD/zxing/LuminanceSource.cpp \
|
||||
$$PWD/zxing/FormatException.cpp \
|
||||
$$PWD/zxing/Exception.cpp \
|
||||
$$PWD/zxing/DecodeHints.cpp \
|
||||
$$PWD/zxing/BinaryBitmap.cpp \
|
||||
$$PWD/zxing/Binarizer.cpp \
|
||||
$$PWD/zxing/BarcodeFormat.cpp \
|
||||
$$PWD/zxing/aztec/AztecReader.cpp \
|
||||
$$PWD/zxing/aztec/AztecDetectorResult.cpp \
|
||||
$$PWD/zxing/common/StringUtils.cpp \
|
||||
$$PWD/zxing/common/Str.cpp \
|
||||
$$PWD/zxing/common/PerspectiveTransform.cpp \
|
||||
$$PWD/zxing/common/IllegalArgumentException.cpp \
|
||||
$$PWD/zxing/common/HybridBinarizer.cpp \
|
||||
$$PWD/zxing/common/GridSampler.cpp \
|
||||
$$PWD/zxing/common/GreyscaleRotatedLuminanceSource.cpp \
|
||||
$$PWD/zxing/common/GreyscaleLuminanceSource.cpp \
|
||||
$$PWD/zxing/common/GlobalHistogramBinarizer.cpp \
|
||||
$$PWD/zxing/common/EdgeDetector.cpp \
|
||||
$$PWD/zxing/common/DetectorResult.cpp \
|
||||
$$PWD/zxing/common/DecoderResult.cpp \
|
||||
$$PWD/zxing/common/Counted.cpp \
|
||||
$$PWD/zxing/common/CharacterSetECI.cpp \
|
||||
$$PWD/zxing/common/BitSource.cpp \
|
||||
$$PWD/zxing/common/BitMatrix.cpp \
|
||||
$$PWD/zxing/common/BitArray.cpp \
|
||||
$$PWD/zxing/common/Array.cpp \
|
||||
$$PWD/zxing/common/detector/WhiteRectangleDetector.cpp \
|
||||
$$PWD/zxing/common/detector/MonochromeRectangleDetector.cpp \
|
||||
$$PWD/zxing/common/reedsolomon/ReedSolomonException.cpp \
|
||||
$$PWD/zxing/common/reedsolomon/ReedSolomonDecoder.cpp \
|
||||
$$PWD/zxing/common/reedsolomon/GenericGFPoly.cpp \
|
||||
$$PWD/zxing/common/reedsolomon/GenericGF.cpp \
|
||||
$$PWD/zxing/datamatrix/DataMatrixReader.cpp \
|
||||
$$PWD/zxing/oned/UPCEReader.cpp \
|
||||
$$PWD/zxing/oned/UPCEANReader.cpp \
|
||||
$$PWD/zxing/oned/UPCAReader.cpp \
|
||||
$$PWD/zxing/oned/OneDResultPoint.cpp \
|
||||
$$PWD/zxing/oned/OneDReader.cpp \
|
||||
$$PWD/zxing/oned/MultiFormatUPCEANReader.cpp \
|
||||
$$PWD/zxing/oned/MultiFormatOneDReader.cpp \
|
||||
$$PWD/zxing/oned/ITFReader.cpp \
|
||||
$$PWD/zxing/oned/EAN13Reader.cpp \
|
||||
$$PWD/zxing/oned/EAN8Reader.cpp \
|
||||
$$PWD/zxing/oned/Code128Reader.cpp \
|
||||
$$PWD/zxing/oned/Code39Reader.cpp \
|
||||
$$PWD/zxing/qrcode/QRCodeReader.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QREdgeDetector.cpp \
|
||||
$$PWD/zxing/multi/MultipleBarcodeReader.cpp \
|
||||
$$PWD/zxing/multi/GenericMultipleBarcodeReader.cpp \
|
||||
$$PWD/zxing/multi/ByQuadrantReader.cpp \
|
||||
$$PWD/zxing/multi/qrcode/QRCodeMultiReader.cpp \
|
||||
$$PWD/zxing/multi/qrcode/detector/MultiFinderPatternFinder.cpp \
|
||||
$$PWD/zxing/multi/qrcode/detector/MultiDetector.cpp \
|
||||
$$PWD/zxing/aztec/decoder/AztecDecoder.cpp \
|
||||
$$PWD/zxing/aztec/detector/AztecDetector.cpp \
|
||||
$$PWD/zxing/datamatrix/DataMatrixVersion.cpp \
|
||||
$$PWD/zxing/datamatrix/decoder/DataMatrixDecoder.cpp \
|
||||
$$PWD/zxing/datamatrix/decoder/DataMatrixBitMatrixParser.cpp \
|
||||
$$PWD/zxing/datamatrix/decoder/DataMatrixDataBlock.cpp \
|
||||
$$PWD/zxing/datamatrix/decoder/DataMatrixDecodedBitStreamParser.cpp \
|
||||
$$PWD/zxing/datamatrix/detector/DataMatrixCornerPoint.cpp \
|
||||
$$PWD/zxing/datamatrix/detector/DataMatrixDetector.cpp \
|
||||
$$PWD/zxing/datamatrix/detector/DataMatrixDetectorException.cpp \
|
||||
$$PWD/zxing/datamatrix/detector/DataMatrixMonochromeRectangleDetector.cpp \
|
||||
$$PWD/zxing/qrcode/decoder/QRBitMatrixParser.cpp \
|
||||
$$PWD/zxing/qrcode/decoder/QRDataBlock.cpp \
|
||||
$$PWD/zxing/qrcode/decoder/QRDataMask.cpp \
|
||||
$$PWD/zxing/qrcode/decoder/QRDecodedBitStreamParser.cpp \
|
||||
$$PWD/zxing/qrcode/decoder/QRDecoder.cpp \
|
||||
$$PWD/zxing/qrcode/decoder/QRMode.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QRAlignmentPattern.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QRAlignmentPatternFinder.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QRDetector.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QRFinderPattern.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QRFinderPatternFinder.cpp \
|
||||
$$PWD/zxing/qrcode/detector/QRFinderPatternInfo.cpp \
|
||||
$$PWD/zxing/qrcode/QRVersion.cpp \
|
||||
$$PWD/zxing/qrcode/QRFormatInformation.cpp \
|
||||
$$PWD/zxing/qrcode/QRErrorCorrectionLevel.cpp
|
||||
|
179
src/qzxing/qzxing.cpp
Normal file
179
src/qzxing/qzxing.cpp
Normal file
|
@ -0,0 +1,179 @@
|
|||
#include "qzxing.h"
|
||||
|
||||
#include <QtCore>
|
||||
#include <zxing/common/GlobalHistogramBinarizer.h>
|
||||
#include <zxing/Binarizer.h>
|
||||
#include <zxing/BinaryBitmap.h>
|
||||
#include <zxing/MultiFormatReader.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
#include "CameraImageWrapper.h"
|
||||
|
||||
using namespace zxing;
|
||||
|
||||
QZXing::QZXing(QObject *parent) : QObject(parent)
|
||||
{
|
||||
decoder = new MultiFormatReader();
|
||||
setDecoder(DecoderFormat_QR_CODE);
|
||||
/*setDecoder(DecoderFormat_QR_CODE |
|
||||
DecoderFormat_DATA_MATRIX |
|
||||
DecoderFormat_UPC_E |
|
||||
DecoderFormat_UPC_A |
|
||||
DecoderFormat_EAN_8 |
|
||||
DecoderFormat_EAN_13 |
|
||||
DecoderFormat_CODE_128 |
|
||||
DecoderFormat_CODE_39 |
|
||||
DecoderFormat_ITF |
|
||||
DecoderFormat_Aztec);*/
|
||||
}
|
||||
|
||||
QZXing::~QZXing() {
|
||||
delete (MultiFormatReader*)decoder;
|
||||
decoder = 0;
|
||||
}
|
||||
|
||||
void QZXing::setDecoder(DecoderFormatType hint)
|
||||
{
|
||||
DecodeHints newHints;
|
||||
|
||||
if(hint & DecoderFormat_QR_CODE)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_QR_CODE);
|
||||
|
||||
if(hint & DecoderFormat_DATA_MATRIX)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_DATA_MATRIX);
|
||||
|
||||
if(hint & DecoderFormat_UPC_E)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_UPC_E);
|
||||
|
||||
if(hint & DecoderFormat_UPC_A)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_UPC_A);
|
||||
|
||||
if(hint & DecoderFormat_EAN_8)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_EAN_8);
|
||||
|
||||
if(hint & DecoderFormat_EAN_13)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_EAN_13);
|
||||
|
||||
if(hint & DecoderFormat_CODE_128)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_CODE_128);
|
||||
|
||||
if(hint & DecoderFormat_CODE_39)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_CODE_39);
|
||||
|
||||
if(hint & DecoderFormat_ITF)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_ITF);
|
||||
|
||||
if(hint & DecoderFormat_Aztec)
|
||||
newHints.addFormat((BarcodeFormat)BarcodeFormat_AZTEC);
|
||||
|
||||
supportedFormats = newHints.getCurrentHint();
|
||||
}
|
||||
|
||||
|
||||
QString QZXing::decodeImage(QImage image)
|
||||
{
|
||||
Ref<Result> result;
|
||||
emit decodingStarted();
|
||||
|
||||
try {
|
||||
Ref<LuminanceSource> source(new CameraImageWrapper(image));
|
||||
|
||||
Ref<Binarizer> binarizer;
|
||||
binarizer = new GlobalHistogramBinarizer(source);
|
||||
|
||||
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
|
||||
|
||||
DecodeHints hints((int)supportedFormats);
|
||||
|
||||
result = ((MultiFormatReader*)decoder)->decode(binary, hints);
|
||||
|
||||
QString string = QString(result->getText()->getText().c_str());
|
||||
emit tagFound(string);
|
||||
emit decodingFinished(true);
|
||||
return string;
|
||||
}
|
||||
catch(zxing::Exception& e)
|
||||
{
|
||||
qDebug() << "[decodeImage()] Exception:" << e.what();
|
||||
emit decodingFinished(false);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
QVariantHash QZXing::decodeImageEx(QImage image)
|
||||
{
|
||||
QVariantHash resultMap;
|
||||
Ref<Result> result;
|
||||
emit decodingStarted();
|
||||
|
||||
try {
|
||||
Ref<LuminanceSource> source(new CameraImageWrapper(image));
|
||||
|
||||
Ref<Binarizer> binarizer;
|
||||
binarizer = new GlobalHistogramBinarizer(source);
|
||||
|
||||
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
|
||||
|
||||
DecodeHints hints((int)supportedFormats);
|
||||
|
||||
result = ((MultiFormatReader*)decoder)->decode(binary, hints);
|
||||
|
||||
QString string = QString(result->getText()->getText().c_str());
|
||||
QList<QVariant> points;
|
||||
emit tagFound(string);
|
||||
emit decodingFinished(true);
|
||||
resultMap.insert("content", string);
|
||||
|
||||
std::vector<Ref<ResultPoint> > resultPoints = result->getResultPoints();
|
||||
for (unsigned int i = 0; i < resultPoints.size(); i++) {
|
||||
points.append(QPoint(resultPoints[i]->getX(), resultPoints[i]->getY()));
|
||||
}
|
||||
resultMap.insert("points", points);
|
||||
}
|
||||
catch(zxing::Exception& e)
|
||||
{
|
||||
qDebug() << "[decodeImage()] Exception:" << e.what();
|
||||
emit decodingFinished(false);
|
||||
resultMap.insert("content", QString(""));
|
||||
resultMap.insert("points", QList<QVariant>());
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
QString QZXing::decodeImageFromFile(QString imageFilePath)
|
||||
{
|
||||
//used to have a check if this image exists
|
||||
//but was removed because if the image file path doesn't point to a valid image
|
||||
// then the QImage::isNull will return true and the decoding will fail eitherway.
|
||||
return decodeImage(QImage(imageFilePath));
|
||||
}
|
||||
|
||||
QString QZXing::decodeImageQML(const QUrl &imageUrl)
|
||||
{
|
||||
return decodeSubImageQML(imageUrl);
|
||||
}
|
||||
|
||||
QString QZXing::decodeSubImageQML(const QUrl &imageUrl,
|
||||
const double offsetX, const double offsetY,
|
||||
const double width, const double height)
|
||||
{
|
||||
|
||||
QString imagePath = imageUrl.path();
|
||||
imagePath = imagePath.trimmed();
|
||||
QFile file(imagePath);
|
||||
|
||||
if (!file.exists()) {
|
||||
qDebug() << "[decodeSubImageQML()] The file" << file.fileName() << "does not exist.";
|
||||
emit decodingFinished(false);
|
||||
return "";
|
||||
}
|
||||
|
||||
QImage img(imageUrl.path());
|
||||
|
||||
if(!(offsetX == 0 && offsetY == 0 && width == 0 && height == 0)) {
|
||||
img = img.copy(offsetX, offsetY, width, height);
|
||||
}
|
||||
|
||||
return decodeImage(img);
|
||||
|
||||
}
|
||||
|
94
src/qzxing/qzxing.h
Normal file
94
src/qzxing/qzxing.h
Normal file
|
@ -0,0 +1,94 @@
|
|||
#ifndef QZXING_H
|
||||
#define QZXING_H
|
||||
|
||||
#include "qzxing_global.h"
|
||||
#include <QObject>
|
||||
#include <QImage>
|
||||
#include <QVariantHash>
|
||||
#include <QtQuick>
|
||||
|
||||
/**
|
||||
* A class containing a very very small subset of the ZXing library.
|
||||
* Created for ease of use.
|
||||
*
|
||||
* Anyone interested in using more technical stuff
|
||||
* from the ZXing library is welcomed to add/edit on free will.
|
||||
*
|
||||
* Regarding DecoderFormat, by default all of those are enabled (except DataMatrix will is still not supported)
|
||||
*/
|
||||
class QZXing : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS(DecoderFormat)
|
||||
|
||||
public:
|
||||
enum DecoderFormat {
|
||||
DecoderFormat_None = 0,
|
||||
DecoderFormat_QR_CODE = 1,
|
||||
DecoderFormat_DATA_MATRIX = 2,
|
||||
DecoderFormat_UPC_E = 4,
|
||||
DecoderFormat_UPC_A = 8,
|
||||
DecoderFormat_EAN_8 = 16,
|
||||
DecoderFormat_EAN_13 = 32,
|
||||
DecoderFormat_CODE_128 = 64,
|
||||
DecoderFormat_CODE_39 = 128,
|
||||
DecoderFormat_ITF = 256,
|
||||
DecoderFormat_Aztec = 512
|
||||
} ;
|
||||
typedef unsigned int DecoderFormatType;
|
||||
|
||||
public:
|
||||
QZXing(QObject *parent = NULL);
|
||||
virtual ~QZXing();
|
||||
|
||||
/**
|
||||
* Set the enabled decoders.
|
||||
* As argument it is possible to pass conjuction of decoders by using logic OR.
|
||||
* e.x. setDecoder ( DecoderFormat_QR_CODE | DecoderFormat_EAN_13 | DecoderFormat_CODE_39 )
|
||||
*/
|
||||
void setDecoder(DecoderFormatType hint);
|
||||
|
||||
#if QT_VERSION >= 0x040700
|
||||
static void registerQMLTypes()
|
||||
{
|
||||
qmlRegisterType<QZXing>("harbour.sailotp.QZXing", 2, 2, "QZXing");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* The decoding function. Will try to decode the given image based on the enabled decoders.
|
||||
*
|
||||
*/
|
||||
QString decodeImage(QImage image);
|
||||
QVariantHash decodeImageEx(QImage image);
|
||||
|
||||
/**
|
||||
* The decoding function accessible from QML
|
||||
*/
|
||||
QString decodeImageQML(const QUrl &imageUrl);
|
||||
|
||||
|
||||
QString decodeImageFromFile(QString imageFilePath);
|
||||
|
||||
/**
|
||||
* The decoding function accessible from QML. Able to set the decoding
|
||||
* of a portion of the image.
|
||||
*/
|
||||
QString decodeSubImageQML(const QUrl &imageUrl,
|
||||
const double offsetX = 0 , const double offsetY = 0,
|
||||
const double width = 0, const double height = 0);
|
||||
|
||||
signals:
|
||||
void decodingStarted();
|
||||
void decodingFinished(bool succeeded);
|
||||
void tagFound(QString tag);
|
||||
|
||||
private:
|
||||
void* decoder;
|
||||
DecoderFormatType supportedFormats;
|
||||
};
|
||||
|
||||
#endif // QZXING_H
|
||||
|
12
src/qzxing/qzxing_global.h
Normal file
12
src/qzxing/qzxing_global.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef QZXING_GLOBAL_H
|
||||
#define QZXING_GLOBAL_H
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if defined(QZXING_LIBRARY)
|
||||
# define QZXINGSHARED_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
# define QZXINGSHARED_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif //QZXING_GLOBAL_H
|
37
src/qzxing/zxing/BarcodeFormat.cpp
Normal file
37
src/qzxing/zxing/BarcodeFormat.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Created by Christian Brunschen on 13/05/2008.
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/BarcodeFormat.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
const char *barcodeFormatNames[] = {
|
||||
"None",
|
||||
"QR_CODE",
|
||||
"DATA_MATRIX",
|
||||
"UPC_E",
|
||||
"UPC_A",
|
||||
"EAN_8",
|
||||
"EAN_13",
|
||||
"CODE_128",
|
||||
"CODE_39",
|
||||
"ITF",
|
||||
"AZTEC"
|
||||
};
|
||||
|
||||
}
|
44
src/qzxing/zxing/BarcodeFormat.h
Normal file
44
src/qzxing/zxing/BarcodeFormat.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __BARCODE_FORMAT_H__
|
||||
#define __BARCODE_FORMAT_H__
|
||||
|
||||
/*
|
||||
* BarcodeFormat.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace zxing {
|
||||
|
||||
typedef enum BarcodeFormat {
|
||||
BarcodeFormat_None = 0,
|
||||
BarcodeFormat_QR_CODE,
|
||||
BarcodeFormat_DATA_MATRIX,
|
||||
BarcodeFormat_UPC_E,
|
||||
BarcodeFormat_UPC_A,
|
||||
BarcodeFormat_EAN_8,
|
||||
BarcodeFormat_EAN_13,
|
||||
BarcodeFormat_CODE_128,
|
||||
BarcodeFormat_CODE_39,
|
||||
BarcodeFormat_ITF,
|
||||
BarcodeFormat_AZTEC
|
||||
} BarcodeFormat;
|
||||
|
||||
/* if you update the enum, please update the name in BarcodeFormat.cpp */
|
||||
extern const char *barcodeFormatNames[];
|
||||
}
|
||||
|
||||
#endif // __BARCODE_FORMAT_H__
|
37
src/qzxing/zxing/Binarizer.cpp
Normal file
37
src/qzxing/zxing/Binarizer.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Binarizer.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Ralf Kistner on 16/10/2009.
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
* Modified by Lukasz Warchol on 02/02/2010.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/Binarizer.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
Binarizer::Binarizer(Ref<LuminanceSource> source) : source_(source) {
|
||||
}
|
||||
|
||||
Binarizer::~Binarizer() {
|
||||
}
|
||||
|
||||
Ref<LuminanceSource> Binarizer::getLuminanceSource() const {
|
||||
return source_;
|
||||
}
|
||||
|
||||
}
|
46
src/qzxing/zxing/Binarizer.h
Normal file
46
src/qzxing/zxing/Binarizer.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
#ifndef BINARIZER_H_
|
||||
#define BINARIZER_H_
|
||||
|
||||
/*
|
||||
* Binarizer.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/LuminanceSource.h>
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/common/BitMatrix.h>
|
||||
#include <zxing/common/Counted.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class Binarizer : public Counted {
|
||||
private:
|
||||
Ref<LuminanceSource> source_;
|
||||
|
||||
public:
|
||||
Binarizer(Ref<LuminanceSource> source);
|
||||
virtual ~Binarizer();
|
||||
|
||||
virtual Ref<BitArray> getBlackRow(int y, Ref<BitArray> row) = 0;
|
||||
virtual Ref<BitMatrix> getBlackMatrix() = 0;
|
||||
|
||||
Ref<LuminanceSource> getLuminanceSource() const ;
|
||||
virtual Ref<Binarizer> createBinarizer(Ref<LuminanceSource> source) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
#endif /* BINARIZER_H_ */
|
67
src/qzxing/zxing/BinaryBitmap.cpp
Normal file
67
src/qzxing/zxing/BinaryBitmap.cpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* BinaryBitmap.cpp
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/BinaryBitmap.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
BinaryBitmap::BinaryBitmap(Ref<Binarizer> binarizer) : binarizer_(binarizer) {
|
||||
|
||||
}
|
||||
|
||||
BinaryBitmap::~BinaryBitmap() {
|
||||
}
|
||||
|
||||
Ref<BitArray> BinaryBitmap::getBlackRow(int y, Ref<BitArray> row) {
|
||||
return binarizer_->getBlackRow(y, row);
|
||||
}
|
||||
|
||||
Ref<BitMatrix> BinaryBitmap::getBlackMatrix() {
|
||||
return binarizer_->getBlackMatrix();
|
||||
}
|
||||
|
||||
int BinaryBitmap::getWidth() const {
|
||||
return getLuminanceSource()->getWidth();
|
||||
}
|
||||
|
||||
int BinaryBitmap::getHeight() const {
|
||||
return getLuminanceSource()->getHeight();
|
||||
}
|
||||
|
||||
Ref<LuminanceSource> BinaryBitmap::getLuminanceSource() const {
|
||||
return binarizer_->getLuminanceSource();
|
||||
}
|
||||
|
||||
|
||||
bool BinaryBitmap::isCropSupported() const {
|
||||
return getLuminanceSource()->isCropSupported();
|
||||
}
|
||||
|
||||
Ref<BinaryBitmap> BinaryBitmap::crop(int left, int top, int width, int height) {
|
||||
return Ref<BinaryBitmap> (new BinaryBitmap(binarizer_->createBinarizer(getLuminanceSource()->crop(left, top, width, height))));
|
||||
}
|
||||
|
||||
bool BinaryBitmap::isRotateSupported() const {
|
||||
return getLuminanceSource()->isRotateSupported();
|
||||
}
|
||||
|
||||
Ref<BinaryBitmap> BinaryBitmap::rotateCounterClockwise() {
|
||||
return Ref<BinaryBitmap> (new BinaryBitmap(binarizer_->createBinarizer(getLuminanceSource()->rotateCounterClockwise())));
|
||||
}
|
||||
}
|
57
src/qzxing/zxing/BinaryBitmap.h
Normal file
57
src/qzxing/zxing/BinaryBitmap.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
#ifndef __BINARYBITMAP_H__
|
||||
#define __BINARYBITMAP_H__
|
||||
|
||||
/*
|
||||
* BinaryBitmap.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <zxing/common/BitMatrix.h>
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/Binarizer.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class BinaryBitmap : public Counted {
|
||||
private:
|
||||
Ref<Binarizer> binarizer_;
|
||||
int cached_y_;
|
||||
|
||||
public:
|
||||
BinaryBitmap(Ref<Binarizer> binarizer);
|
||||
virtual ~BinaryBitmap();
|
||||
|
||||
Ref<BitArray> getBlackRow(int y, Ref<BitArray> row);
|
||||
Ref<BitMatrix> getBlackMatrix();
|
||||
|
||||
Ref<LuminanceSource> getLuminanceSource() const;
|
||||
|
||||
int getWidth() const;
|
||||
int getHeight() const;
|
||||
|
||||
bool isRotateSupported() const;
|
||||
Ref<BinaryBitmap> rotateCounterClockwise();
|
||||
|
||||
bool isCropSupported() const;
|
||||
Ref<BinaryBitmap> crop(int left, int top, int width, int height);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* BINARYBITMAP_H_ */
|
120
src/qzxing/zxing/DecodeHints.cpp
Normal file
120
src/qzxing/zxing/DecodeHints.cpp
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* DecodeHintType.cpp
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/DecodeHints.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
namespace zxing {
|
||||
|
||||
const DecodeHintType DecodeHints::CHARACTER_SET;
|
||||
|
||||
const DecodeHints DecodeHints::PRODUCT_HINT(
|
||||
BARCODEFORMAT_UPC_E_HINT |
|
||||
BARCODEFORMAT_UPC_A_HINT |
|
||||
BARCODEFORMAT_EAN_8_HINT |
|
||||
BARCODEFORMAT_EAN_13_HINT);
|
||||
|
||||
const DecodeHints DecodeHints::ONED_HINT(
|
||||
BARCODEFORMAT_UPC_E_HINT |
|
||||
BARCODEFORMAT_UPC_A_HINT |
|
||||
BARCODEFORMAT_EAN_8_HINT |
|
||||
BARCODEFORMAT_EAN_13_HINT |
|
||||
BARCODEFORMAT_CODE_128_HINT |
|
||||
BARCODEFORMAT_CODE_39_HINT |
|
||||
BARCODEFORMAT_ITF_HINT);
|
||||
|
||||
const DecodeHints DecodeHints::DEFAULT_HINT(
|
||||
BARCODEFORMAT_UPC_E_HINT |
|
||||
BARCODEFORMAT_UPC_A_HINT |
|
||||
BARCODEFORMAT_EAN_8_HINT |
|
||||
BARCODEFORMAT_EAN_13_HINT |
|
||||
BARCODEFORMAT_CODE_128_HINT |
|
||||
BARCODEFORMAT_CODE_39_HINT |
|
||||
BARCODEFORMAT_ITF_HINT |
|
||||
BARCODEFORMAT_DATA_MATRIX_HINT |
|
||||
BARCODEFORMAT_AZTEC_HINT |
|
||||
BARCODEFORMAT_QR_CODE_HINT);
|
||||
|
||||
DecodeHints::DecodeHints() {
|
||||
hints = 0;
|
||||
}
|
||||
|
||||
DecodeHints::DecodeHints(DecodeHintType init) {
|
||||
hints = init;
|
||||
}
|
||||
|
||||
void DecodeHints::addFormat(BarcodeFormat toadd) {
|
||||
switch (toadd) {
|
||||
case BarcodeFormat_AZTEC: hints |= BARCODEFORMAT_AZTEC_HINT; break;
|
||||
case BarcodeFormat_QR_CODE: hints |= BARCODEFORMAT_QR_CODE_HINT; break;
|
||||
case BarcodeFormat_DATA_MATRIX: hints |= BARCODEFORMAT_DATA_MATRIX_HINT; break;
|
||||
case BarcodeFormat_UPC_E: hints |= BARCODEFORMAT_UPC_E_HINT; break;
|
||||
case BarcodeFormat_UPC_A: hints |= BARCODEFORMAT_UPC_A_HINT; break;
|
||||
case BarcodeFormat_EAN_8: hints |= BARCODEFORMAT_EAN_8_HINT; break;
|
||||
case BarcodeFormat_EAN_13: hints |= BARCODEFORMAT_EAN_13_HINT; break;
|
||||
case BarcodeFormat_CODE_128: hints |= BARCODEFORMAT_CODE_128_HINT; break;
|
||||
case BarcodeFormat_CODE_39: hints |= BARCODEFORMAT_CODE_39_HINT; break;
|
||||
case BarcodeFormat_ITF: hints |= BARCODEFORMAT_ITF_HINT; break;
|
||||
default: throw IllegalArgumentException("Unrecognizd barcode format");
|
||||
}
|
||||
}
|
||||
|
||||
bool DecodeHints::containsFormat(BarcodeFormat tocheck) const {
|
||||
DecodeHintType checkAgainst;
|
||||
switch (tocheck) {
|
||||
case BarcodeFormat_AZTEC: checkAgainst = BARCODEFORMAT_AZTEC_HINT; break;
|
||||
case BarcodeFormat_QR_CODE: checkAgainst = BARCODEFORMAT_QR_CODE_HINT; break;
|
||||
case BarcodeFormat_DATA_MATRIX: checkAgainst = BARCODEFORMAT_DATA_MATRIX_HINT; break;
|
||||
case BarcodeFormat_UPC_E: checkAgainst = BARCODEFORMAT_UPC_E_HINT; break;
|
||||
case BarcodeFormat_UPC_A: checkAgainst = BARCODEFORMAT_UPC_A_HINT; break;
|
||||
case BarcodeFormat_EAN_8: checkAgainst = BARCODEFORMAT_EAN_8_HINT; break;
|
||||
case BarcodeFormat_EAN_13: checkAgainst = BARCODEFORMAT_EAN_13_HINT; break;
|
||||
case BarcodeFormat_CODE_128: checkAgainst = BARCODEFORMAT_CODE_128_HINT; break;
|
||||
case BarcodeFormat_CODE_39: checkAgainst = BARCODEFORMAT_CODE_39_HINT; break;
|
||||
case BarcodeFormat_ITF: checkAgainst = BARCODEFORMAT_ITF_HINT; break;
|
||||
default: throw IllegalArgumentException("Unrecognizd barcode format");
|
||||
}
|
||||
return (hints & checkAgainst);
|
||||
}
|
||||
|
||||
void DecodeHints::setTryHarder(bool toset) {
|
||||
if (toset) {
|
||||
hints |= TRYHARDER_HINT;
|
||||
} else {
|
||||
hints &= ~TRYHARDER_HINT;
|
||||
}
|
||||
}
|
||||
|
||||
bool DecodeHints::getTryHarder() const {
|
||||
return (hints & TRYHARDER_HINT);
|
||||
}
|
||||
|
||||
void DecodeHints::setResultPointCallback(Ref<ResultPointCallback> const& _callback) {
|
||||
callback = _callback;
|
||||
}
|
||||
|
||||
Ref<ResultPointCallback> DecodeHints::getResultPointCallback() const {
|
||||
return callback;
|
||||
}
|
||||
|
||||
DecodeHintType DecodeHints::getCurrentHint()
|
||||
{
|
||||
return hints;
|
||||
}
|
||||
|
||||
} /* namespace */
|
72
src/qzxing/zxing/DecodeHints.h
Normal file
72
src/qzxing/zxing/DecodeHints.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
#ifndef __DECODEHINTS_H_
|
||||
#define __DECODEHINTS_H_
|
||||
/*
|
||||
* DecodeHintType.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/BarcodeFormat.h>
|
||||
#include <zxing/ResultPointCallback.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
typedef unsigned int DecodeHintType;
|
||||
|
||||
class DecodeHints {
|
||||
|
||||
private:
|
||||
|
||||
DecodeHintType hints;
|
||||
|
||||
Ref<ResultPointCallback> callback;
|
||||
|
||||
public:
|
||||
|
||||
static const DecodeHintType BARCODEFORMAT_QR_CODE_HINT = 1 << BarcodeFormat_QR_CODE;
|
||||
static const DecodeHintType BARCODEFORMAT_DATA_MATRIX_HINT = 1 << BarcodeFormat_DATA_MATRIX;
|
||||
static const DecodeHintType BARCODEFORMAT_UPC_E_HINT = 1 << BarcodeFormat_UPC_E;
|
||||
static const DecodeHintType BARCODEFORMAT_UPC_A_HINT = 1 << BarcodeFormat_UPC_A;
|
||||
static const DecodeHintType BARCODEFORMAT_EAN_8_HINT = 1 << BarcodeFormat_EAN_8;
|
||||
static const DecodeHintType BARCODEFORMAT_EAN_13_HINT = 1 << BarcodeFormat_EAN_13;
|
||||
static const DecodeHintType BARCODEFORMAT_CODE_128_HINT = 1 << BarcodeFormat_CODE_128;
|
||||
static const DecodeHintType BARCODEFORMAT_CODE_39_HINT = 1 << BarcodeFormat_CODE_39;
|
||||
static const DecodeHintType BARCODEFORMAT_ITF_HINT = 1 << BarcodeFormat_ITF;
|
||||
static const DecodeHintType BARCODEFORMAT_AZTEC_HINT = 1 << BarcodeFormat_AZTEC;
|
||||
static const DecodeHintType CHARACTER_SET = 1 << 30;
|
||||
static const DecodeHintType TRYHARDER_HINT = 1 << 31;
|
||||
|
||||
static const DecodeHints PRODUCT_HINT;
|
||||
static const DecodeHints ONED_HINT;
|
||||
static const DecodeHints DEFAULT_HINT;
|
||||
|
||||
DecodeHints();
|
||||
DecodeHints(DecodeHintType init);
|
||||
|
||||
void addFormat(BarcodeFormat toadd);
|
||||
bool containsFormat(BarcodeFormat tocheck) const;
|
||||
void setTryHarder(bool toset);
|
||||
bool getTryHarder() const;
|
||||
|
||||
void setResultPointCallback(Ref<ResultPointCallback> const&);
|
||||
Ref<ResultPointCallback> getResultPointCallback() const;
|
||||
|
||||
DecodeHintType getCurrentHint();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
40
src/qzxing/zxing/Exception.cpp
Normal file
40
src/qzxing/zxing/Exception.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Exception.cpp
|
||||
* ZXing
|
||||
*
|
||||
* Created by Christian Brunschen on 03/06/2008.
|
||||
* Copyright 2008-2011 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
|
||||
*/
|
||||
|
||||
#include <zxing/Exception.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
Exception::Exception() {}
|
||||
|
||||
Exception::Exception(const char *msg) :
|
||||
message(msg) {
|
||||
}
|
||||
|
||||
const char* Exception::what() const throw() {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
Exception::~Exception() throw() {
|
||||
}
|
||||
|
||||
}
|
40
src/qzxing/zxing/Exception.h
Normal file
40
src/qzxing/zxing/Exception.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#ifndef __EXCEPTION_H__
|
||||
#define __EXCEPTION_H__
|
||||
|
||||
/*
|
||||
* Exception.h
|
||||
* ZXing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class Exception : public std::exception {
|
||||
private:
|
||||
std::string message;
|
||||
|
||||
public:
|
||||
Exception();
|
||||
Exception(const char *msg);
|
||||
virtual const char* what() const throw();
|
||||
virtual ~Exception() throw();
|
||||
};
|
||||
|
||||
}
|
||||
#endif // __EXCEPTION_H__
|
35
src/qzxing/zxing/FormatException.cpp
Normal file
35
src/qzxing/zxing/FormatException.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* FormatException.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 13/05/2008.
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/FormatException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
FormatException::FormatException() {}
|
||||
|
||||
FormatException::FormatException(const char *msg) :
|
||||
ReaderException(msg) {
|
||||
}
|
||||
|
||||
FormatException::~FormatException() throw() {
|
||||
}
|
||||
|
||||
}
|
35
src/qzxing/zxing/FormatException.h
Normal file
35
src/qzxing/zxing/FormatException.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
#ifndef __FORMAT_EXCEPTION_H__
|
||||
#define __FORMAT_EXCEPTION_H__
|
||||
|
||||
/*
|
||||
* FormatException.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/ReaderException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class FormatException : public ReaderException {
|
||||
public:
|
||||
FormatException();
|
||||
FormatException(const char *msg);
|
||||
~FormatException() throw();
|
||||
};
|
||||
|
||||
}
|
||||
#endif // __FORMAT_EXCEPTION_H__
|
80
src/qzxing/zxing/LuminanceSource.cpp
Normal file
80
src/qzxing/zxing/LuminanceSource.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* LuminanceSource.cpp
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <sstream>
|
||||
#include <zxing/LuminanceSource.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
LuminanceSource::LuminanceSource() {
|
||||
}
|
||||
|
||||
LuminanceSource::~LuminanceSource() {
|
||||
}
|
||||
|
||||
bool LuminanceSource::isCropSupported() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
Ref<LuminanceSource> LuminanceSource::crop(int left, int top, int width, int height) {
|
||||
(void)left;
|
||||
(void)top;
|
||||
(void)width;
|
||||
(void)height;
|
||||
throw IllegalArgumentException("This luminance source does not support cropping.");
|
||||
}
|
||||
|
||||
bool LuminanceSource::isRotateSupported() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
Ref<LuminanceSource> LuminanceSource::rotateCounterClockwise() {
|
||||
throw IllegalArgumentException("This luminance source does not support rotation.");
|
||||
}
|
||||
|
||||
LuminanceSource::operator std::string() {
|
||||
unsigned char* row = 0;
|
||||
std::ostringstream oss;
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
row = getRow(y, row);
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
int luminance = row[x] & 0xFF;
|
||||
char c;
|
||||
if (luminance < 0x40) {
|
||||
c = '#';
|
||||
} else if (luminance < 0x80) {
|
||||
c = '+';
|
||||
} else if (luminance < 0xC0) {
|
||||
c = '.';
|
||||
} else {
|
||||
c = ' ';
|
||||
}
|
||||
oss << c;
|
||||
}
|
||||
oss << '\n';
|
||||
}
|
||||
delete [] row;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
52
src/qzxing/zxing/LuminanceSource.h
Normal file
52
src/qzxing/zxing/LuminanceSource.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __LUMINANCESOURCE_H__
|
||||
#define __LUMINANCESOURCE_H__
|
||||
/*
|
||||
* LuminanceSource.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class LuminanceSource : public Counted {
|
||||
public:
|
||||
LuminanceSource();
|
||||
virtual ~LuminanceSource();
|
||||
|
||||
virtual int getWidth() const = 0;
|
||||
virtual int getHeight() const = 0;
|
||||
|
||||
// Callers take ownership of the returned memory and must call delete [] on it themselves.
|
||||
virtual unsigned char* getRow(int y, unsigned char* row) = 0;
|
||||
virtual unsigned char* getMatrix() = 0;
|
||||
|
||||
virtual bool isCropSupported() const;
|
||||
virtual Ref<LuminanceSource> crop(int left, int top, int width, int height);
|
||||
|
||||
virtual bool isRotateSupported() const;
|
||||
virtual Ref<LuminanceSource> rotateCounterClockwise();
|
||||
|
||||
operator std::string (); // should be const but don't want to make sure a
|
||||
// large breaking change right now
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* LUMINANCESOURCE_H_ */
|
106
src/qzxing/zxing/MultiFormatReader.cpp
Normal file
106
src/qzxing/zxing/MultiFormatReader.cpp
Normal file
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* MultiFormatBarcodeReader.cpp
|
||||
* ZXing
|
||||
*
|
||||
* Created by Lukasz Warchol on 10-01-26.
|
||||
* Modified by Luiz Silva on 09/02/2010.
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/MultiFormatReader.h>
|
||||
#include <zxing/qrcode/QRCodeReader.h>
|
||||
#include <zxing/datamatrix/DataMatrixReader.h>
|
||||
#include <zxing/aztec/AztecReader.h>
|
||||
#include <zxing/oned/MultiFormatUPCEANReader.h>
|
||||
#include <zxing/oned/MultiFormatOneDReader.h>
|
||||
#include <zxing/ReaderException.h>
|
||||
|
||||
namespace zxing {
|
||||
MultiFormatReader::MultiFormatReader() {
|
||||
|
||||
}
|
||||
|
||||
Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image) {
|
||||
setHints(DecodeHints::DEFAULT_HINT);
|
||||
return decodeInternal(image);
|
||||
}
|
||||
|
||||
Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
|
||||
setHints(hints);
|
||||
return decodeInternal(image);
|
||||
}
|
||||
|
||||
Ref<Result> MultiFormatReader::decodeWithState(Ref<BinaryBitmap> image) {
|
||||
// Make sure to set up the default state so we don't crash
|
||||
if (readers_.size() == 0) {
|
||||
setHints(DecodeHints::DEFAULT_HINT);
|
||||
}
|
||||
return decodeInternal(image);
|
||||
}
|
||||
|
||||
void MultiFormatReader::setHints(DecodeHints hints) {
|
||||
hints_ = hints;
|
||||
readers_.clear();
|
||||
bool tryHarder = hints.getTryHarder();
|
||||
|
||||
bool addOneDReader = hints.containsFormat(BarcodeFormat_UPC_E) ||
|
||||
hints.containsFormat(BarcodeFormat_UPC_A) ||
|
||||
hints.containsFormat(BarcodeFormat_EAN_8) ||
|
||||
hints.containsFormat(BarcodeFormat_EAN_13) ||
|
||||
hints.containsFormat(BarcodeFormat_CODE_128) ||
|
||||
hints.containsFormat(BarcodeFormat_CODE_39) ||
|
||||
hints.containsFormat(BarcodeFormat_ITF);
|
||||
if (addOneDReader && !tryHarder) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::oned::MultiFormatOneDReader(hints)));
|
||||
}
|
||||
if (hints.containsFormat(BarcodeFormat_QR_CODE)) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::qrcode::QRCodeReader()));
|
||||
}
|
||||
if (hints.containsFormat(BarcodeFormat_DATA_MATRIX)) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::datamatrix::DataMatrixReader()));
|
||||
}
|
||||
if (hints.containsFormat(BarcodeFormat_AZTEC)) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::aztec::AztecReader()));
|
||||
}
|
||||
//TODO: add PDF417 here once PDF417 reader is implemented
|
||||
if (addOneDReader && tryHarder) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::oned::MultiFormatOneDReader(hints)));
|
||||
}
|
||||
if (readers_.size() == 0) {
|
||||
if (!tryHarder) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::oned::MultiFormatOneDReader(hints)));
|
||||
}
|
||||
readers_.push_back(Ref<Reader>(new zxing::qrcode::QRCodeReader()));
|
||||
if (tryHarder) {
|
||||
readers_.push_back(Ref<Reader>(new zxing::oned::MultiFormatOneDReader(hints)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ref<Result> MultiFormatReader::decodeInternal(Ref<BinaryBitmap> image) {
|
||||
for (unsigned int i = 0; i < readers_.size(); i++) {
|
||||
try {
|
||||
return readers_[i]->decode(image, hints_);
|
||||
} catch (ReaderException const& re) {
|
||||
// continue
|
||||
}
|
||||
}
|
||||
throw ReaderException("No code detected");
|
||||
}
|
||||
|
||||
MultiFormatReader::~MultiFormatReader() {
|
||||
|
||||
}
|
||||
}
|
49
src/qzxing/zxing/MultiFormatReader.h
Normal file
49
src/qzxing/zxing/MultiFormatReader.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
#ifndef __MULTI_FORMAT_READER_H__
|
||||
#define __MULTI_FORMAT_READER_H__
|
||||
|
||||
/*
|
||||
* MultiFormatBarcodeReader.h
|
||||
* ZXing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#include <zxing/Reader.h>
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/Result.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
class MultiFormatReader : public Reader {
|
||||
|
||||
private:
|
||||
Ref<Result> decodeInternal(Ref<BinaryBitmap> image);
|
||||
|
||||
std::vector<Ref<Reader> > readers_;
|
||||
DecodeHints hints_;
|
||||
|
||||
public:
|
||||
MultiFormatReader();
|
||||
|
||||
Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
Ref<Result> decodeWithState(Ref<BinaryBitmap> image);
|
||||
void setHints(DecodeHints hints);
|
||||
~MultiFormatReader();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
28
src/qzxing/zxing/NotFoundException.cpp
Normal file
28
src/qzxing/zxing/NotFoundException.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Copyright 20011 ZXing authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/NotFoundException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
NotFoundException::NotFoundException(const char *msg)
|
||||
: ReaderException(msg) {}
|
||||
|
||||
NotFoundException::~NotFoundException() throw() {
|
||||
}
|
||||
|
||||
}
|
33
src/qzxing/zxing/NotFoundException.h
Normal file
33
src/qzxing/zxing/NotFoundException.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
|
||||
#ifndef __NOT_FOUND_EXCEPTION_H__
|
||||
#define __NOT_FOUND_EXCEPTION_H__
|
||||
|
||||
/*
|
||||
* Copyright 20011 ZXing authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/ReaderException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class NotFoundException : public ReaderException {
|
||||
public:
|
||||
NotFoundException(const char *msg);
|
||||
~NotFoundException() throw();
|
||||
};
|
||||
|
||||
}
|
||||
#endif // __NOT_FOUND_EXCEPTION_H__
|
31
src/qzxing/zxing/Reader.cpp
Normal file
31
src/qzxing/zxing/Reader.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Reader.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 13/05/2008.
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/Reader.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
Reader::~Reader() { }
|
||||
|
||||
Ref<Result> Reader::decode(Ref<BinaryBitmap> image) {
|
||||
return decode(image, DecodeHints::DEFAULT_HINT);
|
||||
}
|
||||
|
||||
}
|
40
src/qzxing/zxing/Reader.h
Normal file
40
src/qzxing/zxing/Reader.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#ifndef __READER_H__
|
||||
#define __READER_H__
|
||||
|
||||
/*
|
||||
* Reader.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/BinaryBitmap.h>
|
||||
#include <zxing/Result.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class Reader : public Counted {
|
||||
protected:
|
||||
Reader() {}
|
||||
public:
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints) = 0;
|
||||
virtual ~Reader();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __READER_H__
|
35
src/qzxing/zxing/ReaderException.cpp
Normal file
35
src/qzxing/zxing/ReaderException.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* ReaderException.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 13/05/2008.
|
||||
* Copyright 2008-2011 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/ReaderException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
ReaderException::ReaderException() {}
|
||||
|
||||
ReaderException::ReaderException(const char *msg) :
|
||||
Exception(msg) {
|
||||
}
|
||||
|
||||
ReaderException::~ReaderException() throw() {
|
||||
}
|
||||
|
||||
}
|
35
src/qzxing/zxing/ReaderException.h
Normal file
35
src/qzxing/zxing/ReaderException.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
#ifndef __READER_EXCEPTION_H__
|
||||
#define __READER_EXCEPTION_H__
|
||||
|
||||
/*
|
||||
* ReaderException.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/Exception.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class ReaderException : public Exception {
|
||||
public:
|
||||
ReaderException();
|
||||
ReaderException(const char *msg);
|
||||
~ReaderException() throw();
|
||||
};
|
||||
|
||||
}
|
||||
#endif // __READER_EXCEPTION_H__
|
64
src/qzxing/zxing/Result.cpp
Normal file
64
src/qzxing/zxing/Result.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Result.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 13/05/2008.
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/Result.h>
|
||||
|
||||
namespace zxing {
|
||||
using namespace std;
|
||||
|
||||
Result::Result(Ref<String> text, ArrayRef<unsigned char> rawBytes, std::vector<Ref<ResultPoint> > resultPoints,
|
||||
BarcodeFormat format) :
|
||||
text_(text), rawBytes_(rawBytes), resultPoints_(resultPoints), format_(format) {
|
||||
}
|
||||
|
||||
Result::~Result() {
|
||||
}
|
||||
|
||||
Ref<String> Result::getText() {
|
||||
return text_;
|
||||
}
|
||||
|
||||
ArrayRef<unsigned char> Result::getRawBytes() {
|
||||
return rawBytes_;
|
||||
}
|
||||
|
||||
const std::vector<Ref<ResultPoint> >& Result::getResultPoints() const {
|
||||
return resultPoints_;
|
||||
}
|
||||
|
||||
std::vector<Ref<ResultPoint> >& Result::getResultPoints() {
|
||||
return resultPoints_;
|
||||
}
|
||||
|
||||
BarcodeFormat Result::getBarcodeFormat() const {
|
||||
return format_;
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream &out, Result& result) {
|
||||
if (result.text_ != 0) {
|
||||
out << result.text_->getText();
|
||||
} else {
|
||||
out << "[" << result.rawBytes_->size() << " bytes]";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
54
src/qzxing/zxing/Result.h
Normal file
54
src/qzxing/zxing/Result.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
#ifndef __RESULT_H__
|
||||
#define __RESULT_H__
|
||||
|
||||
/*
|
||||
* Result.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <zxing/common/Array.h>
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <zxing/common/Str.h>
|
||||
#include <zxing/ResultPoint.h>
|
||||
#include <zxing/BarcodeFormat.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class Result : public Counted {
|
||||
private:
|
||||
Ref<String> text_;
|
||||
ArrayRef<unsigned char> rawBytes_;
|
||||
std::vector<Ref<ResultPoint> > resultPoints_;
|
||||
BarcodeFormat format_;
|
||||
|
||||
public:
|
||||
Result(Ref<String> text, ArrayRef<unsigned char> rawBytes, std::vector<Ref<ResultPoint> > resultPoints,
|
||||
BarcodeFormat format);
|
||||
~Result();
|
||||
Ref<String> getText();
|
||||
ArrayRef<unsigned char> getRawBytes();
|
||||
const std::vector<Ref<ResultPoint> >& getResultPoints() const;
|
||||
std::vector<Ref<ResultPoint> >& getResultPoints();
|
||||
BarcodeFormat getBarcodeFormat() const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream &out, Result& result);
|
||||
};
|
||||
|
||||
}
|
||||
#endif // __RESULT_H__
|
100
src/qzxing/zxing/ResultPoint.cpp
Normal file
100
src/qzxing/zxing/ResultPoint.cpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* ResultPoint.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 13/05/2008.
|
||||
* Copyright 2008 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/ResultPoint.h>
|
||||
#include <math.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
ResultPoint::ResultPoint() : posX_(0), posY_(0) {}
|
||||
|
||||
ResultPoint::ResultPoint(float x, float y) : posX_(x), posY_(y) {}
|
||||
|
||||
ResultPoint::~ResultPoint() {}
|
||||
|
||||
float ResultPoint::getX() const {
|
||||
return posX_;
|
||||
}
|
||||
|
||||
float ResultPoint::getY() const {
|
||||
return posY_;
|
||||
}
|
||||
|
||||
bool ResultPoint::equals(Ref<ResultPoint> other) {
|
||||
return posX_ == other->getX() && posY_ == other->getY();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Orders an array of three ResultPoints in an order [A,B,C] such that AB < AC and
|
||||
* BC < AC and the angle between BC and BA is less than 180 degrees.
|
||||
*/
|
||||
void ResultPoint::orderBestPatterns(std::vector<Ref<ResultPoint> > &patterns) {
|
||||
// Find distances between pattern centers
|
||||
float zeroOneDistance = distance(patterns[0]->getX(), patterns[1]->getX(),patterns[0]->getY(), patterns[1]->getY());
|
||||
float oneTwoDistance = distance(patterns[1]->getX(), patterns[2]->getX(),patterns[1]->getY(), patterns[2]->getY());
|
||||
float zeroTwoDistance = distance(patterns[0]->getX(), patterns[2]->getX(),patterns[0]->getY(), patterns[2]->getY());
|
||||
|
||||
Ref<ResultPoint> pointA, pointB, pointC;
|
||||
// Assume one closest to other two is B; A and C will just be guesses at first
|
||||
if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) {
|
||||
pointB = patterns[0];
|
||||
pointA = patterns[1];
|
||||
pointC = patterns[2];
|
||||
} else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) {
|
||||
pointB = patterns[1];
|
||||
pointA = patterns[0];
|
||||
pointC = patterns[2];
|
||||
} else {
|
||||
pointB = patterns[2];
|
||||
pointA = patterns[0];
|
||||
pointC = patterns[1];
|
||||
}
|
||||
|
||||
// Use cross product to figure out whether A and C are correct or flipped.
|
||||
// This asks whether BC x BA has a positive z component, which is the arrangement
|
||||
// we want for A, B, C. If it's negative, then we've got it flipped around and
|
||||
// should swap A and C.
|
||||
if (crossProductZ(pointA, pointB, pointC) < 0.0f) {
|
||||
Ref<ResultPoint> temp = pointA;
|
||||
pointA = pointC;
|
||||
pointC = temp;
|
||||
}
|
||||
|
||||
patterns[0] = pointA;
|
||||
patterns[1] = pointB;
|
||||
patterns[2] = pointC;
|
||||
}
|
||||
|
||||
float ResultPoint::distance(Ref<ResultPoint> point1, Ref<ResultPoint> point2) {
|
||||
return distance(point1->getX(), point1->getY(), point2->getX(), point2->getY());
|
||||
}
|
||||
|
||||
float ResultPoint::distance(float x1, float x2, float y1, float y2) {
|
||||
float xDiff = x1 - x2;
|
||||
float yDiff = y1 - y2;
|
||||
return (float) sqrt((double) (xDiff * xDiff + yDiff * yDiff));
|
||||
}
|
||||
|
||||
float ResultPoint::crossProductZ(Ref<ResultPoint> pointA, Ref<ResultPoint> pointB, Ref<ResultPoint> pointC) {
|
||||
float bX = pointB->getX();
|
||||
float bY = pointB->getY();
|
||||
return ((pointC->getX() - bX) * (pointA->getY() - bY)) - ((pointC->getY() - bY) * (pointA->getX() - bX));
|
||||
}
|
||||
}
|
54
src/qzxing/zxing/ResultPoint.h
Normal file
54
src/qzxing/zxing/ResultPoint.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __RESULT_POINT_H__
|
||||
#define __RESULT_POINT_H__
|
||||
|
||||
/*
|
||||
* ResultPoint.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <vector>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class ResultPoint : public Counted {
|
||||
protected:
|
||||
float posX_;
|
||||
float posY_;
|
||||
|
||||
public:
|
||||
ResultPoint();
|
||||
ResultPoint(float x, float y);
|
||||
virtual ~ResultPoint();
|
||||
|
||||
virtual float getX() const;
|
||||
virtual float getY() const;
|
||||
|
||||
bool equals(Ref<ResultPoint> other);
|
||||
|
||||
static void orderBestPatterns(std::vector<Ref<ResultPoint> > &patterns);
|
||||
static float distance(Ref<ResultPoint> point1, Ref<ResultPoint> point2);
|
||||
static float distance(float x1, float x2, float y1, float y2);
|
||||
|
||||
private:
|
||||
static float crossProductZ(Ref<ResultPoint> pointA, Ref<ResultPoint> pointB, Ref<ResultPoint> pointC);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __RESULT_POINT_H__
|
26
src/qzxing/zxing/ResultPointCallback.cpp
Normal file
26
src/qzxing/zxing/ResultPointCallback.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* ResultPointCallback.cpp
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/ResultPointCallback.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
ResultPointCallback::~ResultPointCallback() {}
|
||||
|
||||
}
|
39
src/qzxing/zxing/ResultPointCallback.h
Normal file
39
src/qzxing/zxing/ResultPointCallback.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
#ifndef __RESULT_POINT_CALLBACK_H__
|
||||
#define __RESULT_POINT_CALLBACK_H__
|
||||
|
||||
/*
|
||||
* ResultPointCallback.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class ResultPoint;
|
||||
|
||||
class ResultPointCallback : public Counted {
|
||||
protected:
|
||||
ResultPointCallback() {}
|
||||
public:
|
||||
virtual void foundPossibleResultPoint(ResultPoint const& point) = 0;
|
||||
virtual ~ResultPointCallback();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __RESULT_POINT_CALLBACK_H__
|
45
src/qzxing/zxing/aztec/AztecDetectorResult.cpp
Normal file
45
src/qzxing/zxing/aztec/AztecDetectorResult.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* AtztecDetecorResult.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/aztec/AztecDetectorResult.h>
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
AztecDetectorResult::AztecDetectorResult(Ref<BitMatrix> bits, std::vector<Ref<ResultPoint> > points, bool compact, int nbDatablocks, int nbLayers)
|
||||
: DetectorResult(bits, points),
|
||||
compact_(compact),
|
||||
nbDatablocks_(nbDatablocks),
|
||||
nbLayers_(nbLayers) {
|
||||
}
|
||||
|
||||
bool AztecDetectorResult::isCompact() {
|
||||
return compact_;
|
||||
}
|
||||
|
||||
int AztecDetectorResult::getNBDatablocks() {
|
||||
return nbDatablocks_;
|
||||
}
|
||||
|
||||
int AztecDetectorResult::getNBLayers() {
|
||||
return nbLayers_;
|
||||
}
|
||||
}
|
||||
}
|
42
src/qzxing/zxing/aztec/AztecDetectorResult.h
Normal file
42
src/qzxing/zxing/aztec/AztecDetectorResult.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* AtztecDetecorResult.h
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/DetectorResult.h>
|
||||
|
||||
#ifndef ZXingWidget_AtztecDetecorResult_h
|
||||
#define ZXingWidget_AtztecDetecorResult_h
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
class AztecDetectorResult : public DetectorResult {
|
||||
private:
|
||||
bool compact_;
|
||||
int nbDatablocks_, nbLayers_;
|
||||
public:
|
||||
AztecDetectorResult(Ref<BitMatrix> bits, std::vector<Ref<ResultPoint> > points, bool compact, int nbDatablocks, int nbLayers);
|
||||
bool isCompact();
|
||||
int getNBDatablocks();
|
||||
int getNBLayers();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
64
src/qzxing/zxing/aztec/AztecReader.cpp
Normal file
64
src/qzxing/zxing/aztec/AztecReader.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* AztecReader.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/aztec/AztecReader.h>
|
||||
#include <zxing/aztec/detector/Detector.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
|
||||
AztecReader::AztecReader() : decoder_() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
Ref<Result> AztecReader::decode(Ref<zxing::BinaryBitmap> image) {
|
||||
Detector detector(image->getBlackMatrix());
|
||||
|
||||
Ref<AztecDetectorResult> detectorResult(detector.detect());
|
||||
|
||||
std::vector<Ref<ResultPoint> > points(detectorResult->getPoints());
|
||||
|
||||
Ref<DecoderResult> decoderResult(decoder_.decode(detectorResult));
|
||||
|
||||
Ref<Result> result(new Result(decoderResult->getText(),
|
||||
decoderResult->getRawBytes(),
|
||||
points,
|
||||
BarcodeFormat_AZTEC));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Ref<Result> AztecReader::decode(Ref<BinaryBitmap> image, DecodeHints) {
|
||||
//cout << "decoding with hints not supported for aztec" << "\n" << flush;
|
||||
return this->decode(image);
|
||||
}
|
||||
|
||||
AztecReader::~AztecReader() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
Decoder& AztecReader::getDecoder() {
|
||||
return decoder_;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
49
src/qzxing/zxing/aztec/AztecReader.h
Normal file
49
src/qzxing/zxing/aztec/AztecReader.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* AztecReader.h
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/Reader.h>
|
||||
#include <zxing/aztec/decoder/Decoder.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
#ifndef ZXingWidget_AztecReader_h
|
||||
#define ZXingWidget_AztecReader_h
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
|
||||
class AztecReader : public Reader {
|
||||
private:
|
||||
Decoder decoder_;
|
||||
|
||||
protected:
|
||||
Decoder &getDecoder();
|
||||
|
||||
public:
|
||||
AztecReader();
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
virtual ~AztecReader();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
496
src/qzxing/zxing/aztec/decoder/AztecDecoder.cpp
Normal file
496
src/qzxing/zxing/aztec/decoder/AztecDecoder.cpp
Normal file
|
@ -0,0 +1,496 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Decoder.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/aztec/decoder/Decoder.h>
|
||||
#ifndef NO_ICONV
|
||||
#include <iconv.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <zxing/FormatException.h>
|
||||
#include <zxing/common/reedsolomon/ReedSolomonDecoder.h>
|
||||
#include <zxing/common/reedsolomon/ReedSolomonException.h>
|
||||
#include <zxing/common/reedsolomon/GenericGF.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
|
||||
#include <qglobal.h>
|
||||
|
||||
using zxing::aztec::Decoder;
|
||||
using zxing::DecoderResult;
|
||||
using zxing::String;
|
||||
using zxing::BitArray;
|
||||
using zxing::BitMatrix;
|
||||
using zxing::Ref;
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace {
|
||||
void add(string& result, unsigned char character) {
|
||||
#ifndef NO_ICONV
|
||||
char s[] = { character & 0xff };
|
||||
char* ss = s;
|
||||
size_t sl = sizeof(s);
|
||||
char d[4];
|
||||
char* ds = d;
|
||||
size_t dl = sizeof(d);
|
||||
iconv_t ic = iconv_open("UTF-8", "ISO-8859-1");
|
||||
|
||||
|
||||
#if defined(Q_OS_SYMBIAN)
|
||||
iconv(ic, (const char**)&ss, &sl, &ds, &dl); // for Symbian and Mingw
|
||||
#else
|
||||
iconv(ic, (char**)&ss, &sl, &ds, &dl); // for Harmattan
|
||||
#endif
|
||||
iconv_close(ic);
|
||||
d[sizeof(d)-dl] = 0;
|
||||
result.append(d);
|
||||
#else
|
||||
result.push_back(character);
|
||||
#endif
|
||||
}
|
||||
|
||||
const int NB_BITS_COMPACT[] = {
|
||||
0, 104, 240, 408, 608
|
||||
};
|
||||
|
||||
const int NB_BITS[] = {
|
||||
0, 128, 288, 480, 704, 960, 1248, 1568, 1920, 2304, 2720, 3168, 3648, 4160, 4704, 5280, 5888, 6528,
|
||||
7200, 7904, 8640, 9408, 10208, 11040, 11904, 12800, 13728, 14688, 15680, 16704, 17760, 18848, 19968
|
||||
};
|
||||
|
||||
const int NB_DATABLOCK_COMPACT[] = {
|
||||
0, 17, 40, 51, 76
|
||||
};
|
||||
|
||||
const int NB_DATABLOCK[] = {
|
||||
0, 21, 48, 60, 88, 120, 156, 196, 240, 230, 272, 316, 364, 416, 470, 528, 588, 652, 720, 790, 864,
|
||||
940, 1020, 920, 992, 1066, 1144, 1224, 1306, 1392, 1480, 1570, 1664
|
||||
};
|
||||
|
||||
const char* UPPER_TABLE[] = {
|
||||
"CTRL_PS", " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
|
||||
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "CTRL_LL", "CTRL_ML", "CTRL_DL", "CTRL_BS"
|
||||
};
|
||||
|
||||
const char* LOWER_TABLE[] = {
|
||||
"CTRL_PS", " ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
|
||||
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "CTRL_US", "CTRL_ML", "CTRL_DL", "CTRL_BS"
|
||||
};
|
||||
|
||||
const char* MIXED_TABLE[] = {
|
||||
"CTRL_PS", " ", "\1", "\2", "\3", "\4", "\5", "\6", "\7", "\b", "\t", "\n",
|
||||
"\13", "\f", "\r", "\33", "\34", "\35", "\36", "\37", "@", "\\", "^", "_",
|
||||
"`", "|", "~", "\177", "CTRL_LL", "CTRL_UL", "CTRL_PL", "CTRL_BS"
|
||||
};
|
||||
|
||||
const char* PUNCT_TABLE[] = {
|
||||
"", "\r", "\r\n", ". ", ", ", ": ", "!", "\"", "#", "$", "%", "&", "'", "(", ")",
|
||||
"*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "[", "]", "{", "}", "CTRL_UL"
|
||||
};
|
||||
|
||||
const char* DIGIT_TABLE[] = {
|
||||
"CTRL_PS", " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", ".", "CTRL_UL", "CTRL_US"
|
||||
};
|
||||
}
|
||||
|
||||
Decoder::Table Decoder::getTable(char t) {
|
||||
switch (t) {
|
||||
case 'L':
|
||||
return LOWER;
|
||||
case 'P':
|
||||
return PUNCT;
|
||||
case 'M':
|
||||
return MIXED;
|
||||
case 'D':
|
||||
return DIGIT;
|
||||
case 'B':
|
||||
return BINARY;
|
||||
case 'U':
|
||||
default:
|
||||
return UPPER;
|
||||
}
|
||||
}
|
||||
|
||||
const char* Decoder::getCharacter(zxing::aztec::Decoder::Table table, int code) {
|
||||
switch (table) {
|
||||
case UPPER:
|
||||
return UPPER_TABLE[code];
|
||||
case LOWER:
|
||||
return LOWER_TABLE[code];
|
||||
case MIXED:
|
||||
return MIXED_TABLE[code];
|
||||
case PUNCT:
|
||||
return PUNCT_TABLE[code];
|
||||
case DIGIT:
|
||||
return DIGIT_TABLE[code];
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Decoder::Decoder() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
Ref<DecoderResult> Decoder::decode(Ref<zxing::aztec::AztecDetectorResult> detectorResult) {
|
||||
ddata_ = detectorResult;
|
||||
|
||||
// std::printf("getting bits\n");
|
||||
|
||||
Ref<BitMatrix> matrix = detectorResult->getBits();
|
||||
|
||||
if (!ddata_->isCompact()) {
|
||||
// std::printf("removing lines\n");
|
||||
matrix = removeDashedLines(ddata_->getBits());
|
||||
}
|
||||
|
||||
// std::printf("extracting bits\n");
|
||||
Ref<BitArray> rawbits = extractBits(matrix);
|
||||
|
||||
// std::printf("correcting bits\n");
|
||||
Ref<BitArray> aCorrectedBits = correctBits(rawbits);
|
||||
|
||||
// std::printf("decoding bits\n");
|
||||
Ref<String> result = getEncodedData(aCorrectedBits);
|
||||
|
||||
// std::printf("constructing array\n");
|
||||
ArrayRef<unsigned char> arrayOut(aCorrectedBits->getSize());
|
||||
for (int i = 0; i < aCorrectedBits->count(); i++) {
|
||||
arrayOut[i] = (unsigned char)aCorrectedBits->get(i);
|
||||
}
|
||||
|
||||
// std::printf("returning\n");
|
||||
|
||||
return Ref<DecoderResult>(new DecoderResult(arrayOut, result));
|
||||
}
|
||||
|
||||
Ref<String> Decoder::getEncodedData(Ref<zxing::BitArray> correctedBits) {
|
||||
int endIndex = codewordSize_ * ddata_->getNBDatablocks() - invertedBitCount_;
|
||||
if (endIndex > (int)correctedBits->getSize()) {
|
||||
// std::printf("invalid input\n");
|
||||
throw FormatException("invalid input data");
|
||||
}
|
||||
|
||||
Table lastTable = UPPER;
|
||||
Table table = UPPER;
|
||||
int startIndex = 0;
|
||||
std::string result;
|
||||
bool end = false;
|
||||
bool shift = false;
|
||||
bool switchShift = false;
|
||||
bool binaryShift = false;
|
||||
|
||||
while (!end) {
|
||||
// std::printf("decoooooding\n");
|
||||
|
||||
if (shift) {
|
||||
switchShift = true;
|
||||
} else {
|
||||
lastTable = table;
|
||||
}
|
||||
|
||||
int code;
|
||||
if (binaryShift) {
|
||||
if (endIndex - startIndex < 5) {
|
||||
break;
|
||||
}
|
||||
|
||||
int length = readCode(correctedBits, startIndex, 5);
|
||||
startIndex += 5;
|
||||
if (length == 0) {
|
||||
if (endIndex - startIndex < 11) {
|
||||
break;
|
||||
}
|
||||
|
||||
length = readCode(correctedBits, startIndex, 11) + 31;
|
||||
startIndex += 11;
|
||||
}
|
||||
for (int charCount = 0; charCount < length; charCount++) {
|
||||
if (endIndex - startIndex < 8) {
|
||||
end = true;
|
||||
break;
|
||||
}
|
||||
|
||||
code = readCode(correctedBits, startIndex, 8);
|
||||
add(result, code);
|
||||
startIndex += 8;
|
||||
}
|
||||
binaryShift = false;
|
||||
} else {
|
||||
if (table == BINARY) {
|
||||
if (endIndex - startIndex < 8) {
|
||||
end = true;
|
||||
break;
|
||||
}
|
||||
code = readCode(correctedBits, startIndex, 8);
|
||||
startIndex += 8;
|
||||
|
||||
add(result, code);
|
||||
} else {
|
||||
int size = 5;
|
||||
|
||||
if (table == DIGIT) {
|
||||
size = 4;
|
||||
}
|
||||
|
||||
if (endIndex - startIndex < size) {
|
||||
end = true;
|
||||
break;
|
||||
}
|
||||
|
||||
code = readCode(correctedBits, startIndex, size);
|
||||
startIndex += size;
|
||||
|
||||
const char *str = getCharacter(table, code);
|
||||
std::string string(str);
|
||||
if ((int)string.find("CTRL_") != -1) {
|
||||
table = getTable(str[5]);
|
||||
|
||||
if (str[6] == 'S') {
|
||||
shift = true;
|
||||
if (str[5] == 'B') {
|
||||
binaryShift = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result.append(string);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (switchShift) {
|
||||
table = lastTable;
|
||||
shift = false;
|
||||
switchShift = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return Ref<String>(new String(result));
|
||||
|
||||
}
|
||||
|
||||
Ref<zxing::BitArray> Decoder::correctBits(Ref<zxing::BitArray> rawbits) {
|
||||
//return rawbits;
|
||||
// std::printf("decoding stuff:%d datablocks in %d layers\n", ddata_->getNBDatablocks(), ddata_->getNBLayers());
|
||||
|
||||
Ref<GenericGF> gf = GenericGF::AZTEC_DATA_6;
|
||||
|
||||
if (ddata_->getNBLayers() <= 2) {
|
||||
codewordSize_ = 6;
|
||||
gf = GenericGF::AZTEC_DATA_6;
|
||||
} else if (ddata_->getNBLayers() <= 8) {
|
||||
codewordSize_ = 8;
|
||||
gf = GenericGF::AZTEC_DATA_8;
|
||||
} else if (ddata_->getNBLayers() <= 22) {
|
||||
codewordSize_ = 10;
|
||||
gf = GenericGF::AZTEC_DATA_10;
|
||||
} else {
|
||||
codewordSize_ = 12;
|
||||
gf = GenericGF::AZTEC_DATA_12;
|
||||
}
|
||||
|
||||
int numDataCodewords = ddata_->getNBDatablocks();
|
||||
int numECCodewords;
|
||||
int offset;
|
||||
|
||||
if (ddata_->isCompact()) {
|
||||
offset = NB_BITS_COMPACT[ddata_->getNBLayers()] - numCodewords_ * codewordSize_;
|
||||
numECCodewords = NB_DATABLOCK_COMPACT[ddata_->getNBLayers()] - numDataCodewords;
|
||||
} else {
|
||||
offset = NB_BITS[ddata_->getNBLayers()] - numCodewords_ * codewordSize_;
|
||||
numECCodewords = NB_DATABLOCK[ddata_->getNBLayers()] - numDataCodewords;
|
||||
}
|
||||
|
||||
ArrayRef<int> dataWords(numCodewords_);
|
||||
|
||||
for (int i = 0; i < numCodewords_; i++) {
|
||||
int flag = 1;
|
||||
for (int j = 1; j <= codewordSize_; j++) {
|
||||
if (rawbits->get(codewordSize_ * i + codewordSize_ - j + offset)) {
|
||||
dataWords[i] += flag;
|
||||
}
|
||||
flag <<= 1;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
}
|
||||
|
||||
try {
|
||||
// std::printf("trying reed solomon, numECCodewords:%d\n", numECCodewords);
|
||||
ReedSolomonDecoder rsDecoder(gf);
|
||||
rsDecoder.decode(dataWords, numECCodewords);
|
||||
} catch (ReedSolomonException& rse) {
|
||||
// std::printf("got reed solomon exception:%s, throwing formatexception\n", rse.what());
|
||||
throw FormatException("rs decoding failed");
|
||||
} catch (IllegalArgumentException& iae) {
|
||||
// std::printf("illegal argument exception: %s", iae.what());
|
||||
}
|
||||
|
||||
offset = 0;
|
||||
invertedBitCount_ = 0;
|
||||
|
||||
Ref<BitArray> correctedBits(new BitArray(numDataCodewords * codewordSize_));
|
||||
for (int i = 0; i < numDataCodewords; i++) {
|
||||
|
||||
bool seriesColor = false;
|
||||
int seriesCount = 0;
|
||||
int flag = 1 << (codewordSize_ - 1);
|
||||
|
||||
for (int j = 0; j < codewordSize_; j++) {
|
||||
|
||||
bool color = (dataWords[i] & flag) == flag;
|
||||
|
||||
if (seriesCount == codewordSize_ - 1) {
|
||||
|
||||
if (color == seriesColor) {
|
||||
throw FormatException("bit was not inverted");
|
||||
}
|
||||
|
||||
seriesColor = false;
|
||||
seriesCount = 0;
|
||||
offset++;
|
||||
invertedBitCount_++;
|
||||
|
||||
} else {
|
||||
|
||||
if (seriesColor == color) {
|
||||
seriesCount++;
|
||||
} else {
|
||||
seriesCount = 1;
|
||||
seriesColor = color;
|
||||
}
|
||||
|
||||
if (color) correctedBits->set(i * codewordSize_ + j - offset);
|
||||
|
||||
}
|
||||
|
||||
flag = (unsigned int)flag >> 1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return correctedBits;
|
||||
}
|
||||
|
||||
Ref<BitArray> Decoder::extractBits(Ref<zxing::BitMatrix> matrix) {
|
||||
std::vector<bool> rawbits;
|
||||
|
||||
if (ddata_->isCompact()) {
|
||||
if (ddata_->getNBLayers() > 5) { //NB_BITS_COMPACT length
|
||||
throw FormatException("data is too long");
|
||||
}
|
||||
rawbits = std::vector<bool>(NB_BITS_COMPACT[ddata_->getNBLayers()]);
|
||||
numCodewords_ = NB_DATABLOCK_COMPACT[ddata_->getNBLayers()];
|
||||
} else {
|
||||
if (ddata_->getNBLayers() > 33) { //NB_BITS length
|
||||
throw FormatException("data is too long");
|
||||
}
|
||||
rawbits = std::vector<bool>(NB_BITS[ddata_->getNBLayers()]);
|
||||
numCodewords_ = NB_DATABLOCK[ddata_->getNBLayers()];
|
||||
}
|
||||
|
||||
int layer = ddata_->getNBLayers();
|
||||
int size = matrix->getHeight();
|
||||
int rawbitsOffset = 0;
|
||||
int matrixOffset = 0;
|
||||
|
||||
|
||||
while (layer != 0) {
|
||||
|
||||
int flip = 0;
|
||||
for (int i = 0; i < 2 * size - 4; i++) {
|
||||
rawbits[rawbitsOffset + i] = matrix->get(matrixOffset + flip, matrixOffset + i / 2);
|
||||
rawbits[rawbitsOffset + 2 * size - 4 + i] = matrix->get(matrixOffset + i / 2, matrixOffset + size - 1 - flip);
|
||||
flip = (flip + 1) % 2;
|
||||
}
|
||||
|
||||
flip = 0;
|
||||
for (int i = 2 * size + 1; i > 5; i--) {
|
||||
rawbits[rawbitsOffset + 4 * size - 8 + (2 * size - i) + 1] =
|
||||
matrix->get(matrixOffset + size - 1 - flip, matrixOffset + i / 2 - 1);
|
||||
rawbits[rawbitsOffset + 6 * size - 12 + (2 * size - i) + 1] =
|
||||
matrix->get(matrixOffset + i / 2 - 1, matrixOffset + flip);
|
||||
flip = (flip + 1) % 2;
|
||||
}
|
||||
|
||||
matrixOffset += 2;
|
||||
rawbitsOffset += 8 * size - 16;
|
||||
layer--;
|
||||
size -= 4;
|
||||
|
||||
}
|
||||
|
||||
Ref<BitArray> returnValue(new BitArray(rawbits.size()));
|
||||
for (int i = 0; i < (int)rawbits.size(); i++) {
|
||||
if (rawbits[i]) returnValue->set(i);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
|
||||
}
|
||||
|
||||
Ref<BitMatrix> Decoder::removeDashedLines(Ref<zxing::BitMatrix> matrix) {
|
||||
int nbDashed = 1 + 2 * ((matrix->getWidth() - 1) / 2 / 16);
|
||||
Ref<BitMatrix> newMatrix(new BitMatrix(matrix->getWidth() - nbDashed, matrix->getHeight() - nbDashed));
|
||||
|
||||
int nx = 0;
|
||||
|
||||
for (int x = 0; x < (int)matrix->getWidth(); x++) {
|
||||
|
||||
if ((matrix->getWidth() / 2 - x) % 16 == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int ny = 0;
|
||||
for (int y = 0; y < (int)matrix->getHeight(); y++) {
|
||||
|
||||
if ((matrix->getWidth() / 2 - y) % 16 == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (matrix->get(x, y)) {
|
||||
newMatrix->set(nx, ny);
|
||||
}
|
||||
ny++;
|
||||
|
||||
}
|
||||
nx++;
|
||||
|
||||
}
|
||||
return newMatrix;
|
||||
}
|
||||
|
||||
int Decoder::readCode(Ref<zxing::BitArray> rawbits, int startIndex, int length) {
|
||||
int res = 0;
|
||||
|
||||
for (int i = startIndex; i < startIndex + length; i++) {
|
||||
res <<= 1;
|
||||
if (rawbits->get(i)) {
|
||||
res ++;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
63
src/qzxing/zxing/aztec/decoder/Decoder.h
Normal file
63
src/qzxing/zxing/aztec/decoder/Decoder.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Decoder.h
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/DecoderResult.h>
|
||||
#include <zxing/common/BitMatrix.h>
|
||||
#include <zxing/common/Str.h>
|
||||
#include <zxing/aztec/AztecDetectorResult.h>
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
|
||||
class Decoder : public Counted {
|
||||
private:
|
||||
enum Table {
|
||||
UPPER,
|
||||
LOWER,
|
||||
MIXED,
|
||||
DIGIT,
|
||||
PUNCT,
|
||||
BINARY
|
||||
};
|
||||
|
||||
static Table getTable(char t);
|
||||
static const char* getCharacter(Table table, int code);
|
||||
|
||||
int numCodewords_;
|
||||
int codewordSize_;
|
||||
Ref<AztecDetectorResult> ddata_;
|
||||
int invertedBitCount_;
|
||||
|
||||
Ref<String> getEncodedData(Ref<BitArray> correctedBits);
|
||||
Ref<BitArray> correctBits(Ref<BitArray> rawbits);
|
||||
Ref<BitArray> extractBits(Ref<BitMatrix> matrix);
|
||||
static Ref<BitMatrix> removeDashedLines(Ref<BitMatrix> matrix);
|
||||
static int readCode(Ref<BitArray> rawbits, int startIndex, int length);
|
||||
|
||||
|
||||
public:
|
||||
Decoder();
|
||||
Ref<DecoderResult> decode(Ref<AztecDetectorResult> detectorResult);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
541
src/qzxing/zxing/aztec/detector/AztecDetector.cpp
Normal file
541
src/qzxing/zxing/aztec/detector/AztecDetector.cpp
Normal file
|
@ -0,0 +1,541 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Detector.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/aztec/detector/Detector.h>
|
||||
#include <zxing/common/GridSampler.h>
|
||||
#include <zxing/common/detector/WhiteRectangleDetector.h>
|
||||
#include <zxing/common/reedsolomon/ReedSolomonDecoder.h>
|
||||
#include <zxing/common/reedsolomon/ReedSolomonException.h>
|
||||
#include <zxing/common/reedsolomon/GenericGF.h>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <zxing/NotFoundException.h>
|
||||
|
||||
using zxing::aztec::Detector;
|
||||
using zxing::aztec::Point;
|
||||
using zxing::aztec::AztecDetectorResult;
|
||||
using zxing::Ref;
|
||||
using zxing::ResultPoint;
|
||||
using zxing::BitArray;
|
||||
using zxing::BitMatrix;
|
||||
|
||||
Detector::Detector(Ref<BitMatrix> image):
|
||||
image_(image),
|
||||
nbLayers_(0),
|
||||
nbDataBlocks_(0),
|
||||
nbCenterLayers_(0) {
|
||||
|
||||
}
|
||||
|
||||
// using namespace std;
|
||||
|
||||
Ref<AztecDetectorResult> Detector::detect() {
|
||||
Ref<Point> pCenter = getMatrixCenter();
|
||||
|
||||
std::vector<Ref<Point> > bullEyeCornerPoints = getBullEyeCornerPoints(pCenter);
|
||||
|
||||
extractParameters(bullEyeCornerPoints);
|
||||
|
||||
std::vector<Ref<ResultPoint> > corners = getMatrixCornerPoints(bullEyeCornerPoints);
|
||||
|
||||
Ref<BitMatrix> bits = sampleGrid(image_, corners[shift_%4], corners[(shift_+3)%4], corners[(shift_+2)%4], corners[(shift_+1)%4]);
|
||||
|
||||
// std::printf("------------\ndetected: compact:%s, nbDataBlocks:%d, nbLayers:%d\n------------\n",compact_?"YES":"NO", nbDataBlocks_, nbLayers_);
|
||||
|
||||
return Ref<AztecDetectorResult>(new AztecDetectorResult(bits, corners, compact_, nbDataBlocks_, nbLayers_));
|
||||
}
|
||||
|
||||
void Detector::extractParameters(std::vector<Ref<Point> > bullEyeCornerPoints) {
|
||||
// get the bits around the bull's eye
|
||||
Ref<BitArray> resab = sampleLine(bullEyeCornerPoints[0], bullEyeCornerPoints[1], 2*nbCenterLayers_+1);
|
||||
Ref<BitArray> resbc = sampleLine(bullEyeCornerPoints[1], bullEyeCornerPoints[2], 2*nbCenterLayers_+1);
|
||||
Ref<BitArray> rescd = sampleLine(bullEyeCornerPoints[2], bullEyeCornerPoints[3], 2*nbCenterLayers_+1);
|
||||
Ref<BitArray> resda = sampleLine(bullEyeCornerPoints[3], bullEyeCornerPoints[0], 2*nbCenterLayers_+1);
|
||||
|
||||
// determin the orientation of the matrix
|
||||
if (resab->get(0) && resab->get(2 * nbCenterLayers_)) {
|
||||
shift_ = 0;
|
||||
} else if (resbc->get(0) && resbc->get(2 * nbCenterLayers_)) {
|
||||
shift_ = 1;
|
||||
} else if (rescd->get(0) && rescd->get(2 * nbCenterLayers_)) {
|
||||
shift_ = 2;
|
||||
} else if (resda->get(0) && resda->get(2 * nbCenterLayers_)) {
|
||||
shift_ = 3;
|
||||
} else {
|
||||
// std::printf("could not detemine orientation\n");
|
||||
throw ReaderException("could not determine orientation");
|
||||
}
|
||||
|
||||
//d a
|
||||
//
|
||||
//c b
|
||||
|
||||
//flatten the bits in a single array
|
||||
Ref<BitArray> parameterData(new BitArray(compact_?28:40));
|
||||
Ref<BitArray> shiftedParameterData(new BitArray(compact_?28:40));
|
||||
|
||||
if (compact_) {
|
||||
for (int i = 0; i < 7; i++) {
|
||||
if (resab->get(2+i)) shiftedParameterData->set(i);
|
||||
if (resbc->get(2+i)) shiftedParameterData->set(i+7);
|
||||
if (rescd->get(2+i)) shiftedParameterData->set(i+14);
|
||||
if (resda->get(2+i)) shiftedParameterData->set(i+21);
|
||||
}
|
||||
for (int i = 0; i < 28; i++) {
|
||||
if (shiftedParameterData->get((i+shift_*7)%28)) parameterData->set(i);
|
||||
}
|
||||
|
||||
} else {
|
||||
for (int i = 0; i < 11; i++) {
|
||||
if (i < 5) {
|
||||
if (resab->get(2+i)) shiftedParameterData->set(i);
|
||||
if (resbc->get(2+i)) shiftedParameterData->set(i+10);
|
||||
if (rescd->get(2+i)) shiftedParameterData->set(i+20);
|
||||
if (resda->get(2+i)) shiftedParameterData->set(i+30);
|
||||
}
|
||||
if (i > 5) {
|
||||
if (resab->get(2+i)) shiftedParameterData->set(i-1);
|
||||
if (resbc->get(2+i)) shiftedParameterData->set(i+10-1);
|
||||
if (rescd->get(2+i)) shiftedParameterData->set(i+20-1);
|
||||
if (resda->get(2+i)) shiftedParameterData->set(i+30-1);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 40; i++) {
|
||||
if (shiftedParameterData->get((i+shift_*10)%40)) parameterData->set(i);
|
||||
}
|
||||
}
|
||||
|
||||
correctParameterData(parameterData, compact_);
|
||||
|
||||
getParameters(parameterData);
|
||||
}
|
||||
|
||||
std::vector<Ref<ResultPoint> > Detector::getMatrixCornerPoints(std::vector<Ref<Point> > bullEyeCornerPoints) {
|
||||
float ratio = (2 * nbLayers_ + (nbLayers_ > 4 ? 1 : 0) + (nbLayers_ - 4) / 8) / (2.0f * nbCenterLayers_);
|
||||
|
||||
int dx = bullEyeCornerPoints[0]->x - bullEyeCornerPoints[2]->x;
|
||||
dx += dx > 0 ? 1 : -1;
|
||||
int dy = bullEyeCornerPoints[0]->y - bullEyeCornerPoints[2]->y;
|
||||
dy += dy > 0 ? 1 : -1;
|
||||
|
||||
int targetcx = ROUND(bullEyeCornerPoints[2]->x - ratio * dx);
|
||||
int targetcy = ROUND(bullEyeCornerPoints[2]->y - ratio * dy);
|
||||
|
||||
int targetax = ROUND(bullEyeCornerPoints[0]->x + ratio * dx);
|
||||
int targetay = ROUND(bullEyeCornerPoints[0]->y + ratio * dy);
|
||||
|
||||
dx = bullEyeCornerPoints[1]->x - bullEyeCornerPoints[3]->x;
|
||||
dx += dx > 0 ? 1 : -1;
|
||||
dy = bullEyeCornerPoints[1]->y - bullEyeCornerPoints[3]->y;
|
||||
dy += dy > 0 ? 1 : -1;
|
||||
|
||||
int targetdx = ROUND(bullEyeCornerPoints[3]->x - ratio * dx);
|
||||
int targetdy = ROUND(bullEyeCornerPoints[3]->y - ratio * dy);
|
||||
int targetbx = ROUND(bullEyeCornerPoints[1]->x + ratio * dx);
|
||||
int targetby = ROUND(bullEyeCornerPoints[1]->y + ratio * dy);
|
||||
|
||||
if (!isValid(targetax, targetay) ||
|
||||
!isValid(targetbx, targetby) ||
|
||||
!isValid(targetcx, targetcy) ||
|
||||
!isValid(targetdx, targetdy)) {
|
||||
throw ReaderException("matrix extends over image bounds");
|
||||
}
|
||||
std::vector<Ref<ResultPoint> > returnValue;
|
||||
returnValue.push_back(Ref<ResultPoint>(new ResultPoint(targetax, targetay)));
|
||||
returnValue.push_back(Ref<ResultPoint>(new ResultPoint(targetbx, targetby)));
|
||||
returnValue.push_back(Ref<ResultPoint>(new ResultPoint(targetcx, targetcy)));
|
||||
returnValue.push_back(Ref<ResultPoint>(new ResultPoint(targetdx, targetdy)));
|
||||
|
||||
return returnValue;
|
||||
|
||||
}
|
||||
|
||||
void Detector::correctParameterData(Ref<zxing::BitArray> parameterData, bool compact) {
|
||||
int numCodewords;
|
||||
int numDataCodewords;
|
||||
|
||||
if (compact) {
|
||||
numCodewords = 7;
|
||||
numDataCodewords = 2;
|
||||
} else {
|
||||
numCodewords = 10;
|
||||
numDataCodewords = 4;
|
||||
}
|
||||
|
||||
int numECCodewords = numCodewords - numDataCodewords;
|
||||
|
||||
ArrayRef<int> parameterWords(new Array<int>(numCodewords));
|
||||
|
||||
int codewordSize = 4;
|
||||
for (int i = 0; i < numCodewords; i++) {
|
||||
int flag = 1;
|
||||
for (int j = 1; j <= codewordSize; j++) {
|
||||
if (parameterData->get(codewordSize*i + codewordSize - j)) {
|
||||
parameterWords[i] += flag;
|
||||
}
|
||||
flag <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// std::printf("parameter data reed solomon\n");
|
||||
ReedSolomonDecoder rsDecoder(GenericGF::AZTEC_PARAM);
|
||||
rsDecoder.decode(parameterWords, numECCodewords);
|
||||
} catch (ReedSolomonException& e) {
|
||||
// std::printf("reed solomon decoding failed\n");
|
||||
throw ReaderException("failed to decode parameter data");
|
||||
}
|
||||
|
||||
parameterData->clear();
|
||||
for (int i = 0; i < numDataCodewords; i++) {
|
||||
int flag = 1;
|
||||
for (int j = 1; j <= codewordSize; j++) {
|
||||
if ((parameterWords[i] & flag) == flag) {
|
||||
parameterData->set(i*codewordSize+codewordSize-j);
|
||||
}
|
||||
flag <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Ref<Point> > Detector::getBullEyeCornerPoints(Ref<zxing::aztec::Point> pCenter) {
|
||||
Ref<Point> pina = pCenter;
|
||||
Ref<Point> pinb = pCenter;
|
||||
Ref<Point> pinc = pCenter;
|
||||
Ref<Point> pind = pCenter;
|
||||
|
||||
bool color = true;
|
||||
|
||||
for (nbCenterLayers_ = 1; nbCenterLayers_ < 9; nbCenterLayers_++) {
|
||||
Ref<Point> pouta = getFirstDifferent(pina, color, 1, -1);
|
||||
Ref<Point> poutb = getFirstDifferent(pinb, color, 1, 1);
|
||||
Ref<Point> poutc = getFirstDifferent(pinc, color, -1, 1);
|
||||
Ref<Point> poutd = getFirstDifferent(pind, color, -1, -1);
|
||||
|
||||
//d a
|
||||
//
|
||||
//c b
|
||||
|
||||
if (nbCenterLayers_ > 2) {
|
||||
float q = distance(poutd, pouta) * nbCenterLayers_ / (distance(pind, pina) * (nbCenterLayers_ + 2));
|
||||
if (q < 0.75 || q > 1.25 || !isWhiteOrBlackRectangle(pouta, poutb, poutc, poutd)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pina = pouta;
|
||||
pinb = poutb;
|
||||
pinc = poutc;
|
||||
pind = poutd;
|
||||
|
||||
color = !color;
|
||||
}
|
||||
|
||||
if (nbCenterLayers_ != 5 && nbCenterLayers_ != 7) {
|
||||
throw ReaderException("encountered wrong bullseye ring count");
|
||||
}
|
||||
|
||||
compact_ = nbCenterLayers_ == 5;
|
||||
|
||||
|
||||
|
||||
float ratio = 0.75f*2 / (2*nbCenterLayers_-3);
|
||||
|
||||
int dx = pina->x - pind->x;
|
||||
int dy = pina->y - pinc->y;
|
||||
|
||||
int targetcx = ROUND(pinc->x - ratio * dx);
|
||||
int targetcy = ROUND(pinc->y - ratio * dy);
|
||||
int targetax = ROUND(pina->x + ratio * dx);
|
||||
int targetay = ROUND(pina->y + ratio * dy);
|
||||
|
||||
dx = pinb->x - pind->x;
|
||||
dy = pinb->y - pind->y;
|
||||
|
||||
int targetdx = ROUND(pind->x - ratio * dx);
|
||||
int targetdy = ROUND(pind->y - ratio * dy);
|
||||
int targetbx = ROUND(pinb->x + ratio * dx);
|
||||
int targetby = ROUND(pinb->y + ratio * dy);
|
||||
|
||||
if (!isValid(targetax, targetay) ||
|
||||
!isValid(targetbx, targetby) ||
|
||||
!isValid(targetcx, targetcy) ||
|
||||
!isValid(targetdx, targetdy)) {
|
||||
throw ReaderException("bullseye extends over image bounds");
|
||||
}
|
||||
|
||||
std::vector<Ref<Point> > returnValue;
|
||||
returnValue.push_back(Ref<Point>(new Point(targetax, targetay)));
|
||||
returnValue.push_back(Ref<Point>(new Point(targetbx, targetby)));
|
||||
returnValue.push_back(Ref<Point>(new Point(targetcx, targetcy)));
|
||||
returnValue.push_back(Ref<Point>(new Point(targetdx, targetdy)));
|
||||
|
||||
return returnValue;
|
||||
|
||||
}
|
||||
|
||||
Ref<Point> Detector::getMatrixCenter() {
|
||||
Ref<ResultPoint> pointA, pointB, pointC, pointD;
|
||||
try {
|
||||
|
||||
std::vector<Ref<ResultPoint> > cornerPoints = WhiteRectangleDetector(image_).detect();
|
||||
pointA = cornerPoints[0];
|
||||
pointB = cornerPoints[1];
|
||||
pointC = cornerPoints[2];
|
||||
pointD = cornerPoints[3];
|
||||
|
||||
} catch (NotFoundException& e) {
|
||||
|
||||
int cx = image_->getWidth() / 2;
|
||||
int cy = image_->getHeight() / 2;
|
||||
|
||||
pointA = getFirstDifferent(Ref<Point>(new Point(cx+15/2, cy-15/2)), false, 1, -1)->toResultPoint();
|
||||
pointB = getFirstDifferent(Ref<Point>(new Point(cx+15/2, cy+15/2)), false, 1, 1)->toResultPoint();
|
||||
pointC = getFirstDifferent(Ref<Point>(new Point(cx-15/2, cy+15/2)), false, -1, -1)->toResultPoint();
|
||||
pointD = getFirstDifferent(Ref<Point>(new Point(cx-15/2, cy-15/2)), false, -1, -1)->toResultPoint();
|
||||
|
||||
}
|
||||
|
||||
int cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
|
||||
int cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
|
||||
|
||||
try {
|
||||
|
||||
std::vector<Ref<ResultPoint> > cornerPoints = WhiteRectangleDetector(image_, 15, cx, cy).detect();
|
||||
pointA = cornerPoints[0];
|
||||
pointB = cornerPoints[1];
|
||||
pointC = cornerPoints[2];
|
||||
pointD = cornerPoints[3];
|
||||
|
||||
} catch (NotFoundException& e) {
|
||||
|
||||
pointA = getFirstDifferent(Ref<Point>(new Point(cx+15/2, cy-15/2)), false, 1, -1)->toResultPoint();
|
||||
pointB = getFirstDifferent(Ref<Point>(new Point(cx+15/2, cy+15/2)), false, 1, 1)->toResultPoint();
|
||||
pointC = getFirstDifferent(Ref<Point>(new Point(cx-15/2, cy+15/2)), false, -1, -1)->toResultPoint();
|
||||
pointD = getFirstDifferent(Ref<Point>(new Point(cx-15/2, cy-15/2)), false, -1, -1)->toResultPoint();
|
||||
|
||||
}
|
||||
|
||||
cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
|
||||
cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
|
||||
|
||||
return Ref<Point>(new Point(cx, cy));
|
||||
|
||||
}
|
||||
|
||||
Ref<BitMatrix> Detector::sampleGrid(Ref<zxing::BitMatrix> image,
|
||||
Ref<zxing::ResultPoint> topLeft,
|
||||
Ref<zxing::ResultPoint> bottomLeft,
|
||||
Ref<zxing::ResultPoint> bottomRight,
|
||||
Ref<zxing::ResultPoint> topRight) {
|
||||
int dimension;
|
||||
if (compact_) {
|
||||
dimension = 4 * nbLayers_+11;
|
||||
} else {
|
||||
if (nbLayers_ <= 4) {
|
||||
dimension = 4 * nbLayers_ + 15;
|
||||
} else {
|
||||
dimension = 4 * nbLayers_ + 2 * ((nbLayers_-4)/8 + 1) + 15;
|
||||
}
|
||||
}
|
||||
|
||||
GridSampler sampler = GridSampler::getInstance();
|
||||
|
||||
return sampler.sampleGrid(image,
|
||||
dimension,
|
||||
0.5f,
|
||||
0.5f,
|
||||
dimension - 0.5f,
|
||||
0.5f,
|
||||
dimension - 0.5f,
|
||||
dimension - 0.5f,
|
||||
0.5f,
|
||||
dimension - 0.5f,
|
||||
topLeft->getX(),
|
||||
topLeft->getY(),
|
||||
topRight->getX(),
|
||||
topRight->getY(),
|
||||
bottomRight->getX(),
|
||||
bottomRight->getY(),
|
||||
bottomLeft->getX(),
|
||||
bottomLeft->getY());
|
||||
}
|
||||
|
||||
void Detector::getParameters(Ref<zxing::BitArray> parameterData) {
|
||||
nbLayers_ = 0;
|
||||
nbDataBlocks_ = 0;
|
||||
|
||||
int nbBitsForNbLayers;
|
||||
int nbBitsForNbDatablocks;
|
||||
|
||||
if (compact_) {
|
||||
nbBitsForNbLayers = 2;
|
||||
nbBitsForNbDatablocks = 6;
|
||||
} else {
|
||||
nbBitsForNbLayers = 5;
|
||||
nbBitsForNbDatablocks = 11;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nbBitsForNbLayers; i++) {
|
||||
nbLayers_ <<= 1;
|
||||
if (parameterData->get(i)) {
|
||||
nbLayers_ += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = nbBitsForNbLayers; i < nbBitsForNbLayers + nbBitsForNbDatablocks; i++) {
|
||||
nbDataBlocks_ <<= 1;
|
||||
if (parameterData->get(i)) {
|
||||
nbDataBlocks_ += 1;
|
||||
}
|
||||
}
|
||||
|
||||
nbLayers_ ++;
|
||||
nbDataBlocks_ ++;
|
||||
}
|
||||
|
||||
Ref<BitArray> Detector::sampleLine(Ref<zxing::aztec::Point> p1, Ref<zxing::aztec::Point> p2, int size) {
|
||||
Ref<BitArray> res(new BitArray(size));
|
||||
|
||||
float d = distance(p1, p2);
|
||||
float moduleSize = d / (size-1);
|
||||
float dx = moduleSize * (p2->x - p1->x)/d;
|
||||
float dy = moduleSize * (p2->y - p1->y)/d;
|
||||
|
||||
float px = p1->x;
|
||||
float py = p1->y;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (image_->get(ROUND(px), ROUND(py))) res->set(i);
|
||||
px += dx;
|
||||
py += dy;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Detector::isWhiteOrBlackRectangle(Ref<zxing::aztec::Point> p1,
|
||||
Ref<zxing::aztec::Point> p2,
|
||||
Ref<zxing::aztec::Point> p3,
|
||||
Ref<zxing::aztec::Point> p4) {
|
||||
int corr = 3;
|
||||
|
||||
p1 = new Point(p1->x - corr, p1->y + corr);
|
||||
p2 = new Point(p2->x - corr, p2->y - corr);
|
||||
p3 = new Point(p3->x + corr, p3->y - corr);
|
||||
p4 = new Point(p4->x + corr, p4->y + corr);
|
||||
|
||||
int cInit = getColor(p4, p1);
|
||||
|
||||
if (cInit == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int c = getColor(p1, p2);
|
||||
|
||||
if (c != cInit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
c = getColor(p2, p3);
|
||||
|
||||
if (c != cInit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
c = getColor(p3, p4);
|
||||
|
||||
if (c != cInit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int Detector::getColor(Ref<zxing::aztec::Point> p1, Ref<zxing::aztec::Point> p2) {
|
||||
float d = distance(p1, p2);
|
||||
|
||||
float dx = (p2->x - p1->x) / d;
|
||||
float dy = (p2->y - p1->y) / d;
|
||||
|
||||
int error = 0;
|
||||
|
||||
float px = p1->x;
|
||||
float py = p1->y;
|
||||
|
||||
bool colorModel = image_->get(p1->x, p1->y);
|
||||
|
||||
for (int i = 0; i < d; i++) {
|
||||
px += dx;
|
||||
py += dy;
|
||||
if (image_->get(ROUND(px), ROUND(py)) != colorModel) {
|
||||
error ++;
|
||||
}
|
||||
}
|
||||
|
||||
float errRatio = (float)error/d;
|
||||
|
||||
|
||||
if (errRatio > 0.1 && errRatio < 0.9) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (errRatio <= 0.1) {
|
||||
return colorModel?1:-1;
|
||||
} else {
|
||||
return colorModel?-1:1;
|
||||
}
|
||||
}
|
||||
|
||||
Ref<Point> Detector::getFirstDifferent(Ref<zxing::aztec::Point> init, bool color, int dx, int dy) {
|
||||
int x = init->x + dx;
|
||||
int y = init->y + dy;
|
||||
|
||||
while (isValid(x, y) && image_->get(x, y) == color) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
x -= dx;
|
||||
y -= dy;
|
||||
|
||||
while (isValid(x, y) && image_->get(x, y) == color) {
|
||||
x += dx;
|
||||
}
|
||||
|
||||
x -= dx;
|
||||
|
||||
while (isValid(x, y) && image_->get(x, y) == color) {
|
||||
y += dy;
|
||||
}
|
||||
|
||||
y -= dy;
|
||||
|
||||
return Ref<Point>(new Point(x, y));
|
||||
}
|
||||
|
||||
bool Detector::isValid(int x, int y) {
|
||||
return x >= 0 && x < (int)image_->getWidth() && y > 0 && y < (int)image_->getHeight();
|
||||
}
|
||||
|
||||
float Detector::distance(Ref<zxing::aztec::Point> a, Ref<zxing::aztec::Point> b) {
|
||||
return sqrtf((float)((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y)));
|
||||
}
|
87
src/qzxing/zxing/aztec/detector/Detector.h
Normal file
87
src/qzxing/zxing/aztec/detector/Detector.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Detector.h
|
||||
* zxing
|
||||
*
|
||||
* Created by Lukas Stabe on 08/02/2012.
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/ResultPoint.h>
|
||||
#include <zxing/common/BitMatrix.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
#include <zxing/aztec/AztecDetectorResult.h>
|
||||
|
||||
#define ROUND(a) ((int)(a + 0.5f))
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
|
||||
class Point : public Counted {
|
||||
public:
|
||||
int x;
|
||||
int y;
|
||||
|
||||
Ref<ResultPoint> toResultPoint() {
|
||||
return Ref<ResultPoint>(new ResultPoint(x, y));
|
||||
}
|
||||
|
||||
Point(int ax, int ay):x(ax),y(ay) {};
|
||||
|
||||
};
|
||||
|
||||
class Detector : public Counted {
|
||||
|
||||
private:
|
||||
Ref<BitMatrix> image_;
|
||||
|
||||
bool compact_;
|
||||
int nbLayers_;
|
||||
int nbDataBlocks_;
|
||||
int nbCenterLayers_;
|
||||
int shift_;
|
||||
|
||||
void extractParameters(std::vector<Ref<Point> > bullEyeCornerPoints);
|
||||
std::vector<Ref<ResultPoint> > getMatrixCornerPoints(std::vector<Ref<Point> > bullEyeCornerPoints);
|
||||
static void correctParameterData(Ref<BitArray> parameterData, bool compact);
|
||||
std::vector<Ref<Point> > getBullEyeCornerPoints(Ref<Point> pCenter);
|
||||
Ref<Point> getMatrixCenter();
|
||||
Ref<BitMatrix> sampleGrid(Ref<BitMatrix> image,
|
||||
Ref<ResultPoint> topLeft,
|
||||
Ref<ResultPoint> bottomLeft,
|
||||
Ref<ResultPoint> bottomRight,
|
||||
Ref<ResultPoint> topRight);
|
||||
void getParameters(Ref<BitArray> parameterData);
|
||||
Ref<BitArray> sampleLine(Ref<Point> p1, Ref<Point> p2, int size);
|
||||
bool isWhiteOrBlackRectangle(Ref<Point> p1,
|
||||
Ref<Point> p2,
|
||||
Ref<Point> p3,
|
||||
Ref<Point> p4);
|
||||
int getColor(Ref<Point> p1, Ref<Point> p2);
|
||||
Ref<Point> getFirstDifferent(Ref<Point> init, bool color, int dx, int dy);
|
||||
bool isValid(int x, int y);
|
||||
static float distance(Ref<Point> a, Ref<Point> b);
|
||||
|
||||
public:
|
||||
Detector(Ref<BitMatrix> image);
|
||||
Ref<AztecDetectorResult> detect();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
22
src/qzxing/zxing/common/Array.cpp
Normal file
22
src/qzxing/zxing/common/Array.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Array.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 07/05/2008.
|
||||
* Copyright 2008 Google UK. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Array.h>
|
||||
|
208
src/qzxing/zxing/common/Array.h
Normal file
208
src/qzxing/zxing/common/Array.h
Normal file
|
@ -0,0 +1,208 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __ARRAY_H__
|
||||
#define __ARRAY_H__
|
||||
|
||||
/*
|
||||
* Array.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
|
||||
#ifdef DEBUG_COUNTING
|
||||
#include <iostream>
|
||||
#include <typeinfo>
|
||||
#endif
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
|
||||
|
||||
namespace zxing {
|
||||
|
||||
template<typename T> class Array : public Counted {
|
||||
protected:
|
||||
public:
|
||||
std::vector<T> values_;
|
||||
Array(size_t n) :
|
||||
Counted(), values_(n, T()) {
|
||||
}
|
||||
Array(T *ts, size_t n) :
|
||||
Counted(), values_(ts, ts+n) {
|
||||
}
|
||||
Array(T v, size_t n) :
|
||||
Counted(), values_(n, v) {
|
||||
}
|
||||
Array(std::vector<T> &v) :
|
||||
Counted(), values_(v) {
|
||||
}
|
||||
Array(Array<T> &other) :
|
||||
Counted(), values_(other.values_) {
|
||||
}
|
||||
Array(Array<T> *other) :
|
||||
Counted(), values_(other->values_) {
|
||||
}
|
||||
virtual ~Array() {
|
||||
}
|
||||
Array<T>& operator=(const Array<T> &other) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "assigning values from Array " << &other << " to this Array " << this << ", ";
|
||||
#endif
|
||||
values_ = other.values_;
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "new size = " << values_.size() << "\n";
|
||||
#endif
|
||||
return *this;
|
||||
}
|
||||
Array<T>& operator=(const std::vector<T> &array) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "assigning values from Array " << &array << " to this Array " << this << ", ";
|
||||
#endif
|
||||
values_ = array;
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "new size = " << values_.size() << "\n";
|
||||
#endif
|
||||
return *this;
|
||||
}
|
||||
T operator[](size_t i) const {
|
||||
return values_[i];
|
||||
}
|
||||
T& operator[](size_t i) {
|
||||
return values_[i];
|
||||
}
|
||||
size_t size() const {
|
||||
return values_.size();
|
||||
}
|
||||
std::vector<T> values() const {
|
||||
return values_;
|
||||
}
|
||||
std::vector<T>& values() {
|
||||
return values_;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> class ArrayRef : public Counted {
|
||||
private:
|
||||
public:
|
||||
Array<T> *array_;
|
||||
ArrayRef() :
|
||||
array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating empty ArrayRef " << this << "\n";
|
||||
#endif
|
||||
}
|
||||
ArrayRef(size_t n) :
|
||||
array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating ArrayRef " << this << "with size " << n << "\n";
|
||||
#endif
|
||||
reset(new Array<T> (n));
|
||||
}
|
||||
ArrayRef(T *ts, size_t n) :
|
||||
array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating ArrayRef " << this << "with " << n << " elements at " << (void *)ts << "\n";
|
||||
#endif
|
||||
reset(new Array<T> (ts, n));
|
||||
}
|
||||
ArrayRef(Array<T> *a) :
|
||||
array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating ArrayRef " << this << " from pointer:\n";
|
||||
#endif
|
||||
reset(a);
|
||||
}
|
||||
ArrayRef(const Array<T> &a) :
|
||||
array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating ArrayRef " << this << " from reference to Array " << (void *)&a << ":\n";
|
||||
#endif
|
||||
reset(const_cast<Array<T> *>(&a));
|
||||
}
|
||||
ArrayRef(const ArrayRef &other) :
|
||||
Counted(), array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
|
||||
#endif
|
||||
reset(other.array_);
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
ArrayRef(const ArrayRef<Y> &other) :
|
||||
array_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
|
||||
#endif
|
||||
reset(static_cast<const Array<T> *>(other.array_));
|
||||
}
|
||||
|
||||
~ArrayRef() {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "destroying ArrayRef " << this << " with " << (array_ ? typeid(*array_).name() : "NULL") << " "
|
||||
<< array_ << "\n";
|
||||
#endif
|
||||
if (array_) {
|
||||
array_->release();
|
||||
}
|
||||
array_ = 0;
|
||||
}
|
||||
|
||||
T operator[](size_t i) const {
|
||||
return (*array_)[i];
|
||||
}
|
||||
T& operator[](size_t i) {
|
||||
return (*array_)[i];
|
||||
}
|
||||
size_t size() const {
|
||||
return array_->size();
|
||||
}
|
||||
|
||||
void reset(Array<T> *a) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "resetting ArrayRef " << this << " from " << (array_ ? typeid(*array_).name() : "NULL") << " "
|
||||
<< array_ << " to " << (a ? typeid(*a).name() : "NULL") << " " << a << "\n";
|
||||
#endif
|
||||
if (a) {
|
||||
a->retain();
|
||||
}
|
||||
if (array_) {
|
||||
array_->release();
|
||||
}
|
||||
array_ = a;
|
||||
}
|
||||
void reset(const ArrayRef<T> &other) {
|
||||
reset(other.array_);
|
||||
}
|
||||
ArrayRef<T>& operator=(const ArrayRef<T> &other) {
|
||||
reset(other);
|
||||
return *this;
|
||||
}
|
||||
ArrayRef<T>& operator=(Array<T> *a) {
|
||||
reset(a);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Array<T>& operator*() {
|
||||
return *array_;
|
||||
}
|
||||
Array<T>* operator->() {
|
||||
return array_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace zxing
|
||||
|
||||
#endif // __ARRAY_H__
|
127
src/qzxing/zxing/common/BitArray.cpp
Normal file
127
src/qzxing/zxing/common/BitArray.cpp
Normal file
|
@ -0,0 +1,127 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Copyright 2010 ZXing authors. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/BitArray.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace zxing {
|
||||
|
||||
|
||||
size_t BitArray::wordsForBits(size_t bits) {
|
||||
int arraySize = (bits + bitsPerWord_ - 1) >> logBits_;
|
||||
return arraySize;
|
||||
}
|
||||
|
||||
BitArray::BitArray(size_t size) :
|
||||
size_(size), bits_(wordsForBits(size), (const unsigned int)0) {
|
||||
}
|
||||
|
||||
BitArray::~BitArray() {
|
||||
}
|
||||
|
||||
size_t BitArray::getSize() {
|
||||
return size_;
|
||||
}
|
||||
|
||||
void BitArray::setBulk(size_t i, unsigned int newBits) {
|
||||
bits_[i >> logBits_] = newBits;
|
||||
}
|
||||
|
||||
void BitArray::setRange(int start, int end) {
|
||||
if (end < start) {
|
||||
throw IllegalArgumentException("invalid call to BitArray::setRange");
|
||||
}
|
||||
if (end == start) {
|
||||
return;
|
||||
}
|
||||
end--; // will be easier to treat this as the last actually set bit -- inclusive
|
||||
int firstInt = start >> 5;
|
||||
int lastInt = end >> 5;
|
||||
for (int i = firstInt; i <= lastInt; i++) {
|
||||
int firstBit = i > firstInt ? 0 : start & 0x1F;
|
||||
int lastBit = i < lastInt ? 31 : end & 0x1F;
|
||||
int mask;
|
||||
if (firstBit == 0 && lastBit == 31) {
|
||||
mask = -1;
|
||||
} else {
|
||||
mask = 0;
|
||||
for (int j = firstBit; j <= lastBit; j++) {
|
||||
mask |= 1 << j;
|
||||
}
|
||||
}
|
||||
bits_[i] |= mask;
|
||||
}
|
||||
}
|
||||
|
||||
void BitArray::clear() {
|
||||
size_t max = bits_.size();
|
||||
for (size_t i = 0; i < max; i++) {
|
||||
bits_[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool BitArray::isRange(size_t start, size_t end, bool value) {
|
||||
if (end < start) {
|
||||
throw IllegalArgumentException("end must be after start");
|
||||
}
|
||||
if (end == start) {
|
||||
return true;
|
||||
}
|
||||
// treat the 'end' as inclusive, rather than exclusive
|
||||
end--;
|
||||
size_t firstWord = start >> logBits_;
|
||||
size_t lastWord = end >> logBits_;
|
||||
for (size_t i = firstWord; i <= lastWord; i++) {
|
||||
size_t firstBit = i > firstWord ? 0 : start & bitsMask_;
|
||||
size_t lastBit = i < lastWord ? bitsPerWord_ - 1: end & bitsMask_;
|
||||
unsigned int mask;
|
||||
if (firstBit == 0 && lastBit == bitsPerWord_ - 1) {
|
||||
mask = numeric_limits<unsigned int>::max();
|
||||
} else {
|
||||
mask = 0;
|
||||
for (size_t j = firstBit; j <= lastBit; j++) {
|
||||
mask |= 1 << j;
|
||||
}
|
||||
}
|
||||
if (value) {
|
||||
if ((bits_[i] & mask) != mask) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((bits_[i] & mask) != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
vector<unsigned int>& BitArray::getBitArray() {
|
||||
return bits_;
|
||||
}
|
||||
|
||||
void BitArray::reverse() {
|
||||
std::vector<unsigned int> newBits(bits_.size(),(const unsigned int) 0);
|
||||
for (size_t i = 0; i < size_; i++) {
|
||||
if (get(size_ - i - 1)) {
|
||||
newBits[i >> logBits_] |= 1<< (i & bitsMask_);
|
||||
}
|
||||
}
|
||||
bits_ = newBits;
|
||||
}
|
||||
}
|
70
src/qzxing/zxing/common/BitArray.h
Normal file
70
src/qzxing/zxing/common/BitArray.h
Normal file
|
@ -0,0 +1,70 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __BIT_ARRAY_H__
|
||||
#define __BIT_ARRAY_H__
|
||||
|
||||
/*
|
||||
* Copyright 2010 ZXing authors. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
#define ZX_LOG_DIGITS(digits) \
|
||||
((digits == 8) ? 3 : \
|
||||
((digits == 16) ? 4 : \
|
||||
((digits == 32) ? 5 : \
|
||||
((digits == 64) ? 6 : \
|
||||
((digits == 128) ? 7 : \
|
||||
(-1))))))
|
||||
|
||||
class BitArray : public Counted {
|
||||
private:
|
||||
size_t size_;
|
||||
std::vector<unsigned int> bits_;
|
||||
static const unsigned int bitsPerWord_ =
|
||||
std::numeric_limits<unsigned int>::digits;
|
||||
static const unsigned int logBits_ = ZX_LOG_DIGITS(bitsPerWord_);
|
||||
static const unsigned int bitsMask_ = (1 << logBits_) - 1;
|
||||
static size_t wordsForBits(size_t bits);
|
||||
explicit BitArray();
|
||||
|
||||
public:
|
||||
BitArray(size_t size);
|
||||
~BitArray();
|
||||
size_t getSize();
|
||||
|
||||
bool get(size_t i) {
|
||||
return (bits_[i >> logBits_] & (1 << (i & bitsMask_))) != 0;
|
||||
}
|
||||
|
||||
void set(size_t i) {
|
||||
bits_[i >> logBits_] |= 1 << (i & bitsMask_);
|
||||
}
|
||||
|
||||
void setBulk(size_t i, unsigned int newBits);
|
||||
void setRange(int start, int end);
|
||||
void clear();
|
||||
bool isRange(size_t start, size_t end, bool value);
|
||||
std::vector<unsigned int>& getBitArray();
|
||||
void reverse();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __BIT_ARRAY_H__
|
157
src/qzxing/zxing/common/BitMatrix.cpp
Normal file
157
src/qzxing/zxing/common/BitMatrix.cpp
Normal file
|
@ -0,0 +1,157 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Copyright 2010 ZXing authors. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/BitMatrix.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using std::ostream;
|
||||
using std::ostringstream;
|
||||
|
||||
using zxing::BitMatrix;
|
||||
using zxing::BitArray;
|
||||
using zxing::Ref;
|
||||
|
||||
namespace {
|
||||
size_t wordsForSize(size_t width,
|
||||
size_t height,
|
||||
unsigned int bitsPerWord,
|
||||
unsigned int logBits) {
|
||||
size_t bits = width * height;
|
||||
int arraySize = (bits + bitsPerWord - 1) >> logBits;
|
||||
return arraySize;
|
||||
}
|
||||
}
|
||||
|
||||
BitMatrix::BitMatrix(size_t dimension) :
|
||||
width_(dimension), height_(dimension), words_(0), bits_(NULL) {
|
||||
words_ = wordsForSize(width_, height_, bitsPerWord, logBits);
|
||||
bits_ = new unsigned int[words_];
|
||||
clear();
|
||||
}
|
||||
|
||||
BitMatrix::BitMatrix(size_t width, size_t height) :
|
||||
width_(width), height_(height), words_(0), bits_(NULL) {
|
||||
words_ = wordsForSize(width_, height_, bitsPerWord, logBits);
|
||||
bits_ = new unsigned int[words_];
|
||||
clear();
|
||||
}
|
||||
|
||||
BitMatrix::~BitMatrix() {
|
||||
delete[] bits_;
|
||||
}
|
||||
|
||||
|
||||
void BitMatrix::flip(size_t x, size_t y) {
|
||||
size_t offset = x + width_ * y;
|
||||
bits_[offset >> logBits] ^= 1 << (offset & bitsMask);
|
||||
}
|
||||
|
||||
void BitMatrix::clear() {
|
||||
std::fill(bits_, bits_+words_, 0);
|
||||
}
|
||||
|
||||
void BitMatrix::setRegion(size_t left, size_t top, size_t width, size_t height) {
|
||||
if ((long)top < 0 || (long)left < 0) {
|
||||
throw IllegalArgumentException("topI and leftJ must be nonnegative");
|
||||
}
|
||||
if (height < 1 || width < 1) {
|
||||
throw IllegalArgumentException("height and width must be at least 1");
|
||||
}
|
||||
size_t right = left + width;
|
||||
size_t bottom = top + height;
|
||||
if (right > width_ || bottom > height_) {
|
||||
throw IllegalArgumentException("top + height and left + width must be <= matrix dimension");
|
||||
}
|
||||
for (size_t y = top; y < bottom; y++) {
|
||||
int yOffset = width_ * y;
|
||||
for (size_t x = left; x < right; x++) {
|
||||
size_t offset = x + yOffset;
|
||||
bits_[offset >> logBits] |= 1 << (offset & bitsMask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ref<BitArray> BitMatrix::getRow(int y, Ref<BitArray> row) {
|
||||
if (row.empty() || row->getSize() < width_) {
|
||||
row = new BitArray(width_);
|
||||
} else {
|
||||
row->clear();
|
||||
}
|
||||
size_t start = y * width_;
|
||||
size_t end = start + width_ - 1; // end is inclusive
|
||||
size_t firstWord = start >> logBits;
|
||||
size_t lastWord = end >> logBits;
|
||||
size_t bitOffset = start & bitsMask;
|
||||
for (size_t i = firstWord; i <= lastWord; i++) {
|
||||
size_t firstBit = i > firstWord ? 0 : start & bitsMask;
|
||||
size_t lastBit = i < lastWord ? bitsPerWord - 1 : end & bitsMask;
|
||||
unsigned int mask;
|
||||
if (firstBit == 0 && lastBit == logBits) {
|
||||
mask = std::numeric_limits<unsigned int>::max();
|
||||
} else {
|
||||
mask = 0;
|
||||
for (size_t j = firstBit; j <= lastBit; j++) {
|
||||
mask |= 1 << j;
|
||||
}
|
||||
}
|
||||
row->setBulk((i - firstWord) << logBits, (bits_[i] & mask) >> bitOffset);
|
||||
if (firstBit == 0 && bitOffset != 0) {
|
||||
unsigned int prevBulk = row->getBitArray()[i - firstWord - 1];
|
||||
prevBulk |= (bits_[i] & mask) << (bitsPerWord - bitOffset);
|
||||
row->setBulk((i - firstWord - 1) << logBits, prevBulk);
|
||||
}
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
size_t BitMatrix::getWidth() const {
|
||||
return width_;
|
||||
}
|
||||
|
||||
size_t BitMatrix::getHeight() const {
|
||||
return height_;
|
||||
}
|
||||
|
||||
size_t BitMatrix::getDimension() const {
|
||||
return width_;
|
||||
}
|
||||
|
||||
unsigned int* BitMatrix::getBits() const {
|
||||
return bits_;
|
||||
}
|
||||
|
||||
namespace zxing {
|
||||
ostream& operator<<(ostream &out, const BitMatrix &bm) {
|
||||
for (size_t y = 0; y < bm.height_; y++) {
|
||||
for (size_t x = 0; x < bm.width_; x++) {
|
||||
out << (bm.get(x, y) ? "X " : " ");
|
||||
}
|
||||
out << "\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
const char* BitMatrix::description() {
|
||||
ostringstream out;
|
||||
out << *this;
|
||||
return out.str().c_str();
|
||||
}
|
87
src/qzxing/zxing/common/BitMatrix.h
Normal file
87
src/qzxing/zxing/common/BitMatrix.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __BIT_MATRIX_H__
|
||||
#define __BIT_MATRIX_H__
|
||||
|
||||
/*
|
||||
* BitMatrix.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <limits>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class BitMatrix : public Counted {
|
||||
private:
|
||||
size_t width_;
|
||||
size_t height_;
|
||||
size_t words_;
|
||||
unsigned int* bits_;
|
||||
|
||||
#define ZX_LOG_DIGITS(digits) \
|
||||
((digits == 8) ? 3 : \
|
||||
((digits == 16) ? 4 : \
|
||||
((digits == 32) ? 5 : \
|
||||
((digits == 64) ? 6 : \
|
||||
((digits == 128) ? 7 : \
|
||||
(-1))))))
|
||||
|
||||
static const unsigned int bitsPerWord =
|
||||
std::numeric_limits<unsigned int>::digits;
|
||||
static const unsigned int logBits = ZX_LOG_DIGITS(bitsPerWord);
|
||||
static const unsigned int bitsMask = (1 << logBits) - 1;
|
||||
|
||||
public:
|
||||
BitMatrix(size_t dimension);
|
||||
BitMatrix(size_t width, size_t height);
|
||||
|
||||
~BitMatrix();
|
||||
|
||||
bool get(size_t x, size_t y) const {
|
||||
size_t offset = x + width_ * y;
|
||||
return ((bits_[offset >> logBits] >> (offset & bitsMask)) & 0x01) != 0;
|
||||
}
|
||||
|
||||
void set(size_t x, size_t y) {
|
||||
size_t offset = x + width_ * y;
|
||||
bits_[offset >> logBits] |= 1 << (offset & bitsMask);
|
||||
}
|
||||
|
||||
void flip(size_t x, size_t y);
|
||||
void clear();
|
||||
void setRegion(size_t left, size_t top, size_t width, size_t height);
|
||||
Ref<BitArray> getRow(int y, Ref<BitArray> row);
|
||||
|
||||
size_t getDimension() const;
|
||||
size_t getWidth() const;
|
||||
size_t getHeight() const;
|
||||
|
||||
unsigned int* getBits() const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream &out, const BitMatrix &bm);
|
||||
const char *description();
|
||||
|
||||
private:
|
||||
BitMatrix(const BitMatrix&);
|
||||
BitMatrix& operator =(const BitMatrix&);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __BIT_MATRIX_H__
|
74
src/qzxing/zxing/common/BitSource.cpp
Normal file
74
src/qzxing/zxing/common/BitSource.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* BitSource.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 09/05/2008.
|
||||
* Copyright 2008 Google UK. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/BitSource.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
int BitSource::readBits(int numBits) {
|
||||
if (numBits < 0 || numBits > 32) {
|
||||
throw IllegalArgumentException("cannot read <1 or >32 bits");
|
||||
} else if (numBits > available()) {
|
||||
throw IllegalArgumentException("reading more bits than are available");
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
|
||||
// First, read remainder from current byte
|
||||
if (bitOffset_ > 0) {
|
||||
int bitsLeft = 8 - bitOffset_;
|
||||
int toRead = numBits < bitsLeft ? numBits : bitsLeft;
|
||||
int bitsToNotRead = bitsLeft - toRead;
|
||||
int mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
|
||||
result = (bytes_[byteOffset_] & mask) >> bitsToNotRead;
|
||||
numBits -= toRead;
|
||||
bitOffset_ += toRead;
|
||||
if (bitOffset_ == 8) {
|
||||
bitOffset_ = 0;
|
||||
byteOffset_++;
|
||||
}
|
||||
}
|
||||
|
||||
// Next read whole bytes
|
||||
if (numBits > 0) {
|
||||
while (numBits >= 8) {
|
||||
result = (result << 8) | (bytes_[byteOffset_] & 0xFF);
|
||||
byteOffset_++;
|
||||
numBits -= 8;
|
||||
}
|
||||
|
||||
|
||||
// Finally read a partial byte
|
||||
if (numBits > 0) {
|
||||
int bitsToNotRead = 8 - numBits;
|
||||
int mask = (0xFF >> bitsToNotRead) << bitsToNotRead;
|
||||
result = (result << numBits) | ((bytes_[byteOffset_] & mask) >> bitsToNotRead);
|
||||
bitOffset_ += numBits;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int BitSource::available() {
|
||||
return 8 * (bytes_.size() - byteOffset_) - bitOffset_;
|
||||
}
|
||||
}
|
70
src/qzxing/zxing/common/BitSource.h
Normal file
70
src/qzxing/zxing/common/BitSource.h
Normal file
|
@ -0,0 +1,70 @@
|
|||
#ifndef __BIT_SOURCE_H__
|
||||
#define __BIT_SOURCE_H__
|
||||
|
||||
/*
|
||||
* BitSource.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Array.h>
|
||||
|
||||
namespace zxing {
|
||||
/**
|
||||
* <p>This provides an easy abstraction to read bits at a time from a sequence of bytes, where the
|
||||
* number of bits read is not often a multiple of 8.</p>
|
||||
*
|
||||
* <p>This class is not thread-safe.</p>
|
||||
*
|
||||
* @author srowen@google.com (Sean Owen)
|
||||
* @author christian.brunschen@gmail.com (Christian Brunschen)
|
||||
*/
|
||||
class BitSource : public Counted {
|
||||
typedef unsigned char byte;
|
||||
private:
|
||||
ArrayRef<byte> bytes_;
|
||||
int byteOffset_;
|
||||
int bitOffset_;
|
||||
public:
|
||||
/**
|
||||
* @param bytes bytes from which this will read bits. Bits will be read from the first byte first.
|
||||
* Bits are read within a byte from most-significant to least-significant bit.
|
||||
*/
|
||||
BitSource(ArrayRef<byte> &bytes) :
|
||||
bytes_(bytes), byteOffset_(0), bitOffset_(0) {
|
||||
}
|
||||
|
||||
int getByteOffset() {
|
||||
return byteOffset_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param numBits number of bits to read
|
||||
* @return int representing the bits read. The bits will appear as the least-significant
|
||||
* bits of the int
|
||||
* @throws IllegalArgumentException if numBits isn't in [1,32]
|
||||
*/
|
||||
int readBits(int numBits);
|
||||
|
||||
/**
|
||||
* @return number of bits that can be read successfully
|
||||
*/
|
||||
int available();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __BIT_SOURCE_H__
|
104
src/qzxing/zxing/common/CharacterSetECI.cpp
Normal file
104
src/qzxing/zxing/common/CharacterSetECI.cpp
Normal file
|
@ -0,0 +1,104 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* Copyright 2008-2011 ZXing authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/CharacterSetECI.h>
|
||||
#include <zxing/common/IllegalArgumentException.h>
|
||||
#include <zxing/FormatException.h>
|
||||
|
||||
using std::string;
|
||||
|
||||
using zxing::common::CharacterSetECI;
|
||||
using zxing::IllegalArgumentException;
|
||||
|
||||
std::map<int, CharacterSetECI*> CharacterSetECI::VALUE_TO_ECI;
|
||||
std::map<std::string, CharacterSetECI*> CharacterSetECI::NAME_TO_ECI;
|
||||
|
||||
const bool CharacterSetECI::inited = CharacterSetECI::init_tables();
|
||||
|
||||
#define ADD_CHARACTER_SET(VALUES, STRINGS) \
|
||||
{ static int values[] = {VALUES, -1}; \
|
||||
static char const* strings[] = {STRINGS, 0}; \
|
||||
addCharacterSet(values, strings); }
|
||||
|
||||
#define XC ,
|
||||
|
||||
bool CharacterSetECI::init_tables() {
|
||||
ADD_CHARACTER_SET(0 XC 2, "Cp437");
|
||||
ADD_CHARACTER_SET(1 XC 3, "ISO8859_1" XC "ISO-8859-1");
|
||||
ADD_CHARACTER_SET(4, "ISO8859_2" XC "ISO-8859-2");
|
||||
ADD_CHARACTER_SET(5, "ISO8859_3" XC "ISO-8859-3");
|
||||
ADD_CHARACTER_SET(6, "ISO8859_4" XC "ISO-8859-4");
|
||||
ADD_CHARACTER_SET(7, "ISO8859_5" XC "ISO-8859-5");
|
||||
ADD_CHARACTER_SET(8, "ISO8859_6" XC "ISO-8859-6");
|
||||
ADD_CHARACTER_SET(9, "ISO8859_7" XC "ISO-8859-7");
|
||||
ADD_CHARACTER_SET(10, "ISO8859_8" XC "ISO-8859-8");
|
||||
ADD_CHARACTER_SET(11, "ISO8859_9" XC "ISO-8859-9");
|
||||
ADD_CHARACTER_SET(12, "ISO8859_10" XC "ISO-8859-10");
|
||||
ADD_CHARACTER_SET(13, "ISO8859_11" XC "ISO-8859-11");
|
||||
ADD_CHARACTER_SET(15, "ISO8859_13" XC "ISO-8859-13");
|
||||
ADD_CHARACTER_SET(16, "ISO8859_14" XC "ISO-8859-14");
|
||||
ADD_CHARACTER_SET(17, "ISO8859_15" XC "ISO-8859-15");
|
||||
ADD_CHARACTER_SET(18, "ISO8859_16" XC "ISO-8859-16");
|
||||
ADD_CHARACTER_SET(20, "SJIS" XC "Shift_JIS");
|
||||
ADD_CHARACTER_SET(21, "Cp1250" XC "windows-1250");
|
||||
ADD_CHARACTER_SET(22, "Cp1251" XC "windows-1251");
|
||||
ADD_CHARACTER_SET(23, "Cp1252" XC "windows-1252");
|
||||
ADD_CHARACTER_SET(24, "Cp1256" XC "windows-1256");
|
||||
ADD_CHARACTER_SET(25, "UnicodeBigUnmarked" XC "UTF-16BE" XC "UnicodeBig");
|
||||
ADD_CHARACTER_SET(26, "UTF8" XC "UTF-8");
|
||||
ADD_CHARACTER_SET(27 XC 170, "ASCII" XC "US-ASCII");
|
||||
ADD_CHARACTER_SET(28, "Big5");
|
||||
ADD_CHARACTER_SET(29, "GB18030" XC "GB2312" XC "EUC_CN" XC "GBK");
|
||||
ADD_CHARACTER_SET(30, "EUC_KR" XC "EUC-KR");
|
||||
return true;
|
||||
}
|
||||
|
||||
#undef XC
|
||||
|
||||
CharacterSetECI::CharacterSetECI(int const* values,
|
||||
char const* const* names)
|
||||
: values_(values), names_(names) {
|
||||
for(int const* values = values_; *values != -1; values++) {
|
||||
VALUE_TO_ECI[*values] = this;
|
||||
}
|
||||
for(char const* const* names = names_; *names; names++) {
|
||||
NAME_TO_ECI[string(*names)] = this;
|
||||
}
|
||||
}
|
||||
|
||||
char const* CharacterSetECI::name() const {
|
||||
return names_[0];
|
||||
}
|
||||
|
||||
int CharacterSetECI::getValue() const {
|
||||
return values_[0];
|
||||
}
|
||||
|
||||
void CharacterSetECI::addCharacterSet(int const* values, char const* const* names) {
|
||||
new CharacterSetECI(values, names);
|
||||
}
|
||||
|
||||
CharacterSetECI* CharacterSetECI::getCharacterSetECIByValue(int value) {
|
||||
if (value < 0 || value >= 900) {
|
||||
throw FormatException();
|
||||
}
|
||||
return VALUE_TO_ECI[value];
|
||||
}
|
||||
|
||||
CharacterSetECI* CharacterSetECI::getCharacterSetECIByName(string const& name) {
|
||||
return NAME_TO_ECI[name];
|
||||
}
|
53
src/qzxing/zxing/common/CharacterSetECI.h
Normal file
53
src/qzxing/zxing/common/CharacterSetECI.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
|
||||
#ifndef __CHARACTERSET_ECI__
|
||||
#define __CHARACTERSET_ECI__
|
||||
|
||||
/*
|
||||
* Copyright 2008-2011 ZXing authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
namespace common {
|
||||
class CharacterSetECI;
|
||||
}
|
||||
}
|
||||
|
||||
class zxing::common::CharacterSetECI {
|
||||
private:
|
||||
static std::map<int, CharacterSetECI*> VALUE_TO_ECI;
|
||||
static std::map<std::string, CharacterSetECI*> NAME_TO_ECI;
|
||||
static const bool inited;
|
||||
static bool init_tables();
|
||||
|
||||
int const* const values_;
|
||||
char const* const* const names_;
|
||||
|
||||
CharacterSetECI(int const* values, char const* const* names);
|
||||
|
||||
static void addCharacterSet(int const* value, char const* const* encodingNames);
|
||||
|
||||
public:
|
||||
char const* name() const;
|
||||
int getValue() const;
|
||||
|
||||
static CharacterSetECI* getCharacterSetECIByValue(int value);
|
||||
static CharacterSetECI* getCharacterSetECIByName(std::string const& name);
|
||||
};
|
||||
|
||||
#endif
|
32
src/qzxing/zxing/common/Counted.cpp
Normal file
32
src/qzxing/zxing/common/Counted.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Counted.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 07/05/2008.
|
||||
* Copyright 2008 Google UK. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<class T>
|
||||
ostream& operator<<(ostream &out, Ref<T>& ref) {
|
||||
out << "Ref(" << (ref.object_ ? (*ref.object_) : "NULL") << ")";
|
||||
return out;
|
||||
}
|
||||
}
|
202
src/qzxing/zxing/common/Counted.h
Normal file
202
src/qzxing/zxing/common/Counted.h
Normal file
|
@ -0,0 +1,202 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __COUNTED_H__
|
||||
#define __COUNTED_H__
|
||||
|
||||
/*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//#define DEBUG_COUNTING
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifdef DEBUG_COUNTING
|
||||
#include <typeinfo>
|
||||
#endif
|
||||
|
||||
namespace zxing {
|
||||
|
||||
/* base class for reference-counted objects */
|
||||
class Counted {
|
||||
private:
|
||||
unsigned int count_;
|
||||
public:
|
||||
Counted() :
|
||||
count_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating " << typeid(*this).name() << " " << this <<
|
||||
" @ " << count_ << "\n";
|
||||
#endif
|
||||
}
|
||||
virtual ~Counted() {
|
||||
}
|
||||
Counted *retain() {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "retaining " << typeid(*this).name() << " " << this <<
|
||||
" @ " << count_;
|
||||
#endif
|
||||
count_++;
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "->" << count_ << "\n";
|
||||
#endif
|
||||
return this;
|
||||
}
|
||||
void release() {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "releasing " << typeid(*this).name() << " " << this <<
|
||||
" @ " << count_;
|
||||
#endif
|
||||
if (count_ == 0 || count_ == 54321) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "\nOverreleasing already-deleted object " << this << "!!!\n";
|
||||
#endif
|
||||
throw 4711;
|
||||
}
|
||||
count_--;
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "->" << count_ << "\n";
|
||||
#endif
|
||||
if (count_ == 0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "deleting " << typeid(*this).name() << " " << this << "\n";
|
||||
#endif
|
||||
count_ = 0xDEADF001;
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* return the current count for denugging purposes or similar */
|
||||
int count() const {
|
||||
return count_;
|
||||
}
|
||||
};
|
||||
|
||||
/* counting reference to reference-counted objects */
|
||||
template<typename T> class Ref {
|
||||
private:
|
||||
public:
|
||||
T *object_;
|
||||
explicit Ref(T *o = 0) :
|
||||
object_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating Ref " << this << " from pointer" << o << "\n";
|
||||
#endif
|
||||
reset(o);
|
||||
}
|
||||
|
||||
explicit Ref(const T &o) :
|
||||
object_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating Ref " << this << " from reference\n";
|
||||
#endif
|
||||
reset(const_cast<T *>(&o));
|
||||
}
|
||||
|
||||
Ref(const Ref &other) :
|
||||
object_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating Ref " << this << " from Ref " << &other << "\n";
|
||||
#endif
|
||||
reset(other.object_);
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
Ref(const Ref<Y> &other) :
|
||||
object_(0) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "instantiating Ref " << this << " from reference\n";
|
||||
#endif
|
||||
reset(other.object_);
|
||||
}
|
||||
|
||||
~Ref() {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "destroying Ref " << this << " with " <<
|
||||
(object_ ? typeid(*object_).name() : "NULL") << " " << object_ << "\n";
|
||||
#endif
|
||||
if (object_) {
|
||||
object_->release();
|
||||
}
|
||||
}
|
||||
|
||||
void reset(T *o) {
|
||||
#ifdef DEBUG_COUNTING
|
||||
cout << "resetting Ref " << this << " from " <<
|
||||
(object_ ? typeid(*object_).name() : "NULL") << " " << object_ <<
|
||||
" to " << (o ? typeid(*o).name() : "NULL") << " " << o << "\n";
|
||||
#endif
|
||||
if (o) {
|
||||
o->retain();
|
||||
}
|
||||
if (object_ != 0) {
|
||||
object_->release();
|
||||
}
|
||||
object_ = o;
|
||||
}
|
||||
Ref& operator=(const Ref &other) {
|
||||
reset(other.object_);
|
||||
return *this;
|
||||
}
|
||||
template<class Y>
|
||||
Ref& operator=(const Ref<Y> &other) {
|
||||
reset(other.object_);
|
||||
return *this;
|
||||
}
|
||||
Ref& operator=(T* o) {
|
||||
reset(o);
|
||||
return *this;
|
||||
}
|
||||
template<class Y>
|
||||
Ref& operator=(Y* o) {
|
||||
reset(o);
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*() {
|
||||
return *object_;
|
||||
}
|
||||
T* operator->() const {
|
||||
return object_;
|
||||
}
|
||||
operator T*() const {
|
||||
return object_;
|
||||
}
|
||||
|
||||
bool operator==(const T* that) {
|
||||
return object_ == that;
|
||||
}
|
||||
bool operator==(const Ref &other) const {
|
||||
return object_ == other.object_ || *object_ == *(other.object_);
|
||||
}
|
||||
template<class Y>
|
||||
bool operator==(const Ref<Y> &other) const {
|
||||
return object_ == other.object_ || *object_ == *(other.object_);
|
||||
}
|
||||
|
||||
bool operator!=(const T* that) {
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return object_ == 0;
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
friend std::ostream& operator<<(std::ostream &out, Ref<Y>& ref);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __COUNTED_H__
|
46
src/qzxing/zxing/common/DecoderResult.cpp
Normal file
46
src/qzxing/zxing/common/DecoderResult.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* DecoderResult.cpp
|
||||
* zxing
|
||||
*
|
||||
* Created by Christian Brunschen on 20/05/2008.
|
||||
* Copyright 2008-2011 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/DecoderResult.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace zxing;
|
||||
|
||||
DecoderResult::DecoderResult(ArrayRef<unsigned char> rawBytes,
|
||||
Ref<String> text,
|
||||
ArrayRef< ArrayRef<unsigned char> >& byteSegments,
|
||||
string const& ecLevel) :
|
||||
rawBytes_(rawBytes),
|
||||
text_(text),
|
||||
byteSegments_(byteSegments),
|
||||
ecLevel_(ecLevel) {}
|
||||
|
||||
DecoderResult::DecoderResult(ArrayRef<unsigned char> rawBytes,
|
||||
Ref<String> text)
|
||||
: rawBytes_(rawBytes), text_(text) {}
|
||||
|
||||
ArrayRef<unsigned char> DecoderResult::getRawBytes() {
|
||||
return rawBytes_;
|
||||
}
|
||||
|
||||
Ref<String> DecoderResult::getText() {
|
||||
return text_;
|
||||
}
|
51
src/qzxing/zxing/common/DecoderResult.h
Normal file
51
src/qzxing/zxing/common/DecoderResult.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#ifndef __DECODER_RESULT_H__
|
||||
#define __DECODER_RESULT_H__
|
||||
|
||||
/*
|
||||
* DecoderResult.h
|
||||
* zxing
|
||||
*
|
||||
* Copyright 2010 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <zxing/common/Counted.h>
|
||||
#include <zxing/common/Array.h>
|
||||
#include <string>
|
||||
#include <zxing/common/Str.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
class DecoderResult : public Counted {
|
||||
private:
|
||||
ArrayRef<unsigned char> rawBytes_;
|
||||
Ref<String> text_;
|
||||
ArrayRef< ArrayRef<unsigned char> > byteSegments_;
|
||||
std::string ecLevel_;
|
||||
|
||||
public:
|
||||
DecoderResult(ArrayRef<unsigned char> rawBytes,
|
||||
Ref<String> text,
|
||||
ArrayRef< ArrayRef<unsigned char> >& byteSegments,
|
||||
std::string const& ecLevel);
|
||||
|
||||
DecoderResult(ArrayRef<unsigned char> rawBytes, Ref<String> text);
|
||||
|
||||
ArrayRef<unsigned char> getRawBytes();
|
||||
Ref<String> getText();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // __DECODER_RESULT_H__
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue