B4J Question [Solved] B4XCanvas DrawBitmap causes loss of quality

CaptKronos

Active Member
Licensed User
Longtime User
I have only just noticed that the following code causes the bitmap's quality to be reduced. It's almost like it has been blurred. Can someone point me to the solution?
Thanks.

B4X:
    Dim bc As BitmapCreator
    Dim bmp As B4XBitmap= xui.LoadBitmap(File.DirAssets, "test.jpg")
    bc.Initialize(bmp.Width, bmp.Height)
    bc.CopyPixelsFromBitmap(bmp)
    ImageView1.SetBitmap(bc.Bitmap) 'looks good here

    BasePanel=xui.CreatePanel("")
    BasePanel.SetLayoutAnimated(0,0,0,bmp.Width,bmp.Height)
    Dim cnv As B4XCanvas
    cnv.Initialize(BasePanel)
    cnv.DrawBitmap(bc.Bitmap, cnv.TargetRect)
    ImageView1.SetBitmap(cnv.CreateBitmap) 'looks bad here'

beforeDrawBitmap.jpg
afterDrawBitmap.jpg
 
Solution
Just discovered the solution to this issue here: https://www.b4x.com/android/forum/threads/disabling-anti-aliasing-on-text.159711/post-980525

Referring back to the code in my opening post:

B4X:
    Dim bc As BitmapCreator
    Dim bmp As B4XBitmap= xui.LoadBitmap(File.DirAssets, "test.jpg")
    bc.Initialize(bmp.Width, bmp.Height)
    bc.CopyPixelsFromBitmap(bmp)
    ImageView1.SetBitmap(bc.Bitmap) 'looks good here
    BasePanel=xui.CreatePanel("")
    BasePanel.SetLayoutAnimated(0,0,0,bmp.Width,bmp.Height)
    Dim cnv As B4XCanvas
    cnv.Initialize(BasePanel)

    '**** add the following line *****
    GetNativeCanvas(cnv).As(JavaObject).RunMethodjo("getGraphicsContext2D",Null).RunMethod("setImageSmoothing",Array(False))...

CaptKronos

Active Member
Licensed User
Longtime User
Can you upload a small project with this image and the drawing code?
I have attached the test project. The actual drawing code is part of a big project, so I can't upload it. But if the attached doesn't help, then I can cobble something together to include a drawing section.
 

Attachments

  • testBitmapCopy.zip
    15.7 KB · Views: 77
Upvote 0

CaptKronos

Active Member
Licensed User
Longtime User
Just realised that the version of the project I uploaded had a rect with 0.5 added to it. (I did this because I read somewhere that making sure coordinates are not integers prevents anti-aliasing which I thought might have been the cause of what I'm seeing.) The attached is a version without the 0.5, however, both projects create the same result.
 

Attachments

  • testBitmapCopy.zip
    15.7 KB · Views: 89
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Tried bigger size of the panel:
B4X:
BasePanel.SetLayoutAnimated(0,0,0,bmp.Width*5, bmp.Height*5)
and no this blurring...

No idea why
 
Upvote 0

teddybear

Well-Known Member
Licensed User
There is no different between B4XCanvas DrawTBitmap and BitmapCreator as long as the imageview dimesion is the same as the bitmaps'.
 

Attachments

  • testBitmapCopy.zip
    15.7 KB · Views: 74
Upvote 0

CaptKronos

Active Member
Licensed User
Longtime User
Teddybear: I'm not sure if you are saying that you can't see a difference, but look carefully at your ImageView2 and you should be able to see that it is slightly blurred compared to your ImageView1.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
At my test it's the same strange problem.

B4X:
Private Sub Button1_Click
    Dim bc As BitmapCreator
    Dim bmp As B4XBitmap= xui.LoadBitmap(File.DirAssets, "test.jpg")
    ImageView1.Width=bmp.Width
    ImageView1.Height=bmp.Height
    bc.Initialize(bmp.Width, bmp.Height)
    bc.CopyPixelsFromBitmap(bmp)
    ImageView1.SetBitmap(bc.Bitmap)
'    xui.MsgboxAsync("looks good at this point...", "")
'    Wait For Msgbox_Result (Result As Int)
    ImageView2.Width=bmp.Width
    ImageView2.Height=bmp.Height
    BasePanel=xui.CreatePanel("")
 
    BasePanel.SetLayoutAnimated(0,0,0,bmp.Width,bmp.Height)   'AS IS
 
    Dim cnv As B4XCanvas
    cnv.Initialize(BasePanel)
    cnv.DrawBitmap(bc.Bitmap, cnv.TargetRect)
    ImageView2.SetBitmap(cnv.CreateBitmap)
End Sub
1746422036127.png

B4X:
Private Sub Button1_Click
    Dim bc As BitmapCreator
    Dim bmp As B4XBitmap= xui.LoadBitmap(File.DirAssets, "test.jpg")
    ImageView1.Width=bmp.Width
    ImageView1.Height=bmp.Height
    bc.Initialize(bmp.Width, bmp.Height)
    bc.CopyPixelsFromBitmap(bmp)
    ImageView1.SetBitmap(bc.Bitmap)
'    xui.MsgboxAsync("looks good at this point...", "")
'    Wait For Msgbox_Result (Result As Int)
    ImageView2.Width=bmp.Width
    ImageView2.Height=bmp.Height
    BasePanel=xui.CreatePanel("")
    
    BasePanel.SetLayoutAnimated(0,0,0,bmp.Width * 5,bmp.Height * 5)   'bigger
    
    Dim cnv As B4XCanvas
    cnv.Initialize(BasePanel)
    cnv.DrawBitmap(bc.Bitmap, cnv.TargetRect)
    ImageView2.SetBitmap(cnv.CreateBitmap)
End Sub

1746422093287.png
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
other than 1.0?
Yes, 1.25

B4X:
BasePanel.SetLayoutAnimated(0,0,0,bmp.Width * Scale,bmp.Height * Scale)    'solving
but, maybe it should be added into the libs ?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Last edited:
Upvote 0
Top