From 21be67b99df39d1a614139caeb0008657fcf362c Mon Sep 17 00:00:00 2001 From: Alexander Kirsch Date: Tue, 18 Nov 2025 17:50:25 +0100 Subject: [PATCH 1/2] Add Base64 compat helper --- .../CapacitorThermalPrinterPlugin.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java b/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java index be3cdc6..caa42d8 100644 --- a/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java +++ b/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java @@ -13,7 +13,6 @@ import android.graphics.BitmapFactory; import android.os.Build; import android.util.Log; -import androidx.annotation.RequiresApi; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.PermissionState; @@ -50,7 +49,6 @@ import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Base64; import java.util.List; import org.json.JSONException; @@ -456,7 +454,6 @@ public void text(PluginCall call) { call.resolve(); } - @RequiresApi(api = Build.VERSION_CODES.O) @PluginMethod public void image(PluginCall call) { String image = call.getString("image"); @@ -465,20 +462,19 @@ public void image(PluginCall call) { try { if (image != null) { - byte[] d = Base64.getDecoder().decode(image.substring(image.indexOf(",") + 1)); + byte[] d = decodeBase64Compat(image.substring(image.indexOf(",") + 1)); cmd.append(cmd.getBitmapCmd(bitmapSetting, BitmapFactory.decodeByteArray(d, 0, d.length))); } } catch (SdkException ignored) {} call.resolve(); } - @RequiresApi(api = Build.VERSION_CODES.O) @PluginMethod public void raw(PluginCall call) { String base64 = call.getString("data"); if (base64 != null) { try { - cmd.append(Base64.getDecoder().decode(base64)); + cmd.append(decodeBase64Compat(base64)); } catch (Exception ignored) { call.reject("Invalid Base64"); return; @@ -684,6 +680,18 @@ private JSArray getJsonDevices() { return array; } + private byte[] decodeBase64Compat(String data) { + if (data == null) { + return new byte[0]; + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return java.util.Base64.getDecoder().decode(data); + } + + return android.util.Base64.decode(data, android.util.Base64.DEFAULT); + } + @SuppressLint("MissingPermission") @Override public void printerObserverCallback(PrinterInterface printerInterface, int state) { From fbf1acca45ba8b074aa4a621fe9d17d276b5dfb9 Mon Sep 17 00:00:00 2001 From: wemotion Team Date: Tue, 18 Nov 2025 17:58:54 +0100 Subject: [PATCH 2/2] Update android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../bluetooth_print/CapacitorThermalPrinterPlugin.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java b/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java index caa42d8..1e05d0e 100644 --- a/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java +++ b/android/src/main/java/com/malik12tree/bluetooth_print/CapacitorThermalPrinterPlugin.java @@ -465,7 +465,11 @@ public void image(PluginCall call) { byte[] d = decodeBase64Compat(image.substring(image.indexOf(",") + 1)); cmd.append(cmd.getBitmapCmd(bitmapSetting, BitmapFactory.decodeByteArray(d, 0, d.length))); } - } catch (SdkException ignored) {} + } catch (SdkException ignored) { + } catch (IllegalArgumentException e) { + call.reject("Invalid Base64 image data"); + return; + } call.resolve(); }