Android Question Calling Java Class

prajinpraveen

Active Member
Licensed User
Longtime User
Hello Everyone,

I am trying to convert the following java code to B4A

1:
PrintTask printTask = new PrintTask();
printTask.setPrintBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.printer));
Printer.getInstance().startPrint(printTask, new PrinterCallback() {

2:
// PrinterTask.Java

package com.pos.device.printer;

import android.graphics.Bitmap;

public class PrintTask {
  public int mStep;
 
  public Bitmap mBitmap;
 
  public PrintTask() {
    throw new RuntimeException();
  }
 
  public int getGray() {
    throw new RuntimeException();
  }
 
  public void setGray(int gray) {
    throw new RuntimeException();
  }
 
  public void addFeedPaper(int step) {
    throw new RuntimeException();
  }
 
  public void setPrintCanvas(PrintCanvas printCanvas) {
    throw new RuntimeException();
  }
 
  public void setPrintBitmap(Bitmap origBitmap) {
    throw new RuntimeException();
  }

  public void setPrintBuffer(byte[] buffer) {
    throw new RuntimeException();
  }
}

B4X:
    Dim bitmap As B4XBitmap = TextToBitmap("Text to Bitmap", 20, xui.Color_Black, "CENTER")
    Dim jo As JavaObject
    Try
        jo = jo.InitializeNewInstance("com.pos.device.printer.PrintTask",Null)
        jo.RunMethod("setPrintBitmap", Array As Object(bitmap))
    Catch
        Log(LastException)
    End Try


java.lang.RuntimeException
    at com.pos.device.printer.PrintTask.<init>(PrintTask.java:20)
    at java.lang.Class.newInstance(Native Method)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:86)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.test.sfpayments.main._print(main.java:496)
    at com.test.sfpayments.main._activity_create(main.java:488)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at com.test.sfpayments.main.afterFirstLayout(main.java:121)
    at com.test.sfpayments.main.access$000(main.java:33)
    at com.test.sfpayments.main$WaitForLayout.run(main.java:99)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6121)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1059)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
(Exception) java.lang.Exception:  java.lang.RuntimeException

InitializeStatic initialises the class, but cannot run the SetPrintBitmap Method


3:
    Dim bitmap As B4XBitmap = TextToBitmap("Text to Bitmap", 20, xui.Color_Black, "CENTER")
    Dim jo As JavaObject
    Try
        jo = jo.InitializeStatic("com.pos.device.printer.PrintTask")
        jo.RunMethod("setPrintBitmap", Array As Object(bitmap))
    Catch
        Log(LastException)
    End Try
    
    Log(jo)
    
    Error occurred on line: 592 (Main)
java.lang.IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.test.sfpayments.main._print(main.java:496)
    at com.test.sfpayments.main._activity_create(main.java:488)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at com.test.sfpayments.main.afterFirstLayout(main.java:121)
    at com.test.sfpayments.main.access$000(main.java:33)
    at com.test.sfpayments.main$WaitForLayout.run(main.java:99)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6121)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1059)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
(Exception) java.lang.Exception:  java.lang.IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
(Class) class com.pos.device.printer.PrintTask

Any assistance will be greatly appreciated.

Thank you
 

DonManfred

Expert
Licensed User
Longtime User
PrintTask printTask = new PrintTask(); printTask.setPrintBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.printer)); Printer.getInstance().startPrint(printTask, new PrinterCallback() {
Where do you get this code from?

It is not inside the example project you recently uploaded.
There is no printtask involved in the example.
 
Upvote 0

prajinpraveen

Active Member
Licensed User
Longtime User
please check line 75 of DevicePrinter.java

2:
PrintCanvas canvas = buildCanvas(t);

        PrintTask task = new PrintTask();
        task.setGray(130);
        task.setPrintCanvas(canvas);

        int result = printData(task, ui);
        Log.e(TAG, "print finish,the result=" + result);
        return result == 0;


1739204753847.png
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Where did you find this printdemo? do you have the documentation for the api of the SDK?
 
Upvote 0

prajinpraveen

Active Member
Licensed User
Longtime User
unfortunately there is no documentation. The manufacturer gave us the printdemo as an example.
It works when installed (Android Studio)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
My understanding from the Java project,
first, the demo entry point is
\PrinterDemo\app\src\main\java\com\newpos\printDemo\MainActivity.java
which utilize the class from a subdirectory \PrinterDemo\app\src\main\java\com\newpos\printDemo\printer
The printer directory contains the following classes:
RecipetPrinter.java
TransLog.java
thus, the code in MainActivity contains the following line:
Java:
import com.newpos.printDemo.printer.RecipetPrinter;
import com.newpos.printDemo.printer.TransLog;
import com.newpos.printlib.ui.IPrinterUI;
In order for the demo to work, you also need to "wrap" or convert the functions inside them using JavaObject in B4X. Unless you compile them as jars and import using #AdditionalJars.

Then the demo project also required the code from com.newpos.printlib.* class.
The source code is inside
\PrinterDemo\printLib\src\main\java\com\newpos\printlib
Inside the printlib directory, there are the following:
print
ui
StringTools.java

Inside \PrinterDemo\printLib\src\main\java\com\newpos\printlib\print, there are the following classes:
DevicePrinter.java
FontSize.java
PrinterTextView.java
PrintManager.java
TicketFontSize.java

If we want to tackle the DevicePrinter.java, we can see the source is depends on the following classes:
B4X:
import com.newpos.printlib.ui.IPrinterUI;
import com.pos.device.printer.PrintCanvas;
import com.pos.device.printer.PrintTask;
import com.pos.device.printer.Printer;

And the line 2 to 4 are depending on the sdk.jar

So my suggestion is you need to convert all the Java classes to B4X first or compile them as jars then only you can make the demo works.
The classes are depending on another classes that they can't work alone.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
The sdk.jar is only for the development , you need to add uses-library section to manifest for runtime environment
B4X:
AddApplicationText(<uses-library android:name="com.pos.device" />)
 
Upvote 0
Top