Giter Club home page Giter Club logo

printama's Introduction

Printama

Android library for bluetooth thermal printer.
Tested to many 2 inch bluetooth thermal printers.

License Pull request Github

Screenshots

Payment Receipt Print Text and Images
Photo Photo Print Result
Screen Layout Screen Layout Print Result

Support me:

I notice that some of you are very happy with this library and some of you want to give support. But I also noticed some of you want to support me anonymously. Well then, I appreciate it if you bought me a coffee โ˜•

โ˜• Link
buymeacoffee https://www.buymeacoffee.com/anggastudio
ko-fi https://ko-fi.com/anggastudio

Usage

Currently still in Alpha. Make sure to use Java 8+ configuration. Documentation is in progress. Just take a look at sample project as an example.

But here for an insight: Permissions in your Manifest

<uses-permission android:name="android.permission.BLUETOOTH_SCAN"  android:usesPermissionFlags="neverForLocation"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

Permission check in your Activity. Call checkPermissions() in your onCreate() method

private static String[] PERMISSIONS_STORAGE = {
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE,
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
    Manifest.permission.BLUETOOTH_SCAN,
    Manifest.permission.BLUETOOTH_CONNECT,
    Manifest.permission.BLUETOOTH_PRIVILEGED
};

private static String[] PERMISSIONS_LOCATION = {
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
    Manifest.permission.BLUETOOTH_SCAN,
    Manifest.permission.BLUETOOTH_CONNECT,
    Manifest.permission.BLUETOOTH_PRIVILEGED
};

private void checkPermissions() {
    int permission1 = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int permission2 = ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN);

    if (permission1 != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                this,
                PERMISSIONS_STORAGE,
                1
        );
    } else if (permission2 != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                PERMISSIONS_LOCATION,
                1
        );
    }
}

Show dialog to choose bonded device (printer list) bind your device initially from the bluetooth config:

Printama.showPrinterList(this, printerName -> {
    // Your code here
});

Show dialog to choose bonded device (Custom Color)

Printama.showPrinterList(this, R.color.colorBlue, printerName -> {
    if (connectedPrinter != null) {
        // Your code here
    }
});

Prepare the text

String text = "-------------\n" +
        "This will be printed\n" +
        "Left aligned\n" + // or Center or Right
        "cool isn't it?\n" +
        "------------------\n";

Print Text LEFT aligned

Printama.with(context).connect(printama -> {
    printama.printText(Printama.LEFT, text);
    printama.close();
});

Print Text CENTER aligned

Printama.with(context).connect(printama -> {
    printama.printText(Printama.CENTER, text);
    printama.close();
});

Print Text RIGHT aligned

Printama.with(context).connect(printama -> {
    printama.printText(Printama.RIGHT, text);
    printama.close();
});

Print Text JUSTIFY aligned

Printama.with(this).connect(printama -> {
    printama.printTextJustify("text1", "text2");
    printama.printTextJustify("text1", "text2", "text3");
    printama.printTextJustify("text1", "text2", "text3", "text4");

    printama.printTextJustifyBold("text1", "text2");
    printama.printTextJustifyBold("text1", "text2", "text3");
    printama.printTextJustifyBold("text1", "text2", "text3", "text4");

    printama.setNormalText();
    printama.feedPaper();
    printama.close();
});

Print Text with format

Printama.with(this).connect(printama -> {
    printama.setSmallText();
    printama.printText("small___________");
    printama.printTextln("TEXTtext");

    printama.setNormalText();
    printama.printText("normal__________");
    printama.printTextln("TEXTtext");

    printama.printTextNormal("bold____________");
    printama.printTextlnBold("TEXTtext");

    printama.setNormalText();
    printama.printTextNormal("tall____________");
    printama.printTextlnTall("TEXTtext");

    printama.printTextNormal("tall bold_______");
    printama.printTextlnTallBold("TEXTtext");

    printama.printTextNormal("wide____________");
    printama.printTextlnWide("TEXTtext");

    printama.printTextNormal("wide bold_______");
    printama.printTextlnWideBold("TEXTtext");

    printama.printTextNormal("wide tall_______");
    printama.printTextlnWideTall("TEXTtext");

    printama.printTextNormal("wide tall bold__");
    printama.printTextlnWideTallBold("TEXTtext");

    printama.printTextNormal("underline_______");
    printama.setUnderline();
    printama.printTextln("TEXTtext");

    printama.printTextNormal("delete line_____");
    printama.setDeleteLine();
    printama.printTextln("TEXTtext");

    printama.setNormalText();
    printama.feedPaper();
    printama.close();
});

