浏览代码

add image handling for raw data

master
munir ishak 4 年前
父节点
当前提交
2d7d722fcb
共有 1 个文件被更改,包括 179 次插入1 次删除
  1. 179
    1
      android/src/main/java/com/pinmi/react/printer/adapter/NetPrinterAdapter.java

+ 179
- 1
android/src/main/java/com/pinmi/react/printer/adapter/NetPrinterAdapter.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package com.pinmi.react.printer.adapter;
2 2
 
3
+import android.graphics.Bitmap;
4
+import android.graphics.BitmapFactory;
3 5
 import android.util.Base64;
4 6
 import android.util.Log;
5 7
 
@@ -12,6 +14,18 @@ import java.net.Socket;
12 14
 import java.util.Collections;
13 15
 import java.util.List;
14 16
 
17
+import java.io.File;
18
+import java.io.FileOutputStream;
19
+
20
+import android.graphics.*;
21
+import android.os.Environment;
22
+
23
+import java.util.ArrayList;
24
+import java.util.List;
25
+
26
+
27
+import java.io.UnsupportedEncodingException;
28
+
15 29
 /**
16 30
  * Created by xiesubin on 2017/9/22.
17 31
  */
@@ -24,6 +38,16 @@ public class NetPrinterAdapter implements PrinterAdapter {
24 38
 
25 39
     private Socket mSocket;
26 40
 
41
+    public static final byte[] UNICODE_TEXT = new byte[] {0x23, 0x23, 0x23,
42
+        0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,
43
+        0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,
44
+        0x23, 0x23, 0x23};
45
+
46
+    private static String hexStr = "0123456789ABCDEF";
47
+    private static String[] binaryArray = { "0000", "0001", "0010", "0011",
48
+            "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
49
+            "1100", "1101", "1110", "1111" };
50
+
27 51
     private NetPrinterAdapter (){
28 52
 
29 53
     }
@@ -90,6 +114,150 @@ public class NetPrinterAdapter implements PrinterAdapter {
90 114
         }
91 115
     }
92 116
 
