Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. public void printPhoto(OutputStream outputStream, Bitmap img) {
  2. try {
  3. Bitmap bmp = img;
  4. if (bmp != null) {
  5. byte[] command = ImageDecoder.decodeBitmap(bmp);
  6. outputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
  7. outputStream.write(command);
  8. } else {
  9. Log.e("Print Photo error", "the file isn't exists");
  10. }
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. Log.e("PrintTools", "the file isn't exists");
  14. }
  15. }
  16.  
  17.  
  18.  
  19.  
  20. public static byte[] decodeBitmap(Bitmap bmp) {
  21. int bmpWidth = bmp.getWidth();
  22. int bmpHeight = bmp.getHeight();
  23.  
  24. List<String> list = new ArrayList<String>(); //binaryString list
  25. StringBuffer sb;
  26.  
  27.  
  28. int bitLen = bmpWidth / 8;
  29. int zeroCount = bmpWidth % 8;
  30.  
  31. String zeroStr = "";
  32. if (zeroCount > 0) {
  33. bitLen = bmpWidth / 8 + 1;
  34. for (int i = 0; i < (8 - zeroCount); i++) {
  35. zeroStr = zeroStr + "0";
  36. }
  37. }
  38.  
  39. for (int i = 0; i < bmpHeight; i++) {
  40. sb = new StringBuffer();
  41. for (int j = 0; j < bmpWidth; j++) {
  42. int color = bmp.getPixel(j, i);
  43.  
  44. int r = (color >> 16) & 0xff;
  45. int g = (color >> 8) & 0xff;
  46. int b = color & 0xff;
  47.  
  48. // if color close to white,bit='0', else bit='1'
  49. if (r > 160 && g > 160 && b > 160)
  50. sb.append("0");
  51. else
  52. sb.append("1");
  53. }
  54. if (zeroCount > 0) {
  55. sb.append(zeroStr);
  56. }
  57. list.add(sb.toString());
  58. }
  59.  
  60. List<String> bmpHexList = binaryListToHexStringList(list);
  61. String commandHexString = "1D763000";
  62. String widthHexString = Integer
  63. .toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8
  64. : (bmpWidth / 8 + 1));
  65. if (widthHexString.length() > 2) {
  66. Log.e("decodeBitmap error", " width is too large");
  67. return null;
  68. } else if (widthHexString.length() == 1) {
  69. widthHexString = "0" + widthHexString;
  70. }
  71. widthHexString = widthHexString + "00";
  72.  
  73. String heightHexString = Integer.toHexString(bmpHeight);
  74. if (heightHexString.length() > 2) {
  75. Log.e("decodeBitmap error", " height is too large");
  76. return null;
  77. } else if (heightHexString.length() == 1) {
  78. heightHexString = "0" + heightHexString;
  79. }
  80. heightHexString = heightHexString + "00";
  81.  
  82. List<String> commandList = new ArrayList<String>();
  83. commandList.add(commandHexString + widthHexString + heightHexString);
  84. commandList.addAll(bmpHexList);
  85.  
  86. return hexList2Byte(commandList);
  87. }
  88.  
  89.  
  90. public static List<String> binaryListToHexStringList(List<String> list) {
  91. List<String> hexList = new ArrayList<String>();
  92. for (String binaryStr : list) {
  93. StringBuffer sb = new StringBuffer();
  94. for (int i = 0; i < binaryStr.length(); i += 8) {
  95. String str = binaryStr.substring(i, i + 8);
  96.  
  97. String hexString = myBinaryStrToHexString(str);
  98. sb.append(hexString);
  99. }
  100. hexList.add(sb.toString());
  101. }
  102. return hexList;
  103.  
  104. }
  105.  
  106. public static String myBinaryStrToHexString(String binaryStr) {
  107. String hex = "";
  108. String f4 = binaryStr.substring(0, 4);
  109. String b4 = binaryStr.substring(4, 8);
  110. for (int i = 0; i < binaryArray.length; i++) {
  111. if (f4.equals(binaryArray[i]))
  112. hex += hexStr.substring(i, i + 1);
  113. }
  114. for (int i = 0; i < binaryArray.length; i++) {
  115. if (b4.equals(binaryArray[i]))
  116. hex += hexStr.substring(i, i + 1);
  117. }
  118.  
  119. return hex;
  120. }
  121.  
  122. public static byte[] hexList2Byte(List<String> list) {
  123. List<byte[]> commandList = new ArrayList<byte[]>();
  124.  
  125. for (String hexStr : list) {
  126. commandList.add(hexStringToBytes(hexStr));
  127. }
  128. byte[] bytes = sysCopy(commandList);
  129. return bytes;
  130. }
  131.  
  132. public static byte[] hexStringToBytes(String hexString) {
  133. if (hexString == null || hexString.equals("")) {
  134. return null;
  135. }
  136. hexString = hexString.toUpperCase();
  137. int length = hexString.length() / 2;
  138. char[] hexChars = hexString.toCharArray();
  139. byte[] d = new byte[length];
  140. for (int i = 0; i < length; i++) {
  141. int pos = i * 2;
  142. d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  143. }
  144. return d;
  145. }
  146.  
  147. public static byte[] sysCopy(List<byte[]> srcArrays) {
  148. int len = 0;
  149. for (byte[] srcArray : srcArrays) {
  150. len += srcArray.length;
  151. }
  152. byte[] destArray = new byte[len];
  153. int destLen = 0;
  154. for (byte[] srcArray : srcArrays) {
  155. System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
  156. destLen += srcArray.length;
  157. }
  158. return destArray;
  159. }
  160.  
  161. private static byte charToByte(char c) {
  162. return (byte) "0123456789ABCDEF".indexOf(c);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement