Commit 62e6c7bf authored by Andrey's avatar Andrey

Print barcodes. Added example.

parent f64bd703
...@@ -53,6 +53,10 @@ void main() { ...@@ -53,6 +53,10 @@ void main() {
// Print image using an alternative (obsolette) command // Print image using an alternative (obsolette) command
// printer.printImageRaster(image); // printer.printImageRaster(image);
// Print barcode
final List<int> barData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 4];
printer.printBarcode(Barcode.upcA(barData));
printer.cut(); printer.cut();
printer.disconnect(); printer.disconnect();
}); });
......
...@@ -45,13 +45,13 @@ class BarcodeFont { ...@@ -45,13 +45,13 @@ class BarcodeFont {
const BarcodeFont._internal(this.value); const BarcodeFont._internal(this.value);
final int value; final int value;
static const fontA = BarcodeText._internal(0); static const fontA = BarcodeFont._internal(0);
static const fontB = BarcodeText._internal(1); static const fontB = BarcodeFont._internal(1);
static const fontC = BarcodeText._internal(2); static const fontC = BarcodeFont._internal(2);
static const fontD = BarcodeText._internal(3); static const fontD = BarcodeFont._internal(3);
static const fontE = BarcodeText._internal(4); static const fontE = BarcodeFont._internal(4);
static const specialA = BarcodeText._internal(97); static const specialA = BarcodeFont._internal(97);
static const specialB = BarcodeText._internal(98); static const specialB = BarcodeFont._internal(98);
} }
class Barcode { class Barcode {
...@@ -59,124 +59,130 @@ class Barcode { ...@@ -59,124 +59,130 @@ class Barcode {
/// ///
/// k = 11, 12 /// k = 11, 12
/// d = '0' – '9' /// d = '0' – '9'
Barcode.upcA(List<int> data) { Barcode.upcA(List<dynamic> barcodeData) {
_type = BarcodeType.upcA; final k = barcodeData.length;
_data = data; if (![11, 12].contains(k)) {
final k = data.length;
if (k != 11 || k != 12) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
final numeric = RegExp(r'^[0-9]$'); final numeric = RegExp(r'^[0-9]$');
final bool isDataValid = data.every((d) => numeric.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => numeric.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
_type = BarcodeType.upcA;
_data = _convertData(barcodeData);
} }
/// UPC-E /// UPC-E
/// ///
/// k = 6 – 8, 11, 12 /// k = 6 – 8, 11, 12
/// d = '0' – '9' (However, d0 = '0' when k = 7, 8, 11, 12) /// d = '0' – '9' (However, d0 = '0' when k = 7, 8, 11, 12)
Barcode.upcE(List<int> data) { Barcode.upcE(List<dynamic> barcodeData) {
_type = BarcodeType.upcE; final k = barcodeData.length;
_data = data;
final k = data.length;
if (![6, 7, 8, 11, 12].contains(k)) { if (![6, 7, 8, 11, 12].contains(k)) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
if ([7, 8, 11, 12].contains(k) && data[0].toString() != '0') { if ([7, 8, 11, 12].contains(k) && barcodeData[0].toString() != '0') {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
final numeric = RegExp(r'^[0-9]$'); final numeric = RegExp(r'^[0-9]$');
final bool isDataValid = data.every((d) => numeric.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => numeric.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
_type = BarcodeType.upcE;
_data = _convertData(barcodeData);
} }
/// JAN13 (EAN13) /// JAN13 (EAN13)
/// ///
/// k = 12, 13 /// k = 12, 13
/// d = '0' – '9' /// d = '0' – '9'
Barcode.ean13(List<int> data) { Barcode.ean13(List<dynamic> barcodeData) {
_type = BarcodeType.ean13; final k = barcodeData.length;
_data = data; if (![12, 13].contains(k)) {
final k = data.length;
if (k != 12 || k != 13) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
final numeric = RegExp(r'^[0-9]$'); final numeric = RegExp(r'^[0-9]$');
final bool isDataValid = data.every((d) => numeric.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => numeric.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
_type = BarcodeType.ean13;
_data = _convertData(barcodeData);
} }
/// JAN8 (EAN8) /// JAN8 (EAN8)
/// ///
/// k = 7, 8 /// k = 7, 8
/// d = '0' – '9' /// d = '0' – '9'
Barcode.ean8(List<int> data) { Barcode.ean8(List<dynamic> barcodeData) {
_type = BarcodeType.ean8; final k = barcodeData.length;
_data = data; if (![7, 8].contains(k)) {
final k = data.length;
if (k != 7 || k != 8) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
final numeric = RegExp(r'^[0-9]$'); final numeric = RegExp(r'^[0-9]$');
final bool isDataValid = data.every((d) => numeric.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => numeric.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
_type = BarcodeType.ean8;
_data = _convertData(barcodeData);
} }
/// CODE39 /// CODE39
/// ///
/// k >= 1 /// k >= 1
/// d: '0'–'9', A–Z, SP, $, %, *, +, -, ., / /// d: '0'–'9', A–Z, SP, $, %, *, +, -, ., /
Barcode.code39(List<int> data) { Barcode.code39(List<dynamic> barcodeData) {
_type = BarcodeType.code39; final k = barcodeData.length;
_data = data;
final k = data.length;
if (k < 1) { if (k < 1) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
final regex = RegExp(r'^[0-9A-Z \$\%\*\+\-\.\/]$'); final regex = RegExp(r'^[0-9A-Z \$\%\*\+\-\.\/]$');
final bool isDataValid = data.every((d) => regex.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => regex.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
_type = BarcodeType.code39;
_data = _convertData(barcodeData);
} }
/// ITF (Interleaved 2 of 5) /// ITF (Interleaved 2 of 5)
/// ///
/// k >= 2 (even number) /// k >= 2 (even number)
/// d = '0'–'9' /// d = '0'–'9'
Barcode.itf(List<int> data) { Barcode.itf(List<dynamic> barcodeData) {
_type = BarcodeType.itf; final k = barcodeData.length;
_data = data;
final k = data.length;
if (k < 2 || !k.isEven) { if (k < 2 || !k.isEven) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
final numeric = RegExp(r'^[0-9]$'); final numeric = RegExp(r'^[0-9]$');
final bool isDataValid = data.every((d) => numeric.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => numeric.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
_type = BarcodeType.itf;
_data = _convertData(barcodeData);
} }
/// CODABAR (NW-7) /// CODABAR (NW-7)
...@@ -184,36 +190,44 @@ class Barcode { ...@@ -184,36 +190,44 @@ class Barcode {
/// k >= 2 /// k >= 2
/// d: '0'–'9', A–D, a–d, $, +, −, ., /, : /// d: '0'–'9', A–D, a–d, $, +, −, ., /, :
/// However, d0 = A–D, dk = A–D (65-68) /// However, d0 = A–D, dk = A–D (65-68)
/// d1 = a-d, dk = a-d (97-100) /// d0 = a-d, dk = a-d (97-100)
Barcode.codabar(List<int> data) { Barcode.codabar(List<dynamic> barcodeData) {
_type = BarcodeType.codabar; final k = barcodeData.length;
_data = data;
final k = data.length;
if (k < 2) { if (k < 2) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
final regex = RegExp(r'^[0-9A-Da-d\$\+\-\.\/\:]$'); final regex = RegExp(r'^[0-9A-Da-d\$\+\-\.\/\:]$');
final bool isDataValid = data.every((d) => regex.hasMatch(d.toString())); final bool isDataValid =
barcodeData.every((dynamic d) => regex.hasMatch(d.toString()));
if (!isDataValid) { if (!isDataValid) {
throw Exception('Barcode: Data is not valid'); throw Exception('Barcode: Data is not valid');
} }
if (!((data[0] >= 65 && data[0] <= 68) && if ((_charcode(barcodeData[0]) >= 65 && _charcode(barcodeData[0]) <= 68) &&
(data[k - 1] >= 65 && data[k - 1] <= 68))) { !(_charcode(barcodeData[k - 1]) >= 65 &&
_charcode(barcodeData[k - 1]) <= 68)) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
if (!((data[0] >= 97 && data[0] <= 100) && if ((_charcode(barcodeData[0]) >= 97 && _charcode(barcodeData[0]) <= 100) &&
(data[k - 1] >= 97 && data[k - 1] <= 100))) { !(_charcode(barcodeData[k - 1]) >= 97 &&
_charcode(barcodeData[k - 1]) <= 100)) {
throw Exception('Barcode: Wrong data range'); throw Exception('Barcode: Wrong data range');
} }
_type = BarcodeType.codabar;
_data = _convertData(barcodeData);
} }
BarcodeType _type; BarcodeType _type;
List<int> _data; List<int> _data;
List<int> _convertData(List<dynamic> list) =>
list.map((dynamic d) => d.toString().codeUnitAt(0)).toList();
int _charcode(dynamic ch) => ch.toString().codeUnitAt(0);
BarcodeType get type => _type; BarcodeType get type => _type;
List<int> get data => _data.map<int>((d) => d.toString().codeUnitAt(0)); List<int> get data => _data;
} }
...@@ -471,11 +471,11 @@ class Printer { ...@@ -471,11 +471,11 @@ class Printer {
} }
// Set width // Set width
if (width >= 0) { if (width != null && width >= 0) {
sendRaw(cBarcodeSetW.codeUnits + [width]); sendRaw(cBarcodeSetW.codeUnits + [width]);
} }
// Set height // Set height
if (height >= 1 && height <= 255) { if (height != null && height >= 1 && height <= 255) {
sendRaw(cBarcodeSetH.codeUnits + [height]); sendRaw(cBarcodeSetH.codeUnits + [height]);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment