Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QR code support for some Printers, and fix bug when split kanji sentence #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/esc_pos_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export './src/enums.dart';
export './src/pos_column.dart';
export './src/pos_styles.dart';
export './src/ticket.dart';
export './src/qrcode.dart';
43 changes: 43 additions & 0 deletions lib/src/qrcode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import 'package:gbk_codec/gbk_codec.dart';

import 'enums.dart';

class QRSize {
const QRSize._internal(this.value);
final List<int> value;

static const Zero4 = QRSize._internal([0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x04]);

static const Zero5 = QRSize._internal([0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05]);

static const Zero6 = QRSize._internal([0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x06]);

static const Zero7 = QRSize._internal([0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x07]);

static const Zero8 = QRSize._internal([0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x08]);

}


class QRCode {
List<int> _data = <int>[];
List<int> get data => _data;
List<int> _gbkText;

QRCode(QRSize size, PosAlign align, String text){
_data += size.value;
_data += [0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x45, 0x30];
_gbkText = gbk_bytes.encode(text);
// 设置数据长度
_data += [0x1d, 0x28, 0x6b, _gbkText.length + 3, 0x00, 0x31,0x50,0x30];
_data += _gbkText;

// 设置二维码位置
_data += [0x1b ,0x61 ,align.index];
_data += [0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x52, 0x30];
_data += [0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30];

}

}
20 changes: 17 additions & 3 deletions lib/src/ticket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import 'dart:convert';
import 'dart:typed_data' show Uint8List;
import 'package:esc_pos_utils/src/qrcode.dart';
import 'package:gbk_codec/gbk_codec.dart';
import 'package:hex/hex.dart';
import 'package:image/image.dart';
Expand Down Expand Up @@ -156,6 +157,7 @@ class Ticket {
bool isKanji = false,
int colWidth = 12,
int maxCharsPerLine,
bool inline
}) {
// Calculate maxCharsPerLine
int charsPerLine;
Expand Down Expand Up @@ -194,9 +196,11 @@ class Ticket {
final hexPair = HEX.decode(hexStr);

// Position
bytes += Uint8List.fromList(
List.from(cPos.codeUnits)..addAll([hexPair[1], hexPair[0]]),
);
if(!inline){
bytes += Uint8List.fromList(
List.from(cPos.codeUnits)..addAll([hexPair[1], hexPair[0]]),
);
}

setStyles(styles, isKanji: isKanji);

Expand Down Expand Up @@ -291,6 +295,7 @@ class Ticket {
styles: styles,
isKanji: isLexemeChinese[i],
maxCharsPerLine: maxCharsPerLine,
inline: i>0
);
}

Expand Down Expand Up @@ -352,6 +357,7 @@ class Ticket {
colInd: colInd,
colWidth: cols[i].width,
isKanji: isLexemeChinese[j],
inline: j>0
);
}
}
Expand Down Expand Up @@ -628,6 +634,14 @@ class Ticket {
}
}

void qrcode(
QRCode code
) {
// 固定头部,表明下面我要开始发功了
bytes += [0x1b, 0x40];
bytes += code.data;
}

/// Print a barcode
///
/// [width] range and units are different depending on the printer model (some printers use 1..5).
Expand Down
216 changes: 216 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.5"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.2"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.3"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.16.1"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
gbk_codec:
dependency: "direct main"
description:
name: gbk_codec
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.3.2"
hex:
dependency: "direct main"
description:
name: hex
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.2"
html:
dependency: transitive
description:
name: html
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.14.0+3"
image:
dependency: "direct main"
description:
name: image
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.8"
path:
dependency: transitive
description:
name: path
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.4.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.5"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.9.3"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.5"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.11"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.6"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.5.0"
sdks:
dart: ">=2.4.0 <3.0.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: esc_pos_utils
description: Basic Flutter/Dart classes for ESC/POS printing. Ticket class generates ESC/POS commands that can be sent to a thermal printer.
version: 0.3.5
version: 0.3.7
homepage: https://github.com/andrey-ushakov/esc_pos_utils

environment:
Expand Down