Android Question Getting and setting the printer resolution

MitchBu

Well-Known Member
Licensed User
Longtime User
I just experimented with both the older canvas.b4a project that I was referring to in the OP, and the printing library.

Unfortunately, none of them print with sufficient resolution for what I need.

I need a minimum of 600 dpi. At present, I believe the printed resolution is 72 dpi, barely what an antique dot matrix printer from the eighties would do.

I got an app in the Play Store that prints at a much higher resolution, so I know it is possible to get much better results.

I found this in the Android Reference that apparently addresses directly my concern :
https://developer.android.com/reference/android/print/PrintAttributes.Resolution

Taking as example the program that prints using JavaObject, it seems to work somewhat like mediaSize.

Unfortunately, I am not familiar enough with JavaObject to create the code.

And I don't see how this could be applied to the Printer class.

Unless I find a way to print at minimum 600 dpi, this will stop any chance of completing my current project.

I will appreciate any assistance.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub Globals
   Private xui As XUI
   Private Print As Printer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Print.Initialize("")
   PrintBmp(File.DirAssets, "test.gif")
End Sub

Sub PrintBmp (Dir As String, FileName As String)
   Dim wv As WebView
   wv.Initialize("wv")
   wv.LoadUrl(xui.FileUri(Dir, FileName))
   Activity.AddView(wv, 0, 0, 100%x, 100%y)
   Wait For wv_PageFinished (Url As String)
   wv.RemoveView
   Dim PrintAttributesBuilder As JavaObject
   PrintAttributesBuilder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim resolution As JavaObject
   resolution.InitializeNewInstance("android.print.PrintAttributes.Resolution", Array("id1", "id2", 600, 600)) 'dpi
   PrintAttributesBuilder.RunMethod("setResolution", Array(resolution))
   Dim jo As JavaObject = Print
   jo.RunMethod("PrintWebView", Array("JobName", wv, PrintAttributesBuilder.RunMethod("build", Null)))
End Sub

Depends on JavaObject and XUI libraries.
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thank you so much for that code.

I tried to print a document that fits in one page with the regular printer class, and get 4 pages. The picture sports a pattern with lines one inch apart, and the printed result is twice as big.

It looks as if instead of printing at the same size with a higher resolution, it prints at the same resolution but bigger.

Maybe we need to get the printer resolutions with
https://developer.android.com/reference/android/print/PrinterCapabilitiesInfo
and https://developer.android.com/reference/android/print/PrinterCapabilitiesInfo.Builder

Or maybe we need to set the printer to fit to paper size ? I could not find where that was in the Google documentation, though.
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I have used the code you posted to slightly modify the Canvas project I had set aside since 2016 and our discussion in https://www.b4x.com/android/forum/threads/cloud-printing-margins.72946/#content.

Now I get one page printed, with the 600 dpi resolution.

Here is where I am now:

B4X:
Sub PrintBitmap (mybmp As Bitmap)
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim photoPrinter As JavaObject
    photoPrinter.InitializeNewInstance("android.support.v4.print.PrintHelper", Array(ctxt))
 
   '--- copied from Erel's new code
    Dim PrintAttributesBuilder As JavaObject
    PrintAttributesBuilder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
    Dim resolution As JavaObject
    resolution.InitializeNewInstance("android.print.PrintAttributes.Resolution", Array("id1", "id2", 600, 600)) 'dpi
    PrintAttributesBuilder.RunMethod("setResolution", Array(resolution))
    Dim MediaSize As JavaObject
    MediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize")
    'PrintAttributesBuilder.RunMethod("setMediaSize", Array(MediaSize.GetField("ISO_A4")))
    PrintAttributesBuilder.RunMethod("setMediaSize", Array(MediaSize.GetField("NA_LETTER")))
   '---
 
   If photoPrinter.RunMethod("systemSupportsPrint", Null) = True Then
     'FIT = 1, FILL = 2
     photoPrinter.RunMethod("setScaleMode", Array(1))
     photoPrinter.RunMethod("printBitmap", Array("Printer Job Name", _
       mybmp, Null))
   Else
     Log("Printing not supported.")
    End If
End Sub

This is the best result I have got yet :)
 
Last edited:
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Never mind. I experimented with the printing library. It appears when printing PDF, it uses the full resolution of the printer.

Better yet, it does not have the margins that got in the way of my printing on top of the page.

It seems I finally found exactly what I needed to complete my project :)

Thank you Erel :)
 
Upvote 0
Top