B4J Question How to resize and save a captured bitmap without losing quality?

jroriz

Active Member
Licensed User
Longtime User
I'm capturing a small portion of the screen using the following code:
B4X:
Dim Bitmap1 As B4XBitmap = Capture(100, 100, 100, 100)
Bitmap1 = Bitmap1.Resize(Bitmap1.Width*4, Bitmap1.Height*4, True)

Dim Out As OutputStream
Out = File.OpenOutput("C:\MEAPAGUE", "Test.png", False)
Bitmap1.WriteToStream(Out, 100, "PNG")
Out.Close
I need to save the image in a larger size, but without losing quality. When I resize the bitmap, the image becomes pixelated or blurry. Is there a way to upscale the image with better quality or apply some kind of smoothing or interpolation?

Thanks in advance.
 

TILogistic

Expert
Licensed User
Longtime User
you can use this library

or in b4j javaobject
B4X:
Sub ResizeImage(original As Image, newWidth As Int, newHeight As Int) As Image
    Dim jo As JavaObject
    jo.InitializeStatic("java.awt.image.BufferedImage")
    Dim bufferedImage As JavaObject = jo.CreateNew(newWidth, newHeight, 2) ' 2 = BufferedImage.TYPE_INT_ARGB

    Dim g2d As JavaObject = bufferedImage.RunMethod("createGraphics", Null)
    g2d.RunMethod("setRenderingHint", Array(jo.GetField("KEY_INTERPOLATION"), jo.GetField("VALUE_INTERPOLATION_BICUBIC")))
    g2d.RunMethod("setRenderingHint", Array(jo.GetField("KEY_ANTIALIASING"), jo.GetField("VALUE_ANTIALIAS_ON")))
    g2d.RunMethod("drawImage", Array(original, 0, 0, newWidth, newHeight, Null))
    g2d.RunMethod("dispose", Null)

    Dim resizedImage As Image = bufferedImage
    Return resizedImage
End Sub
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
you can use this library

or in b4j javaobject
B4X:
Sub ResizeImage(original As Image, newWidth As Int, newHeight As Int) As Image
    Dim jo As JavaObject
    jo.InitializeStatic("java.awt.image.BufferedImage")
    Dim bufferedImage As JavaObject = jo.CreateNew(newWidth, newHeight, 2) ' 2 = BufferedImage.TYPE_INT_ARGB

    Dim g2d As JavaObject = bufferedImage.RunMethod("createGraphics", Null)
    g2d.RunMethod("setRenderingHint", Array(jo.GetField("KEY_INTERPOLATION"), jo.GetField("VALUE_INTERPOLATION_BICUBIC")))
    g2d.RunMethod("setRenderingHint", Array(jo.GetField("KEY_ANTIALIASING"), jo.GetField("VALUE_ANTIALIAS_ON")))
    g2d.RunMethod("drawImage", Array(original, 0, 0, newWidth, newHeight, Null))
    g2d.RunMethod("dispose", Null)

    Dim resizedImage As Image = bufferedImage
    Return resizedImage
End Sub
Thanks for the attention.
There are a few errors in the code...

For instace:
Dim bufferedImage As JavaObject = jo.CreateNew(newWidth, newHeight, 2) ' 2 = BufferedImage.TYPE_INT_ARGB
(unknown member: createnew)

I tried a few thinks, but still with errors:

B4X:
Sub ResizeImage(original As Image, newWidth As Int, newHeight As Int) As Image
'    Dim jo As JavaObject
    Dim bufferedImage As JavaObject
    bufferedImage.InitializeNewInstance("java.awt.image.BufferedImage", Array(newWidth, newHeight, 2)) ' 2 = BufferedImage.TYPE_INT_ARGB

    Dim g2d As JavaObject = bufferedImage.RunMethod("createGraphics", Null)

    Dim renderingHints As JavaObject
    renderingHints.InitializeStatic("java.awt.RenderingHints")
    g2d.RunMethod("setRenderingHint", Array(renderingHints.GetField("KEY_INTERPOLATION"), renderingHints.GetField("VALUE_INTERPOLATION_BICUBIC")))
    g2d.RunMethod("setRenderingHint", Array(renderingHints.GetField("KEY_ANTIALIASING"), renderingHints.GetField("VALUE_ANTIALIAS_ON")))

    ' Converter javafx.scene.image.Image para java.awt.Image
    Dim swingFXUtils As JavaObject
    swingFXUtils.InitializeStatic("javafx.embed.swing.SwingFXUtils")
    Dim awtImage As JavaObject = swingFXUtils.RunMethod("fromFXImage", Array(original, Null))

    g2d.RunMethod("drawImage", Array(awtImage, 0, 0, newWidth, newHeight, Null))
    g2d.RunMethod("dispose", Null)

    ' Converter de volta para javafx.scene.image.Image
    Dim resizedImage As Image = swingFXUtils.RunMethod("toFXImage", Array(bufferedImage, Null))
    Return resizedImage
End Sub
 
Upvote 0
Top