88
99import 'dart:convert' ;
1010import 'dart:typed_data' show Uint8List;
11+
12+ import 'package:esc_pos_utils/esc_pos_utils.dart' ;
13+ import 'package:gbk_codec/gbk_codec.dart' ;
1114import 'package:hex/hex.dart' ;
1215import 'package:image/image.dart' ;
13- import 'package:gbk_codec/gbk_codec.dart' ;
14- import 'package:esc_pos_utils/esc_pos_utils.dart' ;
15- import 'enums.dart' ;
16+
1617import 'commands.dart' ;
1718
1819class Generator {
@@ -69,10 +70,9 @@ class Generator {
6970 // replace some non-ascii characters
7071 text = text
7172 .replaceAll ("’" , "'" )
72- .replaceAll ("‘" , "'" )
7373 .replaceAll ("´" , "'" )
7474 .replaceAll ("»" , '"' )
75- .replaceAll (" " , ' ' )
75+ .replaceAll (" " , ' ' )
7676 .replaceAll ("•" , '.' );
7777 if (! isKanji) {
7878 return latin1.encode (text);
@@ -146,23 +146,80 @@ class Generator {
146146
147147 // Create a black bottom layer
148148 final biggerImage = copyResize (image, width: widthPx, height: heightPx);
149- fill (biggerImage, 0 );
149+ fill (biggerImage, color : ColorRgb8 ( 0 , 0 , 0 ) );
150150 // Insert source image into bigger one
151151 drawImage (biggerImage, image, dstX: 0 , dstY: 0 );
152152
153153 int left = 0 ;
154154 final List <List <int >> blobs = [];
155155
156156 while (left < widthPx) {
157- final Image slice = copyCrop (biggerImage, left, 0 , lineHeight, heightPx);
158- final Uint8List bytes = slice.getBytes (format: Format .luminance);
157+ final Image slice = copyCrop (biggerImage,
158+ x: left, y: 0 , width: lineHeight, height: heightPx);
159+ grayscale (slice);
160+ final imgBinary = slice.convert (numChannels: 1 );
161+ final bytes = imgBinary.getBytes ();
159162 blobs.add (bytes);
160163 left += lineHeight;
161164 }
162165
163166 return blobs;
164167 }
165168
169+ /// Draw the image [src] onto the image [dst] .
170+ ///
171+ /// In other words, drawImage will take an rectangular area from src of
172+ /// width [src_w] and height [src_h] at position ([src_x] ,[src_y] ) and place it
173+ /// in a rectangular area of [dst] of width [dst_w] and height [dst_h] at
174+ /// position ([dst_x] ,[dst_y] ).
175+ ///
176+ /// If the source and destination coordinates and width and heights differ,
177+ /// appropriate stretching or shrinking of the image fragment will be performed.
178+ /// The coordinates refer to the upper left corner. This function can be used to
179+ /// copy regions within the same image (if [dst] is the same as [src] )
180+ /// but if the regions overlap the results will be unpredictable.
181+ Image drawImage (Image dst, Image src,
182+ {int ? dstX,
183+ int ? dstY,
184+ int ? dstW,
185+ int ? dstH,
186+ int ? srcX,
187+ int ? srcY,
188+ int ? srcW,
189+ int ? srcH,
190+ bool blend = true }) {
191+ dstX ?? = 0 ;
192+ dstY ?? = 0 ;
193+ srcX ?? = 0 ;
194+ srcY ?? = 0 ;
195+ srcW ?? = src.width;
196+ srcH ?? = src.height;
197+ dstW ?? = (dst.width < src.width) ? dstW = dst.width : src.width;
198+ dstH ?? = (dst.height < src.height) ? dst.height : src.height;
199+
200+ if (blend) {
201+ for (var y = 0 ; y < dstH; ++ y) {
202+ for (var x = 0 ; x < dstW; ++ x) {
203+ final stepX = (x * (srcW / dstW)).toInt ();
204+ final stepY = (y * (srcH / dstH)).toInt ();
205+ final srcPixel = src.getPixel (srcX + stepX, srcY + stepY);
206+ drawPixel (dst, dstX + x, dstY + y, srcPixel);
207+ }
208+ }
209+ } else {
210+ for (var y = 0 ; y < dstH; ++ y) {
211+ for (var x = 0 ; x < dstW; ++ x) {
212+ final stepX = (x * (srcW / dstW)).toInt ();
213+ final stepY = (y * (srcH / dstH)).toInt ();
214+ final srcPixel = src.getPixel (srcX + stepX, srcY + stepY);
215+ dst.setPixel (dstX + x, dstY + y, srcPixel);
216+ }
217+ }
218+ }
219+
220+ return dst;
221+ }
222+
166223 /// Image rasterization
167224 List <int > _toRasterFormat (Image imgSrc) {
168225 final Image image = Image .from (imgSrc); // make a copy
@@ -174,7 +231,7 @@ class Generator {
174231
175232 // R/G/B channels are same -> keep only one channel
176233 final List <int > oneChannelBytes = [];
177- final List <int > buffer = image.getBytes (format : Format .rgba);
234+ final List <int > buffer = image.getBytes (order : ChannelOrder .rgba);
178235 for (int i = 0 ; i < buffer.length; i += 4 ) {
179236 oneChannelBytes.add (buffer[i]);
180237 }
@@ -578,8 +635,8 @@ class Generator {
578635 const bool highDensityVertical = true ;
579636
580637 invert (image);
581- flip (image, Flip .horizontal);
582- final Image imageRotated = copyRotate (image, 270 );
638+ flip (image, direction : FlipDirection .horizontal);
639+ final Image imageRotated = copyRotate (image, angle : 270 );
583640
584641 const int lineHeight = highDensityVertical ? 3 : 1 ;
585642 final List <List <int >> blobs = _toColumnFormat (imageRotated, lineHeight * 8 );
0 commit comments