PrinterPlugin.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package edesarrollos.printer;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.os.Handler;
  8. import android.os.Looper;
  9. import io.flutter.plugin.common.MethodCall;
  10. import io.flutter.plugin.common.MethodChannel;
  11. import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
  12. import io.flutter.plugin.common.MethodChannel.Result;
  13. import io.flutter.plugin.common.PluginRegistry.Registrar;
  14. import com.qs.helper.printer.Device;
  15. import com.qs.helper.printer.bt.*;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.nio.Buffer;
  19. import java.nio.IntBuffer;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * PrinterPlugin
  27. */
  28. public class PrinterPlugin implements MethodCallHandler {
  29. private BtService BlueService;
  30. private static final int EstadoConectado = 3;
  31. private PrinterPlugin(Activity activity) {
  32. Handler controlador = new Handler(Looper.getMainLooper());
  33. Handler controladorSecundario = new Handler(Looper.getMainLooper());
  34. BlueService = new BtService(activity, controlador, controladorSecundario);
  35. }
  36. /**
  37. * Plugin registration.
  38. */
  39. public static void registerWith(Registrar registrar) {
  40. final MethodChannel channel = new MethodChannel(registrar.messenger(), "printer");
  41. channel.setMethodCallHandler(new PrinterPlugin(registrar.activity()));
  42. }
  43. @Override
  44. public void onMethodCall(MethodCall call, Result result) {
  45. switch (call.method) {
  46. case "getPlatformVersion":
  47. result.success("Android " + android.os.Build.VERSION.RELEASE);
  48. break;
  49. case "getDeviceList":
  50. Map<String, String> map = new HashMap<>();
  51. for (Device d : BlueService.getDeviceList()) {
  52. map.put(d.deviceAddress, d.deviceName);
  53. }
  54. result.success(map);
  55. break;
  56. case "isConnected":
  57. result.success(BlueService.getState() == EstadoConectado);
  58. break;
  59. case "connect":
  60. int intentos = 5;
  61. String mac = call.argument("mac");
  62. if (mac == null || mac.trim().length() == 0) {
  63. result.success(false);
  64. break;
  65. }
  66. do {
  67. if (intentos == 0) {
  68. result.success(false);
  69. break;
  70. }
  71. if (BlueService.getState() == EstadoConectado) {
  72. result.success(true);
  73. break;
  74. }
  75. for (Device dispositivo : BlueService.getDeviceList()) {
  76. if (dispositivo.deviceAddress.equals(mac)) {
  77. if (BlueService.getState() != EstadoConectado) {
  78. intentos--;
  79. BlueService.connect(dispositivo.deviceAddress);
  80. } else {
  81. result.success(true);
  82. }
  83. break;
  84. }
  85. }
  86. try {
  87. Thread.sleep(1000);
  88. } catch (Exception e) {
  89. System.out.print("No se puede detener el hilo");
  90. result.success(false);
  91. break;
  92. }
  93. if (BlueService.getState() == EstadoConectado) {
  94. result.success(true);
  95. break;
  96. }
  97. } while (BlueService.getState() != 3);
  98. break;
  99. case "printText":
  100. String texto = call.argument("text");
  101. if (texto == null || texto.trim().length() == 0) {
  102. result.success(false);
  103. break;
  104. }
  105. BlueService.printText(texto);
  106. result.success(true);
  107. break;
  108. case "printBitmap":
  109. byte[] mapa = call.argument("map");
  110. if (mapa == null) {
  111. result.success(false);
  112. break;
  113. }
  114. Bitmap bitmap = BitmapFactory.decodeByteArray(mapa, 0, mapa.length);
  115. // change transparent bitmap background to white
  116. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
  117. Canvas canvas = new Canvas(newBitmap);
  118. canvas.drawColor(Color.WHITE);
  119. canvas.drawBitmap(bitmap, 0, 0, null);
  120. //crop bounding white color
  121. newBitmap = ImageUtils.cropBitmapToBoundingBox(newBitmap, Color.WHITE);
  122. // get inputStream from bitmap
  123. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  124. newBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
  125. byte[] bitmapdata = bos.toByteArray();
  126. ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
  127. BlueService.printImage(BitmapFactory.decodeStream(bs));
  128. result.success(true);
  129. break;
  130. case "printBitmap2":
  131. byte[] mapa2 = call.argument("map");
  132. if (mapa2 == null) {
  133. result.success(false);
  134. break;
  135. }
  136. Bitmap bitmap2 = BitmapFactory.decodeByteArray(mapa2, 0, mapa2.length);
  137. BlueService.printImage2(bitmap2);
  138. result.success(true);
  139. break;
  140. case "disconnect":
  141. result.success(BlueService.disconnect());
  142. break;
  143. default:
  144. result.notImplemented();
  145. break;
  146. }
  147. }
  148. private int[] convertIntegers(List<Integer> integers) {
  149. int[] ret = new int[integers.size()];
  150. Iterator<Integer> iterator = integers.iterator();
  151. for (int i = 0; i < ret.length; i++) {
  152. ret[i] = iterator.next().intValue();
  153. }
  154. return ret;
  155. }
  156. }