117
+    public static byte[] convertBitmapToBytes(Bitmap bmp){
118
+        int bmpWidth = bmp.getWidth();
119
+        int bmpHeight = bmp.getHeight();
120
+
121
+        List<String> list = new ArrayList<String>(); //binaryString list
122
+        StringBuffer sb;
123
+
124
+
125
+        int bitLen = bmpWidth / 8;
126
+        int zeroCount = bmpWidth % 8;
127
+
128
+        String zeroStr = "";
129
+        if (zeroCount > 0) {
130
+            bitLen = bmpWidth / 8 + 1;
131
+            for (int i = 0; i < (8 - zeroCount); i++) {
132
+                zeroStr = zeroStr + "0";
133
+            }
134
+        }
135
+
136
+        for (int i = 0; i < bmpHeight; i++) {
137
+            sb = new StringBuffer();
138
+            for (int j = 0; j < bmpWidth; j++) {
139
+                int color = bmp.getPixel(j, i);
140
+
141
+                int r = (color >> 16) & 0xff;
142
+                int g = (color >> 8) & 0xff;
143
+                int b = color & 0xff;
144
+
145
+                // if color close to white,bit='0', else bit='1'
146
+                if (r > 160 && g > 160 && b > 160)
147
+                    sb.append("0");
148
+                else
149
+                    sb.append("1");
150
+            }
151
+            if (zeroCount > 0) {
152
+                sb.append(zeroStr);
153
+            }
154
+            list.add(sb.toString());
155
+        }
156
+
157
+        List<String> bmpHexList = binaryListToHexStringList(list);
158
+        String commandHexString = "1D763000";
159
+        String widthHexString = Integer
160
+                .toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8
161
+                        : (bmpWidth / 8 + 1));
162
+        if (widthHexString.length() > 2) {
163
+            Log.e("decodeBitmap error", " width is too large");
164
+            return null;
165
+        } else if (widthHexString.length() == 1) {
166
+            widthHexString = "0" + widthHexString;
167
+        }
168
+        widthHexString = widthHexString + "00";
169
+
170
+        String heightHexString = Integer.toHexString(bmpHeight);
171
+        if (heightHexString.length() > 2) {
172
+            Log.e("decodeBitmap error", " height is too large");
173
+            return null;
174
+        } else if (heightHexString.length() == 1) {
175
+            heightHexString = "0" + heightHexString;
176
+        }
177
+        heightHexString = heightHexString + "00";
178
+
179
+        List<String> commandList = new ArrayList<String>();
180
+        commandList.add(commandHexString+widthHexString+heightHexString);
181
+        commandList.addAll(bmpHexList);
182
+
183
+        return hexList2Byte(commandList);
184
+    }
185
+
186
+    public static List<String> binaryListToHexStringList(List<String> list) {
187
+        List<String> hexList = new ArrayList<String>();
188
+        for (String binaryStr : list) {
189
+            StringBuffer sb = new StringBuffer();
190
+            for (int i = 0; i < binaryStr.length(); i += 8) {
191
+                String str = binaryStr.substring(i, i + 8);
192
+
193
+                String hexString = myBinaryStrToHexString(str);
194
+                sb.append(hexString);
195
+            }
196
+            hexList.add(sb.toString());
197
+        }
198
+        return hexList;
199
+
200
+    }
201
+
202
+    public static String myBinaryStrToHexString(String binaryStr) {
203
+        String hex = "";
204
+        String f4 = binaryStr.substring(0, 4);
205
+        String b4 = binaryStr.substring(4, 8);
206
+        for (int i = 0; i < binaryArray.length; i++) {
207
+            if (f4.equals(binaryArray[i]))
208
+                hex += hexStr.substring(i, i + 1);
209
+        }
210
+        for (int i = 0; i < binaryArray.length; i++) {
211
+            if (b4.equals(binaryArray[i]))
212
+                hex += hexStr.substring(i, i + 1);
213
+        }
214
+
215
+        return hex;
216
+    }
217
+
218
+    public static byte[] hexList2Byte(List<String> list) {
219
+        List<byte[]> commandList = new ArrayList<byte[]>();
220
+
221
+        for (String hexStr : list) {
222
+            commandList.add(hexStringToBytes(hexStr));
223
+        }
224
+        byte[] bytes = sysCopy(commandList);
225
+        return bytes;
226
+    }
227
+
228
+    public static byte[] hexStringToBytes(String hexString) {
229
+        if (hexString == null || hexString.equals("")) {
230
+            return null;
231
+        }
232
+        hexString = hexString.toUpperCase();
233
+        int length = hexString.length() / 2;
234
+        char[] hexChars = hexString.toCharArray();
235
+        byte[] d = new byte[length];
236
+        for (int i = 0; i < length; i++) {
237
+            int pos = i * 2;
238
+            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
239
+        }
240
+        return d;
241
+    }
242
+
243
+    public static byte[] sysCopy(List<byte[]> srcArrays) {
244
+        int len = 0;
245
+        for (byte[] srcArray : srcArrays) {
246
+            len += srcArray.length;
247
+        }
248
+        byte[] destArray = new byte[len];
249
+        int destLen = 0;
250
+        for (byte[] srcArray : srcArrays) {
251
+            System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
252
+            destLen += srcArray.length;
253
+        }
254
+        return destArray;
255
+    }
256
+
257
+    private static byte charToByte(char c) {
258
+        return (byte) "0123456789ABCDEF".indexOf(c);
259
+    }
260
+
93 261
     @Override
94 262
     public void printRawData(String rawBase64Data, Callback errorCallback) {
95 263
         if(this.mSocket == null){
@@ -104,8 +272,18 @@ public class NetPrinterAdapter implements PrinterAdapter {
104 272
             public void run() {
105 273
                 try{
106 274
                     byte [] bytes = Base64.decode(rawData, Base64.DEFAULT);
275
+                    
276
+                    Bitmap mBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
277
+                    
107 278
                     OutputStream printerOutputStream = socket.getOutputStream();
108
-                    printerOutputStream.write(bytes, 0, bytes.length);
279
+                    if (mBitmap != null) {
280
+                        byte[] data = convertBitmapToBytes(mBitmap);
281
+                        printerOutputStream.write(new byte[] { 0x1b, 'a', 0x01 });
282
+                        printerOutputStream.write(data);
283
+                    }else{
284
+                        printerOutputStream.write(bytes, 0, bytes.length);
285
+                    }
286
+                    
109 287
                     printerOutputStream.flush();
110 288
                 }catch (IOException e){
111 289
                     Log.e(LOG_TAG, "failed to print data" + rawData);

正在加载...
取消
保存