Wish Render image to Canvas without smoothing

Jim Brown

Active Member
Licensed User
Longtime User
I want to render an image to a canvas but without the smoothing taking place.
According to the JavaFX API's the image class has a 'isSmooth' option.
Can this be implemented?

isSmooth
public final boolean isSmooth()
Indicates whether to use a better quality filtering algorithm or a faster one when scaling this image to fit within the bounding box provided by width and height.
If not initialized or set to true a better quality filtering will be used, otherwise a faster but lesser quality filtering will be used.
 

stevel05

Expert
Licensed User
Longtime User
You could try reading the value using JavaObject:

B4X:
Dim IvJo As JavaObject = ImageView1
Log(IvJo.RunMethod("isSmooth",Null))

as the default value is dependent on the platform configuration.

If you need to set it you can use:

B4X:
IvJo.RunMethod("setSmooth",Array As Object(False))

I've just tried it an it is set to true on my PC setup.

Although it may not help you if you are rendering to a canvas.

Edit: you can also set the option on an image view in Scene Builder
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Just checked the image object, it doesn't seem to be changable outside of the image constructor.
 

Jim Brown

Active Member
Licensed User
Longtime User
Thanks Steve. Yes, I am using the Image type, and the "setSmooth" option is not available.
What I am doing is rendering an image to a canvas, but it always appears smooth. I want a pixel-sharp unfiltered look.
The problem is, when I later save the canvas the image is blurred.
 

stevel05

Expert
Licensed User
Longtime User
OK, the good news is that the B4j image object is not wrapped, so we can create out own using JavaObject, and use it just the same.

This will do it:

B4X:
Dim I As JavaObject
I.InitializeNewInstance("javafx.scene.image.Image",Array As Object(File.GetUri(File.DirAssets,"color.jpg"),Width,Height,True,False))
Cnv.DrawImage(I,10Dip,10Dip,200Dip,200Dip)

Width and Height need to be Variables of type Double to be recognized.

My testapp is attached.
 

Attachments

  • IVT.zip
    5 KB · Views: 280
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Expert tip:

SS-2014-07-06_07.52.32.png


If you check the XML definition of Image class (ImageWrapper) you can see that there is an ObjectWrapper element. This means that ImageWrapper is a thin wrapper over javafx.scene.image.Image. The compiler will automatically convert between the two.
 

Jim Brown

Active Member
Licensed User
Longtime User
Thanks for your efforts Steve. Donation on its way!

There seems to be a fundamental problem though. Images now look a sharper when rendered at their normal size but if I zoom in (basically rendering the image to the canvas at a larger scaled size) the image still renders blurred.

Here is an example. My B4J app on the left. Windows photo viewer on the right.

sm_blurred_v_original_zpsc0f8b0bf.png



My image load routine is now:
B4X:
' Loads images with PreserveRatio=FALSE and Smooth=FALSE
Sub Image_Load(dir As String,name As String) As JavaObject
    Dim tmpImg As Image = fx.LoadImage(dir,name)
    Dim i As JavaObject
    i.InitializeNewInstance("javafx.scene.image.Image",Array As Object(File.GetUri(dir,name),tmpImg.Width,tmpImg.Height,False,False))
    Return i
End Sub

The draw routine is a simple myCanvas.DrawImage(img,x,y,img.Width*zoom,img.Height*zoom)

It's looking like Canvas is not a viable option for pure image manipulation. I found a related issue here too.

The best bet according to the results there is to use PixelReader and PixelWriter.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi Jim. no problems, and thanks for the donation.

Yes, that is always going to be a problem when scaling.

I've converted the code in that post to B4j, it's in the attached project. See if it is an improvement.

Steve
 

Attachments

  • IVT1.zip
    5.3 KB · Views: 311
Last edited:

Jim Brown

Active Member
Licensed User
Longtime User
Sweet. I might be able to get somewhere with my app now.
Many thanks for your code.
 
Top