对#Hello,2015征文#+Android 热敏打印机打印二维码(图片)博客的尝试

原博客链接

 #Hello,2015征文#+Android 热敏打印机打印二维码

  昨天晚上说抽时间来写一篇关于Android 热敏打印机打印二维码和图片的文章,所幸在下班之前把它给写了,和大家分享吧。我的是Android机器有内置热敏打印机的,我是把apk跑在我的Android机器上的,操作程序打印的。

  一、打印机的型号

  RP-POS80S或RP-POS80P或RP-POS80CS或RP-POS80CP打印机 高速热敏打印机
  打印方式:直接热敏打印
  打印密度:640点/行
  打印纸宽:80mm
  有效打印宽度:72mm
  最小走纸距离:0.125mm
  打印字符
  ACSII码字符集:12×24点
  国标一、二级汉字字库:24×24点
  采用命令集:ESC/POS打印命令集

  二、打印位图命令

     ESC *m nL nH d1...dk
     [名称]   选择位图模式
     [格式]   ASCII码   ESC  *       m nL nH d1...dk
     十六进制码    1B   2A   m nL nH d1...dk
     十进制码   27  42   m nL nH d1...dk
     [范围]   m= 0, 1, 32, 33
     0 £ nL£ 255
     0 £ nH£ 3
     0 £ d£ 255



[描述]用 m选择位图的模式,位图的点数由 nL和 nH指定,如下所示:
  
m
  
模式
垂直方向
水平方向
点数
点密度
点密度
数据个数 (K)
0
8-点 单密度
8
67.7 dpi
101.6 dpi
nL+ nH´ 256
1
8-点 双密度
8
67.7 dpi
203.2 dpi
nL+ nH´ 256
32
24-点 单密度
24
203.2 dpi
101.6 dpi
(nL+ nH ´ 256) ´3
33
24-点 双密度
24
203.2 dpi
203.2 dpi
(nL+ nH ´ 256) ´3

Dpi:每25.4毫米{1英寸}打印点数

[注意]
·如果m的值超出了指定的范围,那么nL和之后的数据被当作常规数据处理。
· nL和 nH表示水平方向上位图中的点数。通过nL+ nH´ 256计算出点数。
· 如果位图数据输入超出了一行上能被打印的点数,那么超出的数据被忽略。
· d表示位图数据。设置相应的位为 1去打印某点,或设置为 0以不打印某点。
· 如果用GS L 和 GSW 设置的打印范围的宽度比用ESC *命令 发送的数据所要求的宽度小时,则对有问题的行执行下列操作(但是打印不能超出最大可打印范围):

① 打印区域的宽度向右扩展以去适应数据量。
② 如果步骤①不能为数据提供足够的宽度,那么左边缘就被减少以去适应数据。对于在单密度模式(m= 0, 32)中的数据的每一位,打印机打印两个点:对于在双密度模式(m= 1, 33)中的数据的每一位,打印机打印一个点。在计算一行中能打印的数据量时,这些必须要考虑。

· 在打印一个位图之后,打印机返回常规数据处理模式。
· 这个命令不被打印模式(粗体、重叠、下划线、字符大小、或反白打印)影响, 除非是颠倒打印模式。
· 下图描述了图象数据与被打印的点之间的关系。

8-点位图被选定时


三、思路

因为所做的是打印图片(或者是二维码) ,先发送一个请求,从网络下载图片或者是二维码,得到这张图片过后,进行压缩到你想要的大小,在对压缩后的图片进行二值化,即黑白化,但是图片本身会有个α的值(即透明度)的问题,如果不进行处理的话,即使是黑白化的图片,得到图片的像素点也不是很精确。对于去透明度处理过后,在对这张图的每个最标点去像素值,即xxx  X  xxx大的图片,会有xxx X xxx个像素点。求出每个像素点的r、g、b的值,通过换算公式进行换算,得到一个像素值(0-255之间)。
如果是0-128是黑色,129-255是白色(非黑即白化),这样每个点的像素值就可以确定了。

因为我的热敏打印机是24*24的,所以会是24个像素点为一组,假设我把图片压缩为360*360像素的大小,就会有15组(纵向的15行,横坐标还是360)。一组的有24*360的像素点,因为8位是一个字节,这样24个像素点可以分为3组,每8位组成一个字节,每个像素点不是0就是1(之前已经非黑即白过),这样会得到一个byte数组,如:byte[] b1 = {1, 0, 0, 1, 0, 0, 0, 1}; 在将这样的数组换成十进制的数值int v1;这样就会得到3*15*360个的int 值,在将这些int的值和打印机的打印头命令拼接起来,组成一个byte[] 数组,就可以打印了。

四、注意

1.热敏打印机是一行一行的打印,所以每一行又得加上打印的头命令,我的打印机头命令是5个,所以byte[]数组的长度会是3*15*360+5*15

2.大家看好自己打印机的打印密度,比如我的是24*24的,所以我剪裁图片的大小会是24的倍数,360*360.大家最好也剪裁成相应的倍数,这样计算会方便点,不然的话,要对空白区域进行白色补缺。
[java]  view plain copy
  1. package com.woyou.util;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.Rect;  
  7. import android.util.Log;  
  8.   
  9. /** 
  10.  * 将图片转化为二进制 
  11.  * @author nsz 
  12.  * 2015年1月30日 
  13.  */  
  14. public class PicFromPrintUtils {  
  15.           
  16.           
  17.         public void init(){  
  18. //                Gray = 0.29900 * R + 0.58700 * G + 0.11400 * B  
  19.         }  
  20.           
  21.         /************************************************************************* 
  22.          * 我们的热敏打印机是RP-POS80S或RP-POS80P或RP-POS80CS或RP-POS80CP打印机 
  23.          * 360*360的图片,8个字节(8个像素点)是一个二进制,将二进制转化为十进制数值 
  24.          * y轴:24个像素点为一组,即360就是15组(0-14) 
  25.          * x轴:360个像素点(0-359) 
  26.          * 里面的每一组(24*360),每8个像素点为一个二进制,(每组有3个,3*8=24) 
  27.          **************************************************************************/  
  28.         /** 
  29.          * 把一张Bitmap图片转化为打印机可以打印的bit(将图片压缩为360*360) 
  30.          * 效率很高(相对于下面) 
  31.          * @param bit 
  32.          * @return 
  33.          */  
  34.         public static byte[] draw2PxPoint(Bitmap bit) {      
  35.                 byte[] data = new byte[16290];  
  36.                 int k = 0;  
  37.                 for (int j = 0; j < 15; j++) {  
  38.                         data[k++] = 0x1B;  
  39.                         data[k++] = 0x2A;  
  40.                         data[k++] = 33// m=33时,选择24点双密度打印,分辨率达到200DPI。  
  41.                         data[k++] = 0x68;  
  42.                         data[k++] = 0x01;  
  43.                         for (int i = 0; i < 360; i++) {  
  44.                                 for (int m = 0; m < 3; m++) {  
  45.                                         for (int n = 0; n < 8; n++) {  
  46.                                                 byte b = px2Byte(i, j * 24 + m * 8 + n, bit);  
  47.                                                 data[k] += data[k] + b;  
  48.                                         }  
  49.                                         k++;  
  50.                                 }  
  51.                         }  
  52.                         data[k++] = 10;  
  53.                 }  
  54.                 return data;  
  55.         }  
  56.           
  57.         /** 
  58.          * 把一张Bitmap图片转化为打印机可以打印的bit 
  59.          * @param bit 
  60.          * @return 
  61.          */  
  62.         public static byte[] pic2PxPoint(Bitmap bit){  
  63.                 long start = System.currentTimeMillis();  
  64.                 byte[] data = new byte[16290];  
  65.                 int k = 0;  
  66.                 for (int i = 0; i < 15; i++) {  
  67.                         data[k++] = 0x1B;  
  68.                         data[k++] = 0x2A;  
  69.                         data[k++] = 33// m=33时,选择24点双密度打印,分辨率达到200DPI。  
  70.                         data[k++] = 0x68;  
  71.                         data[k++] = 0x01;  
  72.                         for (int x = 0; x < 360; x++) {  
  73.                                 for (int m = 0; m < 3; m++) {  
  74.                                         byte[]  by = new byte[8];  
  75.                                         for (int n = 0; n < 8; n++) {  
  76.                                                 byte b = px2Byte(x, i * 24 + m * 8 +7-n, bit);  
  77.                                                 by[n] = b;  
  78.                                         }  
  79.                                         data[k] = (byte) changePointPx1(by);  
  80.                                         k++;  
  81.                                 }  
  82.                         }  
  83.                         data[k++] = 10;  
  84.                 }  
  85.                 long end = System.currentTimeMillis();  
  86.                 long str = end - start;  
  87.                 Log.i("TAG""str:" + str);  
  88.                 return data;  
  89.         }  
  90.           
  91.         /** 
  92.          * 图片二值化,黑色是1,白色是0 
  93.          * @param x  横坐标 
  94.          * @param y         纵坐标                         
  95.          * @param bit 位图 
  96.          * @return 
  97.          */  
  98.         public static byte px2Byte(int x, int y, Bitmap bit) {  
  99.                 byte b;  
  100.                 int pixel = bit.getPixel(x, y);  
  101.                 int red = (pixel & 0x00ff0000) >> 16// 取高两位  
  102.                 int green = (pixel & 0x0000ff00) >> 8// 取中两位  
  103.                 int blue = pixel & 0x000000ff// 取低两位  
  104.                 int gray = RGB2Gray(red, green, blue);  
  105.                 if ( gray < 128 ){  
  106.                         b = 1;  
  107.                 } else {  
  108.                         b = 0;  
  109.                 }  
  110.                 return b;  
  111.         }  
  112.           
  113.         /** 
  114.          * 图片灰度的转化 
  115.          * @param r   
  116.          * @param g 
  117.          * @param b 
  118.          * @return 
  119.          */  
  120.         private static int RGB2Gray(int r, int g, int b){  
  121.                 int gray = (int) (0.29900 * r + 0.58700 * g + 0.11400 * b);  //灰度转化公式  
  122.                 return  gray;  
  123.         }  
  124.           
  125.         /** 
  126.          * 对图片进行压缩(去除透明度) 
  127.          * @param bitmapOrg 
  128.          */  
  129.         public static Bitmap compressPic(Bitmap bitmapOrg) {  
  130.                 // 获取这个图片的宽和高  
  131.                 int width = bitmapOrg.getWidth();  
  132.                 int height = bitmapOrg.getHeight();  
  133.                 // 定义预转换成的图片的宽度和高度  
  134.                 int newWidth = 360;  
  135.                 int newHeight = 360;  
  136.                 Bitmap targetBmp = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);    
  137.                 Canvas targetCanvas = new Canvas(targetBmp);  
  138.                 targetCanvas.drawColor(0xffffffff);  
  139.                 targetCanvas.drawBitmap(bitmapOrg, new Rect(00, width, height), new Rect(00, newWidth, newHeight), null);  
  140.                 return targetBmp;  
  141.         }  
  142.           
  143.           
  144.         /** 
  145.          * 对图片进行压缩(不去除透明度) 
  146.          * @param bitmapOrg 
  147.          */  
  148.         public static Bitmap compressBitmap(Bitmap bitmapOrg) {  
  149.                 // 加载需要操作的图片,这里是一张图片  
  150. //                Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.alipay);  
  151.                 // 获取这个图片的宽和高  
  152.                 int width = bitmapOrg.getWidth();  
  153.                 int height = bitmapOrg.getHeight();  
  154.                 // 定义预转换成的图片的宽度和高度  
  155.                 int newWidth = 360;  
  156.                 int newHeight = 360;  
  157.                 // 计算缩放率,新尺寸除原始尺寸  
  158.                 float scaleWidth = ((float) newWidth) / width;  
  159.                 float scaleHeight = ((float) newHeight) / height;  
  160.                 // 创建操作图片用的matrix对象  
  161.                 Matrix matrix = new Matrix();  
  162.                 // 缩放图片动作  
  163.                 matrix.postScale(scaleWidth, scaleHeight);  
  164.                 // 创建新的图片  
  165.                 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 00, width,height, matrix, true);  
  166.                 // 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中  
  167. //                BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);  
  168.                 return resizedBitmap;  
  169.         }  
  170.           
  171.         /** 
  172.          * 将[1,0,0,1,0,0,0,1]这样的二进制转为化十进制的数值(效率更高) 
  173.          * @param arry 
  174.          * @return 
  175.          */  
  176.         public static int changePointPx1(byte[] arry){  
  177.                 int v = 0;  
  178.                 for (int j = 0; j <arry.length; j++) {  
  179.                         if( arry[j] == 1) {  
  180.                                 v = v | 1 << j;  
  181.                         }  
  182.                 }  
  183.                 return v;  
  184.         }  
  185.           
  186.         /** 
  187.          * 将[1,0,0,1,0,0,0,1]这样的二进制转为化十进制的数值 
  188.          * @param arry 
  189.          * @return 
  190.          */  
  191.         public byte changePointPx(byte[] arry){  
  192.                 byte v = 0;  
  193.                 for (int i = 0; i < 8; i++) {  
  194.                         v += v + arry[i];  
  195.                 }  
  196.                 return v;  
  197.         }  
  198.           
  199.         /** 
  200.          * 得到位图的某个点的像素值 
  201.          * @param bitmap 
  202.          * @return 
  203.          */  
  204.         public byte[] getPicPx(Bitmap bitmap){  
  205.                 int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];// 保存所有的像素的数组,图片宽×高  
  206.                 bitmap.getPixels(pixels, 0, bitmap.getWidth(), 00, bitmap.getWidth(), bitmap.getHeight());  
  207.                 for (int i = 0; i < pixels.length; i++) {  
  208.                         int clr = pixels[i];  
  209.                         int red = (clr & 0x00ff0000) >> 16// 取高两位  
  210.                 int green = (clr & 0x0000ff00) >> 8// 取中两位  
  211.                                 int blue = clr & 0x000000ff// 取低两位  
  212.                                 System.out.println("r=" + red + ",g=" + green + ",b=" + blue);  
  213.                 }  
  214.                 return null;  
  215.         }  
  216.           
  217. }  

