tag:blogger.com,1999:blog-2138874972589776570.post2300235705146839649..comments2023-10-12T08:34:08.322+02:00Comments on New Grumpy Mentat: Java ESC/POS Image PrintingGustavo De Michelihttp://www.blogger.com/profile/03094689119679861823noreply@blogger.comBlogger63125tag:blogger.com,1999:blog-2138874972589776570.post-34078375393441694102023-07-24T17:20:17.271+02:002023-07-24T17:20:17.271+02:00great! you "saved my life " :-) thank yo...great! you "saved my life " :-) thank you it works perfectlyAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-5164836425117492582023-07-12T17:02:39.570+02:002023-07-12T17:02:39.570+02:00This looks really interesting, but it looks like t...This looks really interesting, but it looks like the source code has disappeared? I get a 404.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-12579511458252960392023-04-01T20:41:09.779+02:002023-04-01T20:41:09.779+02:00Hello, My kotlin version for Android. I had to add...Hello, My kotlin version for Android. I had to add a delay after each line or I got strange results. Probably the printer does not process the line on time and therefore I had to add a delay (personally I try to avoid this but no other solution)<br /><br /> fun printImage(bitmap: Bitmap?){<br /> if(bitmap != null){<br /> val pixels = getPixelsSlow(bitmap)<br /> printImage(pixels, bitmap.width, bitmap.height)<br /><br /> }<br /> }<br /><br /> fun printImage(pixels: Array,width: Int,height: Int) {<br /> // Set the line spacing at 24 (we'll print 24 dots high)<br /> _outputStream.write(SET_LINE_SPACE_24)<br />// var y = 0<br /> for(y in pixels.indices step 24){<br />// while (y < pixels.size) {<br /><br /> // Like I said before, when done sending data,<br /> // the printer will resume to normal text printing<br /> _outputStream.write(selectBitImageMode)<br /> // Set nL and nH based on the width of the image<br /> _outputStream.write(<br /> byteArrayOf(<br /> (0x00ff and pixels[y].size).toByte(),<br /> (0xff00 and pixels[y].size shr 8).toByte()<br /> )<br /> )<br /><br /> for (x in 0 until pixels[y].size)<br /> {<br /> // for each stripe, recollect 3 bytes (3 bytes = 24 bits)<br /> val colectedBytes = recollectSlice(y, x, pixels)<br /> _outputStream.write(colectedBytes)<br /> // Log.d("QR","y: $y x: $x Values: ${colectedBytes.toString()}")<br /> }<br /><br /><br /><br /> _outputStream.write(escPosLFCode)<br />// _outputStream.flush()<br /> Thread.sleep(200)<br /> }<br /> _outputStream.write(SET_LINE_SPACE_30)<br /> }<br /><br /> private fun recollectSlice(y: Int, x: Int, img: Array): ByteArray? {<br /> val slices = byteArrayOf(0, 0, 0)<br /> var yy = y<br /> var i = 0<br /> while (yy < y + 24 && i < 3) {<br /> var slice: Byte = 0<br /> for (b in 0..7) {<br /> val yyy = yy + b<br /> if (yyy >= img.size) {<br /> continue<br /> }<br /> val col = img[yyy][x]<br /> val v = shouldPrintColor(col)<br /> slice = (slice.toInt() or ((if (v) 1 else 0) shl 7 - b).toByte().toInt()).toByte()<br /> }<br /> slices[i] = slice<br /> yy += 8<br /> i++<br /> }<br /> return slices<br /> }<br /><br /><br /> private fun shouldPrintColor(col: Int): Boolean {<br /> val threshold = 127<br /> val a: Int<br /> val r: Int<br /> val g: Int<br /> val b: Int<br /> val luminance: Int<br /> a = col shr 24 and 0xff<br /> if (a != 0xff) { // Ignore transparencies<br /> return false<br /> }<br /> r = col shr 16 and 0xff<br /> g = col shr 8 and 0xff<br /> b = col and 0xff<br /> luminance = (0.299 * r + 0.587 * g + 0.114 * b).toInt()<br /> return luminance < threshold<br /> }<br /><br /> fun getPixelsSlow(image: Bitmap): Array {<br /> val width = image.width<br /> val height = image.height<br /> val result = Array(height) { IntArray(width) }<br /> for (row in 0 until height) {<br /> for (col in 0 until width) {<br /> result[row][col] = getRGB(image, col, row)<br /> }<br /> }<br /> return result<br /> }<br /><br /> private fun getRGB(bmpOriginal: Bitmap, col: Int, row: Int): Int {<br /> // get one pixel color<br /> val pixel = bmpOriginal.getPixel(col, row)<br /> // retrieve color of all channels<br /> val R: Int = Color.red(pixel)<br /> val G: Int = Color.green(pixel)<br /> val B: Int = Color.blue(pixel)<br /> return Color.rgb(R, G, B)<br /> }<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-33802767358165150372021-09-27T12:29:56.590+02:002021-09-27T12:29:56.590+02:00Hi, I'm looking for help doing the opposite fl...Hi, I'm looking for help doing the opposite flow - I have the binary data sent to printer which represents an image (saved in binary file) and I need to extract the bitmap image from this data.<br />Any help would be gratefully accepted/Anonymoushttps://www.blogger.com/profile/08528811660837060624noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-47191218492153189342021-09-09T22:35:31.529+02:002021-09-09T22:35:31.529+02:00this was happening with me. the problem was that I...this was happening with me. the problem was that I was sending decimal data instead of hex in the image dimensions... when the printer ended printing with the wrong dimensions I sent to it, it printed the rest of the image data as normal data and the output was some strange characters.GABRIEL ESTEVEShttps://www.blogger.com/profile/04347474428040350062noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-83710320331802658752021-09-09T22:29:06.919+02:002021-09-09T22:29:06.919+02:00Man this article was so much helpful. I've bee...Man this article was so much helpful. I've been searching manuals and stuff through the web and found nothing that I could understand and so many people with the same doubts as me. the funny thing is that your article was way down in the google results. and I almost ignored the link to your article. but then I felt the urge to click and I got the feeling that I would finding just what I wanted here and voilà! GABRIEL ESTEVEShttps://www.blogger.com/profile/04347474428040350062noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-2343398215941230352021-02-19T19:26:14.734+01:002021-02-19T19:26:14.734+01:00Hello,
I try to print a logo on the paper, now i ...Hello,<br /><br />I try to print a logo on the paper, now i did get the example working. I want to print the logo of 300w on the paper with the attatched code. I have build my binary array with LCD image converter. I got a black bar on top and than characters. <br /><br />I work with a TTGO (arduino)<br /><br />Can you help me out?<br /><br /> writeByte(27);<br /> writeByte(42);<br /> writeByte(0); // dot densety<br /> writeByte(44); // <br /> writeByte(1);<br /> // data<br /> for (int i = 0; i < sizeof(digikasPrintLogo)/sizeof(digikasPrintLogo[0]); i++)<br /> {<br /> //Serial.println(digikasPrintLogo[i]);<br /> writeByte(digikasPrintLogo[i]);<br /> }christophe195https://www.blogger.com/profile/00232776753268834656noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-87657831826630822902020-11-25T02:50:00.522+01:002020-11-25T02:50:00.522+01:00public Bitmap resize(Bitmap bitmap) {
///... public Bitmap resize(Bitmap bitmap) { <br /> ////// scale image to fix image ratio => 512 / (int) Math.ceil((512*h)/w)<br /> int w = bitmap.getWidth();<br /> int h = bitmap.getHeight();<br /><br /> int dw = 512;<br /> int dh;<br /> if (w < h) {<br /> // dh = 512;<br /> dh = 342;<br /> } else {<br /> dh = (int) Math.ceil((512*h)/w);<br /> }<br /><br /> return Bitmap.createScaledBitmap(bitmap, dw, dh, false);<br /> }<br /><br /> private int[][] getPixels(Bitmap image) {<br /> int width = image.getWidth();<br /> int height = image.getHeight();<br /> int[][] result = new int[height][width];<br /> for (int row = 0; row < height; row++) {<br /> for (int col = 0; col < width; col++) {<br /> result[row][col] = getRGB(image, col, row);<br /> }<br /> }<br /> <br /> return result;<br /> }<br /><br /> private int getRGB(Bitmap bmpOriginal, int col, int row) {<br /> // get one pixel color<br /> int pixel = bmpOriginal.getPixel(col, row);<br /> // retrieve color of all channels<br /> int R = Color.red(pixel);<br /> int G = Color.green(pixel);<br /> int B = Color.blue(pixel);<br /> return Color.rgb(R, G, B);<br /> }<br /><br /> private byte[] printImage(int[][] pixels) {<br /> ByteArrayOutputStream printPort = new ByteArrayOutputStream(); <br /> <br /> try {<br /> //// Set the line spacing at 24 (we'll print 24 dots high)<br /> printPort.write(SET_LINE_SPACE_24);<br /> for (int y = 0; y < pixels.length; y += 24) {<br /> //// when done sending data per line,<br /> //// the printer will resume to normal text printing<br /> printPort.write(SELECT_BIT_IMAGE_MODE);<br /> //// Set nL and nH based on the width of the image<br /> printPort.write(new byte[]{(byte)(pixels[y].length)<br /> , (byte)((pixels[y].length) >> 8)});<br /> for (int x = 0; x < pixels[y].length; x++) {<br /> //// for each stripe, recollect 3 bytes (3 bytes (8bits *3) => 24 bits)<br /> printPort.write(recollectSlice(y, x, pixels));<br /> }<br /> printPort.write(LINE_FEED);<br /> }<br /> //// Set the line spacing back to 42 (default line spacing for normal text printing)<br /> printPort.write(SET_LINE_SPACE_42);<br /> } catch(Exception e) {<br /> e.getStackTrace();<br /> }<br /> <br /> return printPort.toByteArray();<br /><br /> }<br /> <br /> private byte[] recollectSlice(int y, int x, int[][] img) {<br /> byte[] slices = new byte[] {0, 0, 0};<br /> for (int yy = y, i = 0; yy < y + 24 && i < 3; yy += 8, i++) {<br /> byte slice = 0;<br /> for (int b = 0; b < 8; b++) {<br /> int yyy = yy + b;<br /> if (yyy >= img.length) {<br /> continue;<br /> }<br /> int col = img[yyy][x]; <br /> boolean v = shouldPrintColor(col);<br /> slice |= (byte) ((v ? 1 : 0) << (7 - b));<br /> }<br /> slices[i] = slice;<br /> }<br /> <br /> return slices;<br /> }<br /> <br /> private boolean shouldPrintColor(int col) {<br /> final int threshold = 225; //// 0 (black) - 255 (white)<br /> int a, r, g, b, luminance;<br /> a = (col >> 24) & 0xff;<br /> if (a != 0xff) { //// Ignore transparencies<br /> return false; <br /> }<br /> r = (col >> 16) & 0xff;<br /> g = (col >> 8) & 0xff;<br /> b = col & 0xff;<br /><br /> if (r > threshold && g > threshold && b > threshold) { //// when it is nearly white color<br /> return false;<br /> }<br /><br /> luminance = (int)((r + g + b) / 3); //// (int)(0.299 * r + 0.587 * g + 0.299 * b)<br /><br /> return luminance < threshold; <br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-67205312714231643762020-07-02T06:59:36.742+02:002020-07-02T06:59:36.742+02:00Hi i have used your code but i am unable to print ...Hi i have used your code but i am unable to print the entire image on 58mm thermal printer page.<br />When i print the imaeg with mode 33 it is printing 1/4 part of logo and strange characters between second and third part of an image. in additionto size of the logo or image is greater than the page size which is 58mm?<br />can you please guide me how to resolve it<br />Anonymoushttps://www.blogger.com/profile/03957720671357155570noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-55635210382035970962020-02-28T04:33:10.340+01:002020-02-28T04:33:10.340+01:00hello,i get a same problem , did u find solution ?...hello,i get a same problem , did u find solution ?Anonymoushttps://www.blogger.com/profile/09542968754667624610noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-85091766930888441102020-02-20T11:58:16.098+01:002020-02-20T11:58:16.098+01:00hello,did u find solution ? i am getting wrong siz...hello,did u find solution ? i am getting wrong size of printed image ,can u help me?Anonymoushttps://www.blogger.com/profile/09542968754667624610noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-75089131917724862422020-02-18T19:13:43.769+01:002020-02-18T19:13:43.769+01:00Can you share your C# code? Can you share your C# code? Fernandohttps://www.blogger.com/profile/07558826436980608779noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-78527650836127532962019-12-30T07:42:17.325+01:002019-12-30T07:42:17.325+01:00Is there any sample data of image in hex command, ...Is there any sample data of image in hex command, so that I can send that hex data directly to printer and it prints out. I am using another language where it does only need hex commands to send to my label printer. Please help me with the hex sample data for image printing (like a full response data). Please help!Shubhamhttps://www.blogger.com/profile/12280513550004048691noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-28537328156944683822019-10-22T12:42:56.773+02:002019-10-22T12:42:56.773+02:00"Caused by: javax.usb.UsbClaimException: No i..."Caused by: javax.usb.UsbClaimException: No interface is claimed" exception is comingShubhamhttps://www.blogger.com/profile/12280513550004048691noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-60008106548714587112019-10-22T11:34:44.783+02:002019-10-22T11:34:44.783+02:00"Error: Could not find or load main class usb..."Error: Could not find or load main class usb" while running the Main.java fiile in eclipse. Please help!Shubhamhttps://www.blogger.com/profile/12280513550004048691noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-65370143135798082022019-09-12T07:14:22.285+02:002019-09-12T07:14:22.285+02:00hi,
we have 2 inch thermal printer(320 dots per li...hi,<br />we have 2 inch thermal printer(320 dots per line) .<br />we are trying to display image of 80*75 pixels(dots) and communicating through UART protocol of MCU.<br /><br />we are using ESC * n1 n2 data (m = 0) .<br /><br />320 dots per line but image width is 80 dots. <br />how to send data through UART to printer and proceed with that?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-75821717125376491062019-09-07T12:12:36.322+02:002019-09-07T12:12:36.322+02:00Bro! you saved me hours of pain!!Bro! you saved me hours of pain!!JK1337https://www.blogger.com/profile/00137292502680578986noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-88055294184582989532018-12-17T12:15:57.398+01:002018-12-17T12:15:57.398+01:002018, it was google and stack overflow, 4 days wit...2018, it was google and stack overflow, 4 days without exit, beating a lot of head, doing tests. Horrible manuals of difficult understanding. Thank you very much.<br />From Brazil, PauloAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-40314572830519642462018-09-18T21:22:30.991+02:002018-09-18T21:22:30.991+02:00Hi Gustavo, first of all, really thanks for you he...Hi Gustavo, first of all, really thanks for you help sharing you code.<br /><br />But i having a problem i have a android project and made some changes to make this compatible, it is working fine the bitmap but not every time i print the image go wrong and show much caracters.<br /><br />Well i have some questions, i want to understand the basic why i use this :<br /><br />INIT_PRINTER<br /><br />LINE_FEED<br /><br />SET_LINE_SPACE_24 <br /><br />can you explain what is this parameters? and one last doubt, my image have 255px per 255px what i must put in parameters <br /><br /> public static byte[] SELECT_BIT_IMAGE_MODE_V2 = {0x1B, 0x2A, 33};<br /><br />to work?Wavrikhttps://www.blogger.com/profile/17406401946657001061noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-64496533811642530552018-09-13T16:28:22.471+02:002018-09-13T16:28:22.471+02:00Can you share the c# source code that you are usin...Can you share the c# source code that you are using after converting java to c#?Anonymoushttps://www.blogger.com/profile/14843433960289959962noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-80242088650038467612018-08-06T17:59:00.267+02:002018-08-06T17:59:00.267+02:00THANK YOUUUUUUUU Gustavo,
really, 2 weeks to try ...THANK YOUUUUUUUU Gustavo, <br />really, 2 weeks to try print a little icon and with your code translated in C# i can now print a big Image. <br />It is a good jo, i must yet improve my class to add a few methods, but you helped me a lot !!! <br />A congratulation, because your responded always at all question your are a very nice gentleman !<br />Best regards !!!<br />Christophe<br />PS : even old publication can help todayAnonymoushttps://www.blogger.com/profile/05768641519954940005noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-10819262440006877722018-02-01T17:32:09.499+01:002018-02-01T17:32:09.499+01:00Hi Bryan
Ive got the same issue with the white lin...Hi Bryan<br />Ive got the same issue with the white lines in Bit images. Are you able to post your same for NV images please? The link here isnt working. ThanksLitzohttps://www.blogger.com/profile/12652172167543016486noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-21787891613686362702018-02-01T17:29:58.103+01:002018-02-01T17:29:58.103+01:00This comment has been removed by the author.Litzohttps://www.blogger.com/profile/12652172167543016486noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-4880038497065972422017-11-30T10:38:25.942+01:002017-11-30T10:38:25.942+01:00You're very much welcomed. Nice to hear that t...You're very much welcomed. Nice to hear that the thing works in Haskell as well.<br />Pretty cool project! CheersGustavo De Michelihttps://www.blogger.com/profile/03094689119679861823noreply@blogger.comtag:blogger.com,1999:blog-2138874972589776570.post-45441714419334102002017-11-29T23:28:28.128+01:002017-11-29T23:28:28.128+01:00Hi Gustavo,
I know it’s old, but this article was...Hi Gustavo,<br /><br />I know it’s old, but this article was still one of the ones that helped me the most in coming up with a Haskell version of this code for my Raspberry Pi “photo booth” project : https://trandi.wordpress.com/2017/11/29/receipts-printer-photo-booth/<br /><br />So thanks !<br />Dan<br />Anonymoushttps://www.blogger.com/profile/00719057310184667293noreply@blogger.com