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

image version upgraded #106

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:image/image.dart';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:esc_pos_utils_plus/esc_pos_utils.dart';

Future<void> main() async {
final profile = await CapabilityProfile.load();
Expand Down
4 changes: 2 additions & 2 deletions lib/src/capability_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CapabilityProfile {
/// Public factory
static Future<CapabilityProfile> load({String name = 'default'}) async {
final content = await rootBundle
.loadString('packages/esc_pos_utils/resources/capabilities.json');
.loadString('packages/esc_pos_utils_plus/resources/capabilities.json');
Map capabilities = json.decode(content);

var profile = capabilities['profiles'][name];
Expand Down Expand Up @@ -57,7 +57,7 @@ class CapabilityProfile {

static Future<List<dynamic>> getAvailableProfiles() async {
final content = await rootBundle
.loadString('packages/esc_pos_utils/resources/capabilities.json');
.loadString('packages/esc_pos_utils_plus/resources/capabilities.json');
Map capabilities = json.decode(content);

var profiles = capabilities['profiles'];
Expand Down
116 changes: 59 additions & 57 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import 'dart:typed_data' show Uint8List;
import 'package:hex/hex.dart';
import 'package:image/image.dart';
import 'package:gbk_codec/gbk_codec.dart';
import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'enums.dart';
import 'package:esc_pos_utils_plus/esc_pos_utils.dart';
import 'commands.dart';

class Generator {
Expand Down Expand Up @@ -145,16 +144,19 @@ class Generator {

// Create a black bottom layer
final biggerImage = copyResize(image, width: widthPx, height: heightPx);
fill(biggerImage, 0);
fill(biggerImage, color: ColorRgb8(0, 0, 0));
// Insert source image into bigger one
drawImage(biggerImage, image, dstX: 0, dstY: 0);
compositeImage(biggerImage, image, dstX: 0, dstY: 0);

int left = 0;
final List<List<int>> blobs = [];

while (left < widthPx) {
final Image slice = copyCrop(biggerImage, left, 0, lineHeight, heightPx);
final Uint8List bytes = slice.getBytes(format: Format.luminance);
final Image slice = copyCrop(biggerImage,
x: left, y: 0, width: lineHeight, height: heightPx);
grayscale(slice);
final imgBinary = slice.convert(numChannels: 1);
final Uint8List bytes = imgBinary.getBytes();
blobs.add(bytes);
left += lineHeight;
}
Expand All @@ -173,7 +175,7 @@ class Generator {

// R/G/B channels are same -> keep only one channel
final List<int> oneChannelBytes = [];
final List<int> buffer = image.getBytes(format: Format.rgba);
final List<int> buffer = image.getBytes(order: ChannelOrder.rgba);
for (int i = 0; i < buffer.length; i += 4) {
oneChannelBytes.add(buffer[i]);
}
Expand Down Expand Up @@ -215,7 +217,7 @@ class Generator {
/// Replaces a single bit in a 32-bit unsigned integer.
int _transformUint32Bool(int uint32, int shift, bool newValue) {
return ((0xFFFFFFFF ^ (0x1 << shift)) & uint32) |
((newValue ? 1 : 0) << shift);
((newValue ? 1 : 0) << shift);
}
// ************************ (end) Internal helpers ************************

Expand Down Expand Up @@ -280,7 +282,7 @@ class Generator {
}
if (styles.underline != _styles.underline) {
bytes +=
styles.underline ? cUnderline1dot.codeUnits : cUnderlineOff.codeUnits;
styles.underline ? cUnderline1dot.codeUnits : cUnderlineOff.codeUnits;
_styles = _styles.copyWith(underline: styles.underline);
}

Expand Down Expand Up @@ -342,12 +344,12 @@ class Generator {
}

List<int> text(
String text, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
bool containsChinese = false,
int? maxCharsPerLine,
}) {
String text, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
bool containsChinese = false,
int? maxCharsPerLine,
}) {
List<int> bytes = [];
if (!containsChinese) {
bytes += _text(
Expand Down Expand Up @@ -470,7 +472,7 @@ class Generator {

for (int i = 0; i < cols.length; ++i) {
int colInd =
cols.sublist(0, i).fold(0, (int sum, col) => sum + col.width);
cols.sublist(0, i).fold(0, (int sum, col) => sum + col.width);
double charWidth = _getCharWidth(cols[i].styles);
double fromPos = _colIndToPosition(colInd);
final double toPos =
Expand All @@ -488,7 +490,7 @@ class Generator {
if (realCharactersNb > maxCharactersNb) {
// Print max possible and split to the next row
Uint8List encodedToPrintNextRow =
encodedToPrint.sublist(maxCharactersNb);
encodedToPrint.sublist(maxCharactersNb);
encodedToPrint = encodedToPrint.sublist(0, maxCharactersNb);
isNextRow = true;
nextRow.add(PosColumn(
Expand Down Expand Up @@ -577,8 +579,8 @@ class Generator {
const bool highDensityVertical = true;

invert(image);
flip(image, Flip.horizontal);
final Image imageRotated = copyRotate(image, 270);
flip(image, direction: FlipDirection.horizontal);
final Image imageRotated = copyRotate(image, angle: 270);

const int lineHeight = highDensityVertical ? 3 : 1;
final List<List<int>> blobs = _toColumnFormat(imageRotated, lineHeight * 8);
Expand Down Expand Up @@ -615,12 +617,12 @@ class Generator {
///
/// [image] is an instanse of class from [Image library](https://pub.dev/packages/image)
List<int> imageRaster(
Image image, {
PosAlign align = PosAlign.center,
bool highDensityHorizontal = true,
bool highDensityVertical = true,
PosImageFn imageFn = PosImageFn.bitImageRaster,
}) {
Image image, {
PosAlign align = PosAlign.center,
bool highDensityHorizontal = true,
bool highDensityVertical = true,
PosImageFn imageFn = PosImageFn.bitImageRaster,
}) {
List<int> bytes = [];
// Image alignment
bytes += setStyles(PosStyles().copyWith(align: align));
Expand Down Expand Up @@ -666,13 +668,13 @@ class Generator {
/// [height] range: 1 - 255. The units depend on the printer model.
/// Width, height, font, text position settings are effective until performing of ESC @, reset or power-off.
List<int> barcode(
Barcode barcode, {
int? width,
int? height,
BarcodeFont? font,
BarcodeText textPos = BarcodeText.below,
PosAlign align = PosAlign.center,
}) {
Barcode barcode, {
int? width,
int? height,
BarcodeFont? font,
BarcodeText textPos = BarcodeText.below,
PosAlign align = PosAlign.center,
}) {
List<int> bytes = [];
// Set alignment
bytes += setStyles(PosStyles().copyWith(align: align));
Expand Down Expand Up @@ -708,11 +710,11 @@ class Generator {

/// Print a QR Code
List<int> qrcode(
String text, {
PosAlign align = PosAlign.center,
QRSize size = QRSize.Size4,
QRCorrection cor = QRCorrection.L,
}) {
String text, {
PosAlign align = PosAlign.center,
QRSize size = QRSize.Size4,
QRCorrection cor = QRCorrection.L,
}) {
List<int> bytes = [];
// Set alignment
bytes += setStyles(PosStyles().copyWith(align: align));
Expand Down Expand Up @@ -743,11 +745,11 @@ class Generator {
}

List<int> textEncoded(
Uint8List textBytes, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
int? maxCharsPerLine,
}) {
Uint8List textBytes, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
int? maxCharsPerLine,
}) {
List<int> bytes = [];
bytes += _text(textBytes, styles: styles, maxCharsPerLine: maxCharsPerLine);
// Ensure at least one line break after the text
Expand All @@ -761,17 +763,17 @@ class Generator {
///
/// [colInd] range: 0..11. If null: do not define the position
List<int> _text(
Uint8List textBytes, {
PosStyles styles = const PosStyles(),
int? colInd = 0,
bool isKanji = false,
int colWidth = 12,
int? maxCharsPerLine,
}) {
Uint8List textBytes, {
PosStyles styles = const PosStyles(),
int? colInd = 0,
bool isKanji = false,
int colWidth = 12,
int? maxCharsPerLine,
}) {
List<int> bytes = [];
if (colInd != null) {
double charWidth =
_getCharWidth(styles, maxCharsPerLine: maxCharsPerLine);
_getCharWidth(styles, maxCharsPerLine: maxCharsPerLine);
double fromPos = _colIndToPosition(colInd);

// Align
Expand Down Expand Up @@ -808,11 +810,11 @@ class Generator {

/// Prints one line of styled mixed (chinese and latin symbols) text
List<int> _mixedKanji(
String text, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
int? maxCharsPerLine,
}) {
String text, {
PosStyles styles = const PosStyles(),
int linesAfter = 0,
int? maxCharsPerLine,
}) {
List<int> bytes = [];
final list = _getLexemes(text);
final List<String> lexemes = list[0];
Expand All @@ -835,5 +837,5 @@ class Generator {
bytes += emptyLines(linesAfter + 1);
return bytes;
}
// ************************ (end) Internal command generators ************************
}
// ************************ (end) Internal command generators ************************
}
2 changes: 1 addition & 1 deletion lib/src/qrcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* See LICENSE for distribution and usage details.
*/

import 'package:esc_pos_utils/src/commands.dart';
import 'package:esc_pos_utils_plus/src/commands.dart';
import 'dart:convert';

class QRSize {
Expand Down
Loading