Android Question Color range in PNG graphic file using DrawText

Cesaral

Member
I have a very simpe code with a mutable bitmap and a canvas to be able to write a word on the bitmap.

Then I use Drawtext to write a word (in black color) in that bitmap and save it to disk as PNG.

When I open the file with a graphic editor and ZOOM it to check the colors of the pixels, I can see more colors apart from the pure black (RGB(0,0,0)). Here is the result of the ZOOM:

Imagen.jpg


Most of the letter is pure black but at the borders we can see other colors such as RGB(153,153,153) which is a type of grey.

How can I generate the PNG files exactly with the color that I took for writting the word?

Thanks!
 

Cesaral

Member
Thanks for your fast answer Erel.

I am using a Canvas...not the B4XCanvas .

Even with Canvas.AntiAlias = False the problem is the same. But here is a clue:

In my program I draw a rectange and a text. With Canvas.AntiAlias = False, the text has blur when you zoom it, but the rectange does not have it:

1615450549531.png

When you set Canvas.AntiAlias = True, both the text and the rectangle have blur:

1615450674349.png
 

Attachments

  • 1615450510979.png
    1615450510979.png
    834 bytes · Views: 90
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. The code for DrawText always enables AA.

You can use this code instead:
B4X:
Sub DrawTextNoAA (cvs As Canvas, Text As String, X As Float, Y As Float, Font As Typeface, TextSize As Float, Color As Int, Alignment As String)
    Dim jo As JavaObject = cvs
    Dim canvas As JavaObject = jo.GetField("canvas")
    Dim paint As JavaObject = jo.GetField("paint")
    paint.RunMethod("setTextAlign", Array(Alignment))
    TextSize = TextSize * GetDeviceLayoutValues.Scale
    paint.RunMethod("setTextSize", Array(TextSize))
    paint.RunMethod("setColor", Array(Color))
    Dim StrokeWidth As Float = 0
    paint.RunMethod("setStrokeWidth", Array(StrokeWidth))
    paint.RunMethod("setStyle", Array("FILL"))
    paint.RunMethod("setTypeface", Array(Font))
    canvas.RunMethod("drawText", Array(Text, X, Y, paint))
End Sub
 
Upvote 0
Top