Print Bitmap / Image LEFT aligned

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Printama.with(context).connect(printama -> {
    printama.printImage(Printama.LEFT, bitmap, 200);
    printama.close();
});

Print Bitmap / Image CENTER aligned

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Printama.with(context).connect(printama -> {
    printama.printImage(Printama.CENTER, bitmap, 200);
    printama.close();
});

Print Bitmap / Image RIGHT aligned

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Printama.with(context).connect(printama -> {
    printama.printImage(Printama.RIGHT, bitmap, 200);
    printama.close();
});

Print Bitmap / Image FULL size

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Printama.with(context).connect(printama -> {
    printama.printImage(bitmap, Printama.FULL_WIDTH);
    printama.close();
});

Print Bitmap / Image ORIGINAL size

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Printama.with(context).connect(printama -> {
    printama.printImage(bitmap); // original size, centered as default
    printama.close();
});

Print Drawable Vector

Bitmap bitmap = Printama.getBitmapFromVector(this, R.drawable.ic_launcher_background);
Printama.with(this).connect(printama -> {
    printama.printImage(bitmap, Printama.ORIGINAL_WIDTH);
    printama.close();
});

Print Layout View

Using print layout view, you can design your receipt on your layout XML or DSL, and pass the root view as a parameter:

View view = findViewById(R.id.root_view);
Printama.with(this).connect(printama -> {
    printama.printFromView(view);
    printama.close();
});

Feature

  • Dialog to choose bonded bluetooth device list.
  • Print Text with LEFT, CENTER, or RIGHT Alignment.
  • Print Text with JUSTIFY Alignment.
  • Print auto grayscale Bitmap with Custom width and Alignment.
  • Print photo (grayscaled)
  • Print your android screen or layout by passing the root view
  • Print vector drawable
  • Tested with 2 inch Bluetooth Thermal Printers.

Contributing

You can simply :

  • a pull request, or
  • raise an issue ticket, or
  • request additional feature by raise a ticket.

Download

Gradle:

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Step 2. Add the dependency

version:

dependencies {
  implementation 'com.github.anggastudio:Printama:<version>'

Other like Maven, SBT, Leiningen:

just visit the jitpack page click here -->

License

Apache License 2.0

Thanks To:

Next Step:

  • release 1.0.0 to make it more stable
  • enhance documentation
  • create a video tutorial
  • 3 inches printer
  • testing on more printer brands
  • (still in my mind) migrate to kotlin

printama's People

Contributors

anggastudio avatar derohimat avatar isfaaghyth avatar kucingapes avatar salihuumar75 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

printama's Issues

2 printer device?

can i connect more than one print?
Just create another instance of printama?

Someone can help me?

Build error after adding the gradles

Task failed with an exception.

  • What went wrong:
    Execution failed for task ':app:mergeDebugNativeLibs'.

Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
Could not find com.github.anggastudio:Printama:0.9.7.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/github/anggastudio/Printama/0.9.7/Printama-0.9.7.pom
- https://repo.maven.apache.org/maven2/com/github/anggastudio/Printama/0.9.7/Printama-0.9.7.pom
- https://jcenter.bintray.com/com/github/anggastudio/Printama/0.9.7/Printama-0.9.7.pom
Required by:
project :app

W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

@derohimat @isfaaghyth @utsmannn @anggastudio
W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

BluetoothDevice device = bluetoothDevices[0];
            UUID uuid;
            if (device != null) {
                ParcelUuid[] uuids = device.getUuids();
                uuid = (uuids != null && uuids.length > 0) ? uuids[0].getUuid() : UUID.randomUUID();
            } else {
                return null;
            }
            BluetoothSocket socket = null;
            boolean connected = true;
            try {
                socket = device.createRfcommSocketToServiceRecord(uuid);
                socket.connect();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e2) {
                connected = false;
            }
            return connected ? socket : null;

How can I set bitmap/image width as printer's paper width?

Hi, I just mailed you about this issue.

You set PRINTER_WIDTH = 384 (which is fixed). That doesn't care about the actual printer paper width. Instead of that, can't we detect printer paper size from socket data? or you can add a paper size option on choose printer dialog. So, if we set 58mm/80mm/custom width, then it will be used as printer width throughout the app for that selected printer.

DPI and Font Style

Hi, I'm just wondering how to Set the DPI and Font Style

I download a apk "RawBT" it can change the font to True Type Font and DPI to 304

Printed Images Changing into Unreadable Character

How its happened

Before this happened, I tried to print an image (logo) with several text below it, and its works verry well with no issue, but after I added QR Code (Bitmap) to be printed with, the logo just printed half and half more becomes unreadable character (image below)

Here it is the code that i used for :

val logo = Printama.getBitmapFromVector(activity, R.drawable.logo_black)
val nota = "Some Text"
Printama.with(activity).connect({ printama: Printama ->
      printama.printImage(logo, 200)
      printama.addNewLine()
      printama.printTextln(Printama.CENTER, "Title Text")
      printama.setNormalText()
      printama.printTextln(
          Printama.CENTER,
          "Some Text"
      )
    printama.printDashedLine()

    val writer = QRCodeWriter()
    val bitMatrix = writer.encode(nota, BarcodeFormat.QR_CODE, 300, 300)
    val width = bitMatrix.width
    val height = bitMatrix.height
    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
    for (x in 0 until width) {
        for (y in 0 until height) {
            bitmap.setPixel(x, y, if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE)
        }
    }

    bitmap?.let {
        printama.printImage(it)
    }

    printama.addNewLine()
    printama.feedPaper()
    printama.close()
}) { message: String? -> activity.toast(message) }

WhatsApp Image 2021-04-25 at 15 47 42

System.err: java.io.IOException: Broken pipe

My old application is dependent on this library, but since some time it showing error and worked now , The bluetooth printer is connected fine but not print the receipt, the every function of printer util like printtext, printline etc. I cloned the printama android demo project and showing same error. so Please resolve this issue.

Here is the Android logs:-

2023-06-05 19:00:30.583 18376-18376/com.anggastudio.sample W/System.err: java.io.IOException: Broken pipe
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at android.net.LocalSocketImpl.writeba_native(Native Method)
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at android.net.LocalSocketImpl.access$500(LocalSocketImpl.java:37)
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at android.net.LocalSocketImpl$SocketOutputStream.write(LocalSocketImpl.java:144)
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:786)
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:79)
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at java.io.OutputStream.write(OutputStream.java:122)
2023-06-05 19:00:30.584 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.PrinterUtil.printUnicode(PrinterUtil.java:94)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.PrinterUtil.addNewLine(PrinterUtil.java:166)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.PrinterUtil.feedPaper(PrinterUtil.java:290)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.Printama.feedPaper(Printama.java:184)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.Printama.lambda$printTest$2(Printama.java:127)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.-$$Lambda$Printama$RmuijsPIw_E46bsFguHyZkOvvNI.onConnected(Unknown Source:0)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.Printama.lambda$connect$0$Printama(Printama.java:102)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.-$$Lambda$Printama$8huuBLTLR2MZ4RxuJjF9x13epEw.onConnected(Unknown Source:4)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.PrinterUtil$1.onConnected(PrinterUtil.java:63)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.PrinterUtil$ConnectAsyncTask.onPostExecute(PrinterUtil.java:327)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at com.anggastudio.printama.PrinterUtil$ConnectAsyncTask.onPostExecute(PrinterUtil.java:294)
2023-06-05 19:00:30.585 18376-18376/com.anggastudio.sample W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:771)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at android.os.AsyncTask.access$900(AsyncTask.java:199)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at android.os.Looper.loopOnce(Looper.java:226)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at android.os.Looper.loop(Looper.java:313)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8751)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
2023-06-05 19:00:30.586 18376-18376/com.anggastudio.sample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Screenshot_3

Error when changed android sdk to 32

Please help me! when i changed to android studio debugger says : * What went wrong:
Execution failed for task ':printama:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

Cannot print on Embedded printer

I am testing this library on JP-Q2i POS Terminal PDA (Embedded printer is configured as Bluetooth device)
but I cannot print a text.

Printama.with(this).connect(new Printama.OnConnected() {
                    @Override
                    public void onConnected(Printama printama) {
                        printama.printText("TEST");
                    }
                });

Library connect with MPT-II but not start a print

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.