[java]  view plain copy
  1. package com.woyou.util;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.security.InvalidParameterException;  
  9. import java.text.DecimalFormat;  
  10. import java.text.SimpleDateFormat;  
  11. import java.util.Date;  
  12. import java.util.Locale;  
  13.   
  14. import android.content.Context;  
  15. import android_serialport_api.SerialPort;  
  16.   
  17. /** 
  18.  * 打印机辅助 
  19.  *  
  20.  * @author nsz        2015年1月30日 
  21.  */  
  22. public class PrintUtil {  
  23.         final static int BUFFER_SIZE = 4096;  
  24.   
  25.         /** 
  26.          * 对一个byte[] 进行打印 
  27.          * @param printText 
  28.          * @return 
  29.          * add by yidie 
  30.          */  
  31.         public static boolean printBytes(byte[] printText) {  
  32.                 boolean returnValue = true;  
  33.                 try {  
  34.                         OutputStream mOutputStream = getSerialPort().getOutputStream();  
  35.                         mOutputStream.write(printText);  
  36.                 } catch (Exception ex) {  
  37.                         returnValue = false;  
  38.                 }  
  39.                 return returnValue;  
  40.         }  
  41.   
  42.         /** 
  43.          * "\n" 就是换行 
  44.          * @param paramString 
  45.          * @return 
  46.          * add by yidie 
  47.          */  
  48.         public static boolean printString(String paramString) {  
  49.                 return printBytes(getGbk(paramString));  
  50.         }  
  51.   
  52.         /*************************************************************************** 
  53.          * add by yidie 2012-01-10 功能:设置打印绝对位置 参数: int 在当前行,定位光标位置,取值范围0至576点 说明: 
  54.          * 在字体常规大小下,每汉字24点,英文字符12点 如位于第n个汉字后,则position=24*n 
  55.          * 如位于第n个半角字符后,则position=12*n 
  56.          ****************************************************************************/  
  57.   
  58.         public static byte[] setCusorPosition(int position) {  
  59.                 byte[] returnText = new byte[4]; // 当前行,设置绝对打印位置 ESC $ bL bH  
  60.                 returnText[0] = 0x1B;  
  61.                 returnText[1] = 0x24;  
  62.                 returnText[2] = (byte) (position % 256);  
  63.                 returnText[3] = (byte) (position / 256);  
  64.                 return returnText;  
  65.         }  
  66.   
  67.         /** 
  68.          * 设置打印机的行高 
  69.          * @param h 
  70.          * @return 
  71.          */  
  72.         public static byte[] setLineHeight(byte h) {  
  73.                 byte[] returnText = new byte[] { 0x1B0x33, h }; // 切纸; 1B 33 n  
  74.                 return returnText;  
  75.         }  
  76.   
  77.         public static byte[] setDefaultLineHeight() {  
  78.                 byte[] returnText = new byte[] { 0x1B0x32 }; // 切纸; 1B 32  
  79.                 return returnText;  
  80.         }  
  81.   
  82.         public static byte[] InputStreamTOByte(InputStream in) throws IOException {  
  83.   
  84.                 ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  85.                 byte[] data = new byte[BUFFER_SIZE];  
  86.                 int count = -1;  
  87.                 while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)  
  88.                         outStream.write(data, 0, count);  
  89.   
  90.                 data = null;  
  91.                 return outStream.toByteArray();  
  92.         }  
  93.           
  94.         /** 
  95.          * 打印我有外卖的logo 
  96.          * @param c 
  97.          */  
  98.         public static void printLogo(Context c) {  
  99.                 PrintUtil.printBytes(PrintUtil.setLineHeight((byte0));  
  100.                 InputStream is = c.getClass().getResourceAsStream("/assets/bill.bin");  
  101.                 byte[] b;  
  102.                 try {  
  103.                         b = InputStreamTOByte(is);  
  104.                         PrintUtil.printBytes(b);  
  105.                         PrintUtil.printBytes(PrintUtil.setDefaultLineHeight());  
  106.                 } catch (Exception e) {  
  107.                         e.printStackTrace();  
  108.                 }  
  109.         }  
  110.   
  111.          public static byte[] getLogo(Context c) {  
  112.                 InputStream is = c.getClass().getResourceAsStream("/assets/bill.bin");  
  113.                 byte[] b;  
  114.                 try {  
  115.                         b = InputStreamTOByte(is);  
  116.                         return b;  
  117.                 } catch (Exception e) {  
  118.                         e.printStackTrace();  
  119.                 }  
  120.                 return null;  
  121.          }  
  122.            
  123.          /** 
  124.           * 得到店铺logo 
  125.           * @param c 
  126.           * @param bit 
  127.           * @return 
  128.           */  
  129.         public static byte[] getLogo(Context c, byte[] bit) {  
  130.                 InputStream is = c.getClass().getResourceAsStream("/assets/bill.bin");  
  131.                 byte[] b = bit;  
  132.                 try {  
  133.                         b = InputStreamTOByte(is);  
  134.                         return b;  
  135.                 } catch (Exception e) {  
  136.                         e.printStackTrace();  
  137.                 }  
  138.                 return null;  
  139.         }  
  140.           
  141.         public static byte[] getLogo(byte[] bs) {  
  142.                 byte[] b;  
  143.                 try {  
  144.                         b = bs;  
  145.                         return b;  
  146.                 } catch (Exception e) {  
  147.                         e.printStackTrace();  
  148.                 }  
  149.                 return null;  
  150.         }          
  151.           
  152.         /** 
  153.          * 支付打印(二维码) 
  154.          * @param b 
  155.          * @param money 
  156.          * @return 
  157.          * [url=home.php?mod=space&uid=2643633]@throws[/url] InvalidParameterException 
  158.          * @throws SecurityException 
  159.          * @throws IOException 
  160.          */  
  161.         public static boolean printAlipayTitle(byte[] b, String money)   
  162.                         throws InvalidParameterException, SecurityException, IOException {  
  163.                   
  164.                 int iNum = 0;  
  165.                 byte[] tempBuffer = new byte[1000];  
  166.                   
  167.                 byte[]  oldText = setAlignCenter('2');  
  168.                 System.arraycopy(oldText, 0,  tempBuffer,  iNum,  oldText.length);  
  169.                 iNum += oldText.length;  
  170.                 oldText = setWH('4');   
  171.                 System.arraycopy(oldText, 0, tempBuffer,  iNum,  oldText.length);  
  172.                 iNum += oldText.length;  
  173.                 oldText = setBold(true);  
  174.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  175.                 iNum += oldText.length;  
  176.                 oldText = getGbk("支付凭证\n");  
  177.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  178.                 iNum += oldText.length;  
  179.                   
  180.                 oldText = getGbk("\n");  
  181.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  182.                 iNum += oldText.length;  
  183.                   
  184.                 oldText = setAlignCenter('2');  
  185.                 System.arraycopy(oldText, 0,  tempBuffer,  iNum,  oldText.length);  
  186.                 iNum += oldText.length;  
  187.                 oldText = setWH('3');   
  188.                 System.arraycopy(oldText, 0, tempBuffer,  iNum,  oldText.length);  
  189.                 iNum += oldText.length;  
  190.                 oldText = setBold(true);  
  191.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  192.                 iNum += oldText.length;  
  193.                 oldText = getGbk("您共消费了" + money + "元\n");  
  194.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  195.                 iNum += oldText.length;  
  196.                   
  197.                 oldText = getGbk("\n");  
  198.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  199.                 iNum += oldText.length;  
  200.                   
  201.                 oldText = setAlignCenter('2');  
  202.                 System.arraycopy(oldText, 0,  tempBuffer,  iNum,  oldText.length);  
  203.                 iNum += oldText.length;  
  204.                 oldText = setWH('3');   
  205.                 System.arraycopy(oldText, 0, tempBuffer,  iNum,  oldText.length);  
  206.                 iNum += oldText.length;  
  207.                 oldText = setBold(true);  
  208.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  209.                 iNum += oldText.length;  
  210.                 oldText = getGbk("请扫码支付\n\n");  
  211.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  212.                 iNum += oldText.length;  
  213.                   
  214.                 SerialPort mSerialPort = getSerialPort();  
  215.                 OutputStream mOutputStream = mSerialPort.getOutputStream();  
  216.                 try {  
  217.                         mOutputStream.write(tempBuffer);  
  218.                         printBytes(b);  
  219.                         printString("\n\n\n");  
  220.                         printBytes(CutPaper());  
  221.                 } catch (IOException e) {  
  222.                         e.printStackTrace();  
  223.                         return false;  
  224.                 }  
  225.                 return true;  
  226.         }  
  227.   
  228.         /*************************************************************************** 
  229.          * add by yidie 2012-01-10 功能:订单打印 参数: String 订单短号 OrderDetail 打印内容,包含 
  230.          * GoodsInfo[] String 打印标题 
  231.          ****************************************************************************/  
  232.   
  233.         public static boolean printOrder(Context c, byte[] b)  
  234.                         throws InvalidParameterException, SecurityException, IOException {  
  235.   
  236.                 DecimalFormat dcmFmt = new DecimalFormat("0.00");  
  237.                 int iNum = 0, i;  
  238.   
  239.                 byte[] tempBuffer = new byte[8000];  
  240.                 String stTmp = "";  
  241.   
  242.                 byte[] oldText = setAlignCenter('2');  
  243.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  244.                 iNum += oldText.length;  
  245.   
  246.                 oldText = getLogo(c, b);  
  247.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  248.                 iNum += oldText.length;  
  249.   
  250.                 oldText = setWH('1');  
  251.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  252.                 iNum += oldText.length;  
  253.   
  254.                 oldText = getGbk("\n");  
  255.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  256.                 iNum += oldText.length;  
  257.   
  258.                 oldText = setAlignCenter('2');  
  259.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  260.                 iNum += oldText.length;  
  261.   
  262.                 oldText = setWH('4');  
  263.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  264.                 iNum += oldText.length;  
  265.   
  266.                 oldText = setBold(true);  
  267.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  268.                 iNum += oldText.length;  
  269.   
  270.                 oldText = setWH('1');  
  271.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  272.                 iNum += oldText.length;  
  273.   
  274.                 oldText = getGbk("\n");  
  275.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  276.                 iNum += oldText.length;  
  277.   
  278.                 oldText = setAlignCenter('1');  
  279.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  280.                 iNum += oldText.length;  
  281.   
  282.                 oldText = setCusorPosition(324);  
  283.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  284.                 iNum += oldText.length;  
  285.   
  286.                 String strTime = new SimpleDateFormat("yyyy-MM-dd HH:mm",  
  287.                                 Locale.SIMPLIFIED_CHINESE).format(new Date());  
  288.                 oldText = getGbk(strTime + "打印\n");  
  289.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  290.                 iNum += oldText.length;  
  291.   
  292.                 oldText = setBold(false);  
  293.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  294.                 iNum += oldText.length;  
  295.   
  296.                 oldText = getGbk("----------------------------------------------\n");  
  297.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  298.                 iNum += oldText.length;  
  299.   
  300.                 oldText = setWH('3');  
  301.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  302.                 iNum += oldText.length;  
  303.   
  304.                 oldText = getGbk("   商品名称              单价    数量    金额\n");  
  305.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  306.                 iNum += oldText.length;  
  307.   
  308.                 oldText = getGbk("----------------------------------------------\n");  
  309.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  310.                 iNum += oldText.length;  
  311.   
  312.                 oldText = setWH('3');  
  313.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  314.                 iNum += oldText.length;  
  315.   
  316.                 oldText = setAlignCenter('2');  
  317.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  318.                 iNum += oldText.length;  
  319.   
  320.                 oldText = setWH('1');  
  321.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  322.                 iNum += oldText.length;  
  323.   
  324.                 oldText = getGbk("\n感谢使用[我有外卖]订餐,24小时服务热线 4008519517\n\n\n");  
  325.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  326.                 iNum += oldText.length;  
  327.   
  328.                 oldText = CutPaper();  
  329.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  330.                 iNum += oldText.length;  
  331.                   
  332.                 SerialPort mSerialPort = getSerialPort();  
  333.                 OutputStream mOutputStream = mSerialPort.getOutputStream();  
  334.                 try {  
  335.                         mOutputStream.write(tempBuffer);  
  336.                 } catch (IOException e) {  
  337.                         e.printStackTrace();  
  338.                         return false;  
  339.                 }  
  340.                 return true;  
  341.         }  
  342.   
  343.         /*************************************************************************** 
  344.          * add by yidie 2012-01-12 功能:报表打印 参数: String 打印标题,如“月报表:2013-01” 
  345.          * ReportUserSale 打印内容,包含 UserSaleInfo[] 
  346.          ****************************************************************************/  
  347.   
  348.         public static boolean printReportUser() throws InvalidParameterException,  
  349.                         SecurityException, IOException {  
  350.   
  351.                 int iNum = 0;  
  352.                 String stTmp = "";  
  353.   
  354.                 byte[] tempBuffer = new byte[8000];  
  355.                 SerialPort mSerialPort = getSerialPort();  
  356.                 OutputStream mOutputStream = mSerialPort.getOutputStream();  
  357.   
  358.                 byte[] oldText = setAlignCenter('1');  
  359.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  360.                 iNum += oldText.length;  
  361.   
  362.                 oldText = setWH('3');  
  363.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  364.                 iNum += oldText.length;  
  365.   
  366.                 oldText = setCusorPosition(324);  
  367.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  368.                 iNum += oldText.length;  
  369.   
  370.                 String strTime = new SimpleDateFormat("yyyy-MM-dd HH:mm",  
  371.                                 Locale.SIMPLIFIED_CHINESE).format(new Date());  
  372.                 oldText = getGbk(strTime + "打印\n");  
  373.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  374.                 iNum += oldText.length;  
  375.   
  376.                 oldText = setWH('1');  
  377.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  378.                 iNum += oldText.length;  
  379.   
  380.                 oldText = setAlignCenter('2');  
  381.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  382.                 iNum += oldText.length;  
  383.   
  384.                 oldText = setWH('4');  
  385.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  386.                 iNum += oldText.length;  
  387.   
  388.                 oldText = setBold(true);  
  389.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  390.                 iNum += oldText.length;  
  391.   
  392.                 oldText = setWH('1');  
  393.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  394.                 iNum += oldText.length;  
  395.   
  396.                 oldText = getGbk("\n\n");  
  397.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  398.                 iNum += oldText.length;  
  399.   
  400.                 oldText = setAlignCenter('1');  
  401.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  402.                 iNum += oldText.length;  
  403.   
  404.                 oldText = setBold(false);  
  405.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  406.                 iNum += oldText.length;  
  407.   
  408.                 oldText = setWH('1');  
  409.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  410.                 iNum += oldText.length;  
  411.   
  412.                 oldText = getGbk("     用户           售出数量 售出金额\n");  
  413.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  414.                 iNum += oldText.length;  
  415.   
  416.                 oldText = getGbk("----------------------------------------------\n");  
  417.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  418.                 iNum += oldText.length;  
  419.   
  420.                 oldText = CutPaper();  
  421.                 System.arraycopy(oldText, 0, tempBuffer, iNum, oldText.length);  
  422.                 iNum += oldText.length;  
  423.                 try {  
  424.                         mOutputStream.write(tempBuffer);  
  425.                 } catch (IOException e) {  
  426.                         e.printStackTrace();  
  427.                         return false;  
  428.                 }  
  429.   
  430.                 return true;  
  431.         }  
  432.   
  433.         /*************************************************************************** 
  434.          * add by yidie 2012-01-12 功能:报表打印 参数: String 打印标题,如“月报表:2013-01” ReportSale 
  435.          * 打印内容,包含 SaleInfo[] 
  436.          ****************************************************************************/  
  437.   
  438.         private static SerialPort mSerialPort = null;  
  439.   
  440.         public static SerialPort getSerialPort() throws SecurityException,  
  441.                         IOException, InvalidParameterException {  
  442.                 if (mSerialPort == null) {  
  443.                         String spFile = null;  
  444.                         String model = MainBoardUtil.getModel(); // android.os.Build.MODEL.toLowerCase();  
  445.                         if (model.contains(Constants.MAIN_BOARD_SMDKV210)) {  
  446.                                 spFile = "/dev/s3c2410_serial0";  
  447.                         } else if (model.contains(Constants.MAIN_BOARD_RK30)) {  
  448.                                 spFile = "/dev/ttyS1";  
  449.                         } else if (model.contains(Constants.MAIN_BOARD_C500)) {  
  450.                                 spFile = "/dev/ttyS1";  
  451.                         } else {  
  452.                                 throw new IOException("unknow hardware!");  
  453.                         }  
  454.   
  455.                         int baudrate = 115200;  
  456.                         boolean flagCon = true;  
  457.   
  458.                         File myFile = new File(spFile);  
  459.   
  460.                         /* Open the serial port */  
  461.                         mSerialPort = new SerialPort(myFile, baudrate, 0, flagCon);  
  462.                 }  
  463.                 return mSerialPort;  
  464.         }  
  465.   
  466.         public static void closeSerialPort() {  
  467.                 if (mSerialPort != null) {  
  468.                         mSerialPort.close();  
  469.                         mSerialPort = null;  
  470.                 }  
  471.         }  
  472.   
  473.         public static byte[] getGbk(String stText) {  
  474.                 byte[] returnText = null;  
  475.                 try {  
  476.                         returnText = stText.getBytes("GBK"); // 必须放在try内才可以  
  477.                 } catch (Exception ex) {  
  478.                         ;  
  479.                 }  
  480.                 return returnText;  
  481.         }  
  482.   
  483.         public static byte[] setWH(char dist) {  
  484.                 byte[] returnText = new byte[3]; // GS ! 11H 倍宽倍高  
  485.                 returnText[0] = 0x1D;  
  486.                 returnText[1] = 0x21;  
  487.   
  488.                 switch (dist) // 1-无;2-倍宽;3-倍高; 4-倍宽倍高  
  489.                 {  
  490.                 case '2':  
  491.                         returnText[2] = 0x10;  
  492.                         break;  
  493.                 case '3':  
  494.                         returnText[2] = 0x01;  
  495.                         break;  
  496.                 case '4':  
  497.                         returnText[2] = 0x11;  
  498.                         break;  
  499.                 default:  
  500.                         returnText[2] = 0x00;  
  501.                         break;  
  502.                 }  
  503.   
  504.                 return returnText;  
  505.         }  
  506.   
  507.         /** 
  508.          * 打印的对齐方式 
  509.          * @param dist 
  510.          * @return 
  511.          */  
  512.         public static byte[] setAlignCenter(char dist) {  
  513.                 byte[] returnText = new byte[3]; // 对齐 ESC a  
  514.                 returnText[0] = 0x1B;  
  515.                 returnText[1] = 0x61;  
  516.   
  517.                 switch (dist) // 1-左对齐;2-居中对齐;3-右对齐  
  518.                 {  
  519.                 case '2':  
  520.                         returnText[2] = 0x01;  
  521.                         break;  
  522.                 case '3':  
  523.                         returnText[2] = 0x02;  
  524.                         break;  
  525.                 default:  
  526.                         returnText[2] = 0x00;  
  527.                         break;  
  528.                 }  
  529.                 return returnText;  
  530.         }  
  531.   
  532.         public static byte[] setBold(boolean dist) {  
  533.                 byte[] returnText = new byte[3]; // 加粗 ESC E  
  534.                 returnText[0] = 0x1B;  
  535.                 returnText[1] = 0x45;  
  536.   
  537.                 if (dist) {  
  538.                         returnText[2] = 0x01// 表示加粗  
  539.                 } else {  
  540.                         returnText[2] = 0x00;  
  541.                 }  
  542.                 return returnText;  
  543.         }  
  544.   
  545.         public static byte[] PrintBarcode(String stBarcode) {  
  546.                 int iLength = stBarcode.length() + 4;  
  547.                 byte[] returnText = new byte[iLength];  
  548.   
  549.                 returnText[0] = 0x1D;  
  550.                 returnText[1] = 'k';  
  551.                 returnText[2] = 0x45;  
  552.                 returnText[3] = (byte) stBarcode.length(); // 条码长度;  
  553.   
  554.                 System.arraycopy(stBarcode.getBytes(), 0, returnText, 4,  
  555.                                 stBarcode.getBytes().length);  
  556.   
  557.                 return returnText;  
  558.         }  
  559.   
  560.         /** 
  561.          * 切纸 
  562.          * @return 
  563.          */  
  564.         public static byte[] CutPaper() {  
  565.                 byte[] returnText = new byte[] { 0x1D0x560x420x00 }; // 切纸; GS V  
  566.                                                                                                                                         // 66D 0D  
  567.                 return returnText;  
  568.         }  
  569. }  

[java]  view plain copy
  1. package com.woyou.woyoupay;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.URL;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.json.JSONException;  
  9. import org.json.JSONObject;  
  10.   
  11. import android.app.Activity;  
  12. import android.app.ProgressDialog;  
  13. import android.content.Intent;  
  14. import android.graphics.Bitmap;  
  15. import android.graphics.BitmapFactory;  
  16. import android.graphics.drawable.Drawable;  
  17. import android.os.Bundle;  
  18. import android.text.Html;  
  19. import android.util.Log;  
  20. import android.view.View;  
  21. import android.view.View.OnClickListener;  
  22. import android.view.Window;  
  23. import android.widget.ImageView;  
  24. import android.widget.TextView;  
  25.   
  26. import com.anjoyo.net.AsyncHttpClient;  
  27. import com.anjoyo.net.JsonHttpResponseHandler;  
  28. import com.anjoyo.net.RequestParams;  
  29. import com.woyou.R;  
  30. import com.woyou.bean.ScanCodeRes;  
  31. import com.woyou.util.Constants;  
  32. import com.woyou.util.PicFromPrintUtils;  
  33. import com.woyou.util.PrintUtil;  
  34. import com.woyou.util.ThreadPoolManager;  
  35.   
  36. public class Print2DCodeAct extends Activity implements OnClickListener {  
  37.         private static final String TAG = "Print2DCodeAct";  
  38.         TextView back, print, motifiscan;  
  39.         TextView oId, money, price;  
  40.         ImageView printImg;  
  41.         ProgressDialog dialog;  
  42.   
  43.         @Override  
  44.         protected void onCreate(Bundle savedInstanceState) {  
  45.                 super.onCreate(savedInstanceState);  
  46.                 requestWindowFeature(Window.FEATURE_NO_TITLE);  
  47.                 setContentView(R.layout.activity_print2dcode);  
  48.                 initView();  
  49.         }  
  50.   
  51.         String extra;  
  52.         private void initView() {  
  53.                 Intent intent = getIntent();  
  54.                 extra = intent.getStringExtra("money");  
  55.                 back = (TextView) this.findViewById(R.id.back);  
  56.                 oId = (TextView) this.findViewById(R.id.order_id);  
  57.                 back.setOnClickListener(this);  
  58.                 motifiscan = (TextView) this.findViewById(R.id.motifiscan);  
  59.                 print = (TextView) this.findViewById(R.id.print_image);  
  60.                 printImg = (ImageView) this.findViewById(R.id.print_two_image);  
  61.                 money = (TextView) this.findViewById(R.id.money);  
  62.                 print.setOnClickListener(this);  
  63.                 motifiscan.setOnClickListener(this);  
  64.                 money.setText(Html.fromHtml("¥<big>" + extra + "</big>"));  
  65.                   
  66.                 //显示图片  
  67. //                Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.alipay);  
  68. //                Bitmap bitmap = compressPic(bitmapOrg);  
  69. //                image_alipy.setImageBitmap(bitmap);  
  70.                   
  71.                 //请求数据  
  72.                 getData();  
  73.         }  
  74.   
  75.         private void showDialog() {  
  76.                 if (dialog == null) {  
  77.                         dialog = new ProgressDialog(this);  
  78.                         dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  79.                         dialog.setCancelable(true);  
  80.                         dialog.setMessage("正在加载中,请稍候...");  
  81.                 }  
  82.                 dialog.show();  
  83.         }  
  84.         private void hideDialog() {  
  85.                 runOnUiThread(new Runnable() {  
  86.                           
  87.                         @Override  
  88.                         public void run() {  
  89.                                 if (dialog != null) {  
  90.                                         dialog.dismiss();  
  91.                                 }  
  92.                         }  
  93.                 });  
  94.         }  
  95.           
  96.           
  97.           
  98.         @Override  
  99.         protected void onResume() {  
  100.                 super.onResume();  
  101.                 Constants.ACTIVITY_INSTANCE = Print2DCodeAct.this;  
  102.         }  
  103.   
  104.         List<ScanCodeRes> list = new ArrayList<ScanCodeRes>();  
  105.         Bitmap compressPic = null;  
  106.         String big_pic_url;  
  107.         String pic_url;  
  108.         String small_pic_url;  
  109.         private void getData() {  
  110.                 float f = Float.parseFloat(extra) * 100;  
  111.                 int inte = (int) f;  
  112.                 long parseLong = Long.parseLong(String.valueOf(inte));  
  113.                   
  114.                 String sign = Constants.md5("100512354" + "150039203" + Constants.KEY);  
  115.                 String url = "http://api.coupon.dev.wosai.cn/Upay/alipayQrCodeOffline";  
  116.                 AsyncHttpClient client = new AsyncHttpClient();  
  117.                 RequestParams params = new RequestParams();  
  118.                 params.put("store_own_order_id""1234");  
  119.                 params.put("subject""喔噻体验商品");  
  120.                 params.put("total_fee", parseLong+"");  
  121.                 params.put("wosai_store_id""100512354");  
  122.                 params.put("wosai_app_id""150039203");  
  123.                 params.put("sign", sign);  
  124.                 params.put("notify_url""http://www.woyouwaimai.com/get");   //推送支付成功的通知  
  125.                 Log.i(TAG, url + params);  
  126.                   
  127.                 client.get(url, params, new JsonHttpResponseHandler() {  
  128.                         @Override  
  129.                         public void onStart() {  
  130.                                 super.onStart();  
  131.                                 showDialog();  
  132.                         }  
  133.                         @Override  
  134.                         public void onSuccess(JSONObject response) {  
  135.                                 super.onSuccess(response);  
  136.                                 try {  
  137.                                         String code = response.getString("code");  
  138.                                         String msg = response.getString("msg");  
  139.                                         Log.i(TAG, "code:" + code);  
  140.                                         Log.i(TAG, "msg:" + msg);  
  141.                                         if ("10000".equals(code)) {  
  142.                                                 JSONObject data = response.getJSONObject("data");  
  143.                                                 String order_sn = data.getString("order_sn");  
  144.                                                 String wosai_store_id = data.getString("wosai_store_id");  
  145.                                                 int status = data.getInt("status");  
  146.                                                 String ctime = data.getString("ctime");  
  147.                                                 JSONObject order_pay_detail = data.getJSONObject("order_pay_detail");  
  148.                                                 String order_detail = data.getString("order_detail");  
  149.                                                 String pay_way = data.getString("pay_way");  
  150.                                                 long total_fee = data.getLong("total_fee");  
  151.   
  152.                                                 String is_success = order_pay_detail.getString("is_success");  
  153.                                                 JSONObject responses = order_pay_detail.getJSONObject("response");  
  154.                                                 String sign = order_pay_detail.getString("sign");  
  155.                                                 String sign_type = order_pay_detail.getString("sign_type");  
  156.   
  157.                                                 JSONObject alipay = responses.getJSONObject("alipay");  
  158.                                                 big_pic_url = alipay.getString("big_pic_url");  
  159.                                                 String out_trade_no = alipay.getString("out_trade_no");  
  160.                                                 pic_url = alipay.getString("pic_url");  
  161.                                                 String qr_code = alipay.getString("qr_code");  
  162.                                                 String result_code = alipay.getString("result_code");  
  163.                                                 small_pic_url = alipay.getString("small_pic_url");  
  164.                                                 String voucher_type = alipay.getString("voucher_type");  
  165.                                                 Log.i(TAG, "big_pic_url:" + big_pic_url);  
  166.                                                 Log.i(TAG, "pic_url:" + pic_url);  
  167.                                                 Log.i(TAG, "small_pic_url:" + small_pic_url);  
  168.                                                   
  169.                                                 ScanCodeRes res = new ScanCodeRes();  
  170.                                                 res.setOrder_sn(order_sn);  
  171.                                                 res.setWosai_store_id(wosai_store_id);  
  172.                                                 res.setStatus(status);  
  173.                                                 res.setCtime(ctime);  
  174.                                                 res.setIs_success(is_success);  
  175.                                                 res.setOrder_detail(order_detail);  
  176.                                                 res.setTotal_fee(total_fee);  
  177.                                                 res.setPay_way(pay_way);  
  178.                                                 list.add(res);  
  179.                                                   
  180.                                                 Constants.memoryCache.put(Constants.SCAN_CODE_RESULT, list);  
  181.                                                   
  182.                                         }  
  183.                                 } catch (JSONException e) {  
  184.                                         e.printStackTrace();  
  185.                                         hideDialog();  
  186.                                 }  
  187.                         }  
  188.   
  189.                         @Override  
  190.                         public void onFinish() {  
  191.                                 super.onFinish();  
  192.                                 ThreadPoolManager.getInstance().executeTask(new Runnable() {  
  193.                                         @Override  
  194.                                         public void run() {  
  195.                                                 try {  
  196.                                                         Bitmap bitmap = BitmapFactory.decodeStream(new URL(pic_url).openStream());  
  197.                                                         compressPic = PicFromPrintUtils.compressPic(bitmap);  
  198.                                                 }catch (IOException e) {  
  199.                                                         e.printStackTrace();  
  200.                                                 }  
  201.                                                 runOnUiThread(new Runnable() {  
  202.                                                           
  203.                                                         @Override  
  204.                                                         public void run() {  
  205.                                                                 printImg.setImageBitmap(compressPic);  
  206.                                                                 print2Code(compressPic);  
  207.                                                                 hideDialog();  
  208.                                                         }  
  209.                                                 });  
  210.                                                   
  211.                                         }  
  212.                                 });  
  213.                         }  
  214.   
  215.                 });  
  216.         }  
  217.           
  218.         //打印二维码  
  219.         private void print2Code(Bitmap bitmap){  
  220.                 final byte[] bs = PicFromPrintUtils.draw2PxPoint(bitmap);  
  221.                 ThreadPoolManager.getInstance().executeTask(new Runnable() {  
  222.                         @Override  
  223.                         public void run() {  
  224.                                 try {  
  225.                                         PrintUtil.printAlipayTitle(bs, extra);  
  226.                                 } catch (Exception e) {  
  227.                                         e.printStackTrace();  
  228.                                 }  
  229.                         }  
  230.                 });  
  231.         }  
  232.           
  233.         @Override  
  234.         public void onClick(View v) {  
  235.                 switch (v.getId()) {  
  236.                 case R.id.print_image:  
  237.                         if ( compressPic != null ){  
  238.                                 print2Code(compressPic);  
  239.                         }  
  240.                         break;  
  241.   
  242.                 case R.id.back:  
  243.                         finish();  
  244.                         break;  
  245.   
  246.                 case R.id.motifiscan:  
  247.                         Intent intent = new Intent(this, HomeAct.class);  
  248.                         intent.putExtra("money", extra);  
  249.                         startActivity(intent);  
  250.                         break;  
  251.                 }  
  252.         }  
  253.           
  254.         public Drawable loadImageFromNetwork(String urladdr) {  
  255.                 Drawable drawable = null;  
  256.                 try {  
  257.                         drawable = Drawable.createFromStream(new URL(urladdr).openStream(),"image.jpg");  
  258.                 } catch (IOException e) {  
  259.                         Log.d("test", e.getMessage());  
  260.                 }  
  261.                 if (drawable == null) {  
  262.                         Log.d("test""null drawable");  
  263.                 } else {  
  264.                         Log.d("test""not null drawable");  
  265.                 }  
  266.                 return drawable;  
  267.         }  
  268.           
  269.         // 计算图片的缩放值  
  270.         public static int calculateInSampleSize(BitmapFactory.Options options,  
  271.                         int reqWidth, int reqHeight) {  
  272.                 final int height = options.outHeight;  
  273.                 final int width = options.outWidth;  
  274.                 int inSampleSize = 1;  
  275.                 if (height > reqHeight || width > reqWidth) {  
  276.                         final int heightRatio = Math.round((float) height  
  277.                                         / (float) reqHeight);  
  278.                         final int widthRatio = Math.round((float) width / (float) reqWidth);  
  279.                         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  280.                 }  
  281.                 return inSampleSize;  
  282.         }  
  283.   
  284. }  

另外遇到了和这个兄弟一样的问题:

draw2PxPoint()的data[k++] = 10是啥意思啊楼主 我现在打印出来的二维码 应该是每行多了一条空白行

已经ok了 设置行高为0去打印就好了 谢谢楼主的代码
                              那个 10 是换行,行高的设置用 ESC 3 0;
首先:
这篇文章写的很赞!但通过自己尝试的结果发现如果要用ESC指令打印一个图片。
需要注意几点:
           1.先确认你打印机的打印密度(这个由硬件决定)
           2.通过位图宽高,计算打印的行数和每一行应该打印的字节数,
           3.理解ESC m nL nH XXX命令,正是因为有这条命令所以你每一行都要拼成这个命令的格式:
             <ESC m nL nH> + <一行的数据>+<10(换行> 最后将所有转换好的数据统一发送给打印机即可打印。
另外:
     这篇文章我觉得应该叫打印位图更合适,因为你可以自己创建位图,在位图上面画你任意想画的东西,字符串,图片logo,二维码,一维码,各种条码格式。最终将位图按照ESC指令打印出来。这样倒是省的去指定条码类型这些指令了,因为我打印的是位图,位图上面画的是什么打印机不关心,你想画什么画什么,这也就无所谓是什么条码类型了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值