Skip to content

Commit

Permalink
https://github.com/andrey-ushakov/esc_pos_utils/pull/110
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Rudnei de Souza committed May 24, 2023
1 parent e0c06fe commit 517e881
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 65 deletions.
79 changes: 68 additions & 11 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import 'dart:convert';
import 'dart:typed_data' show Uint8List;

import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:gbk_codec/gbk_codec.dart';
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 'commands.dart';

class Generator {
Expand Down Expand Up @@ -69,10 +70,9 @@ class Generator {
// replace some non-ascii characters
text = text
.replaceAll("’", "'")
.replaceAll("‘", "'")
.replaceAll("´", "'")
.replaceAll("»", '"')
.replaceAll(" ", ' ')
.replaceAll(" ", ' ')
.replaceAll("•", '.');
if (!isKanji) {
return latin1.encode(text);
Expand Down Expand Up @@ -146,23 +146,80 @@ 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);

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 bytes = imgBinary.getBytes();
blobs.add(bytes);
left += lineHeight;
}

return blobs;
}

/// Draw the image [src] onto the image [dst].
///
/// In other words, drawImage will take an rectangular area from src of
/// width [src_w] and height [src_h] at position ([src_x],[src_y]) and place it
/// in a rectangular area of [dst] of width [dst_w] and height [dst_h] at
/// position ([dst_x],[dst_y]).
///
/// If the source and destination coordinates and width and heights differ,
/// appropriate stretching or shrinking of the image fragment will be performed.
/// The coordinates refer to the upper left corner. This function can be used to
/// copy regions within the same image (if [dst] is the same as [src])
/// but if the regions overlap the results will be unpredictable.
Image drawImage(Image dst, Image src,
{int? dstX,
int? dstY,
int? dstW,
int? dstH,
int? srcX,
int? srcY,
int? srcW,
int? srcH,
bool blend = true}) {
dstX ??= 0;
dstY ??= 0;
srcX ??= 0;
srcY ??= 0;
srcW ??= src.width;
srcH ??= src.height;
dstW ??= (dst.width < src.width) ? dstW = dst.width : src.width;
dstH ??= (dst.height < src.height) ? dst.height : src.height;

if (blend) {
for (var y = 0; y < dstH; ++y) {
for (var x = 0; x < dstW; ++x) {
final stepX = (x * (srcW / dstW)).toInt();
final stepY = (y * (srcH / dstH)).toInt();
final srcPixel = src.getPixel(srcX + stepX, srcY + stepY);
drawPixel(dst, dstX + x, dstY + y, srcPixel);
}
}
} else {
for (var y = 0; y < dstH; ++y) {
for (var x = 0; x < dstW; ++x) {
final stepX = (x * (srcW / dstW)).toInt();
final stepY = (y * (srcH / dstH)).toInt();
final srcPixel = src.getPixel(srcX + stepX, srcY + stepY);
dst.setPixel(dstX + x, dstY + y, srcPixel);
}
}
}

return dst;
}

/// Image rasterization
List<int> _toRasterFormat(Image imgSrc) {
final Image image = Image.from(imgSrc); // make a copy
Expand All @@ -174,7 +231,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 @@ -578,8 +635,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
Loading

0 comments on commit 517e881

Please sign in to comment.