Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a logic that support a img size > 256 as well. also added some code to improve image quality #27

Open
dharmik-valani opened this issue Oct 1, 2022 · 0 comments

Comments

@dharmik-valani
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/thermal-printer-cordova-plugin/src/android/ThermalPrinterCordovaPlugin.java b/node_modules/thermal-printer-cordova-plugin/src/android/ThermalPrinterCordovaPlugin.java
index b2aa637..9dd25bb 100644
--- a/node_modules/thermal-printer-cordova-plugin/src/android/ThermalPrinterCordovaPlugin.java
+++ b/node_modules/thermal-printer-cordova-plugin/src/android/ThermalPrinterCordovaPlugin.java
@@ -80,9 +80,26 @@ public class ThermalPrinterCordovaPlugin extends CordovaPlugin {
     private void bytesToHexadecimalString(CallbackContext callbackContext, JSONObject data) throws JSONException {
         EscPosPrinter printer = this.getPrinter(callbackContext, data);
         try {
-            byte[] bytes = (byte[]) data.get("bytes");
+             byte[] bytes = (byte[]) data.get("bytes");
             Bitmap decodedByte = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
-            callbackContext.success(PrinterTextParserImg.bitmapToHexadecimalString(printer, decodedByte));
+            int targetWidth = 383;
+                    //(int) (201 * 5.6 / 2);
+            int targetHeight = (int) ((201 * 5.6 / 2.54) * 2);
+                    //Math.round(decodedByte.getHeight() * targetWidth / decodedByte.getWidth());
+
+            //(int) ((201 * 5.6 / 2.54) * 3)
+            Bitmap resizedBitmap = Bitmap.createScaledBitmap(decodedByte, targetWidth ,targetHeight , true);
+            decodedByte.recycle();
+
+            int width = resizedBitmap.getWidth(), height = resizedBitmap.getHeight();
+            StringBuilder textToPrint = new StringBuilder();
+            for(int y = 0; y < height; y += 256) {
+                Bitmap bitmap = Bitmap.createBitmap(resizedBitmap, 0, y, width, (y + 256 >= height) ? height - y : 256);
+                textToPrint.append("[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, bitmap) + "</img>\n");
+            }
+
+            textToPrint.append("[C]Printed!!!\n");
+            callbackContext.success(String.valueOf(textToPrint));
         } catch (Exception e) {
             callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
                 put("error", e.getMessage());

This issue body was partially generated by patch-package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant