123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package edesarrollos.printer;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.os.Handler;
- import android.os.Looper;
- import io.flutter.plugin.common.MethodCall;
- import io.flutter.plugin.common.MethodChannel;
- import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
- import io.flutter.plugin.common.MethodChannel.Result;
- import io.flutter.plugin.common.PluginRegistry.Registrar;
- import com.qs.helper.printer.Device;
- import com.qs.helper.printer.bt.*;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.nio.Buffer;
- import java.nio.IntBuffer;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- /**
- * PrinterPlugin
- */
- public class PrinterPlugin implements MethodCallHandler {
- private BtService BlueService;
- private static final int EstadoConectado = 3;
- private PrinterPlugin(Activity activity) {
- Handler controlador = new Handler(Looper.getMainLooper());
- Handler controladorSecundario = new Handler(Looper.getMainLooper());
- BlueService = new BtService(activity, controlador, controladorSecundario);
- }
- /**
- * Plugin registration.
- */
- public static void registerWith(Registrar registrar) {
- final MethodChannel channel = new MethodChannel(registrar.messenger(), "printer");
- channel.setMethodCallHandler(new PrinterPlugin(registrar.activity()));
- }
- @Override
- public void onMethodCall(MethodCall call, Result result) {
- switch (call.method) {
- case "getPlatformVersion":
- result.success("Android " + android.os.Build.VERSION.RELEASE);
- break;
- case "getDeviceList":
- Map<String, String> map = new HashMap<>();
- for (Device d : BlueService.getDeviceList()) {
- map.put(d.deviceAddress, d.deviceName);
- }
- result.success(map);
- break;
- case "isConnected":
- result.success(BlueService.getState() == EstadoConectado);
- break;
- case "connect":
- int intentos = 5;
- String mac = call.argument("mac");
- if (mac == null || mac.trim().length() == 0) {
- result.success(false);
- break;
- }
- do {
- if (intentos == 0) {
- result.success(false);
- break;
- }
- if (BlueService.getState() == EstadoConectado) {
- result.success(true);
- break;
- }
- for (Device dispositivo : BlueService.getDeviceList()) {
- if (dispositivo.deviceAddress.equals(mac)) {
- if (BlueService.getState() != EstadoConectado) {
- intentos--;
- BlueService.connect(dispositivo.deviceAddress);
- } else {
- result.success(true);
- }
- break;
- }
- }
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- System.out.print("No se puede detener el hilo");
- result.success(false);
- break;
- }
- if (BlueService.getState() == EstadoConectado) {
- result.success(true);
- break;
- }
- } while (BlueService.getState() != 3);
- break;
- case "printText":
- String texto = call.argument("text");
- if (texto == null || texto.trim().length() == 0) {
- result.success(false);
- break;
- }
- BlueService.printText(texto);
- result.success(true);
- break;
- case "printBitmap":
- byte[] mapa = call.argument("map");
- if (mapa == null) {
- result.success(false);
- break;
- }
- Bitmap bitmap = BitmapFactory.decodeByteArray(mapa, 0, mapa.length);
- // change transparent bitmap background to white
- Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
- Canvas canvas = new Canvas(newBitmap);
- canvas.drawColor(Color.WHITE);
- canvas.drawBitmap(bitmap, 0, 0, null);
- //crop bounding white color
- newBitmap = ImageUtils.cropBitmapToBoundingBox(newBitmap, Color.WHITE);
- // get inputStream from bitmap
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- newBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
- byte[] bitmapdata = bos.toByteArray();
- ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
- BlueService.printImage(BitmapFactory.decodeStream(bs));
- result.success(true);
- break;
- case "printBitmap2":
- byte[] mapa2 = call.argument("map");
- if (mapa2 == null) {
- result.success(false);
- break;
- }
- Bitmap bitmap2 = BitmapFactory.decodeByteArray(mapa2, 0, mapa2.length);
- BlueService.printImage2(bitmap2);
- result.success(true);
- break;
- case "disconnect":
- result.success(BlueService.disconnect());
- break;
- default:
- result.notImplemented();
- break;
- }
- }
- private int[] convertIntegers(List<Integer> integers) {
- int[] ret = new int[integers.size()];
- Iterator<Integer> iterator = integers.iterator();
- for (int i = 0; i < ret.length; i++) {
- ret[i] = iterator.next().intValue();
- }
- return ret;
- }
- }
|