B4J Library [B4X] [XUI] xResizeAndCrop

klaus

Expert
Licensed User
Longtime User
To get the cropped image i need a cropped panel, but why?
You don't need a Panel nor Pane!
The CroppedImage method returns a B4XBitmap.
Don't confuse with CroppedView!
This is a B4XView where you can automatically copy the cropped image.
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
ou don't need a Panel nor Pane!
but on the code i see this:
ExtractCroppedImage:
Private Sub ExtractCroppedImage
    Private rectDest As B4XRect
    Private cLeft, cTop, cWidth, cHeight As Int
   
    If xpnlCropped.IsInitialized Then
        If rectImage.Width / rectImage.Height > imvCroppedWidth / imvCroppedHeight Then
            xpnlCropped.Width = imvCroppedWidth
            xpnlCropped.Height = imvCroppedWidth / rectImage.Width * rectImage.Height
        Else
            xpnlCropped.Height = imvCroppedHeight
            xpnlCropped.Width = imvCroppedHeight / rectImage.Height * rectImage.Width
        End If
        xcvsCropped.Resize(xpnlCropped.Width, xpnlCropped.Height)
       
        cLeft = Floor((rectImage.Left - ximvImage.Left) * PixelScale)
        cTop = Floor((rectImage.Top - ximvImage.Top) * PixelScale)
        cWidth = Ceil(rectImage.Width * PixelScale)
        cHeight = Ceil(rectImage.Height * PixelScale)
        xbmpCropped = xbmpImage.Crop(cLeft, cTop, cWidth, cHeight)
       
        rectDest.Initialize(0, 0, xpnlCropped.Width, xpnlCropped.Height)
       
        xcvsCropped.ClearRect(rectCroppedView)
        xcvsCropped.DrawBitmap(xbmpCropped, rectDest)
        xcvsCropped.Invalidate
    End If
End Sub
and this panel is set on the setCroppedView

if i call CroppedImage without to set a "CroppedView" then the "CoppedImage" is null.
or am I looking at this wrong?
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Klaus,

I tried your code yesterday (it works fine), but today there is the following message:
[IDE message - 1:52:41]
Ein Fehler trat auf.
Fehlende jar-Datei für die Bibliothek: xResizeAndCrop.xml
... and the message apears also when I open B4A with all my other codes!
What is the reason for that?
 

klaus

Expert
Licensed User
Longtime User
Remove the xResizeAndCrop.xml file from your AdditionalLibraries folder as written in the thread:
Don’t copy thexResizeAndCrop.xml file to the AdditionalLibraries folder, copy it in another folder for all b4xlib xml files.
The xResizeAndCrop.xml is only needed if you use the B4X Help Viewer.
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Oooh sorry Klaus, that was my mistake!
I've already read the sentence and copied the xml file into an extra folder, but at the same time I copied this file into the library folder ...
 

LucaMs

Expert
Licensed User
Longtime User
Very useful library, Klaus, thank you.

Am I wrong or there is no property to change the background color, currently gray?


EDIT. It is possible so:
B4X:
xResizeAndCrop1.mBase.Color = xui.Color_Blue
but that does not work if you set the color to xui.Color_Transparent or xui.Color_White
 
Last edited:

klaus

Expert
Licensed User
Longtime User
You can change the background color.
But I draw a dark color over mBase to darken the not cropped part of the image.
This is the gray color you see with xui.Color_White and xui.Color_Transparent.
All other colors are darkened the same way.
 

LucaMs

Expert
Licensed User
Longtime User
Although very well done, class and library, they have a rather important basic flaw.
The image you get is a part of the display, not of the image you uploaded from file, so its resolution is significantly lower than the original.

I hope I have explained, but I will give an example.
I uploaded an image that has a resolution of 1440 x 2560; using the library, I selected the entire displayed image and the CroppedImage method returned a B4XBitmap which has a resolution of 175 x 312.
 

klaus

Expert
Licensed User
Longtime User
How did you do it ?

I just added this routine in the demo program and it returns the correct values.

B4X:
Private Sub xResizeAndCrop1_CropFinished
    Log(xResizeAndCrop1.CroppedImage.Width & " / " & xResizeAndCrop1.CroppedImage.Height)
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
I created a simple B4XPages project.

Actually your demo works much better, albeit not perfectly.

Original Image:




(log: 1125 / 1989 - original image: 1440 / 2560)


[While that's not involved, I can't drag the bottom edge all the way down]
 

LucaMs

Expert
Licensed User
Longtime User
I forgot to write that, for the purpose of that B4XPages, in the layout Square is checked (of course?) but in the project I have attached it's not, because I wanted to select the whole image and see the log.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
I removed my previous post.
I have looked further with a bigger image and I saw that when I copy an image from the computer onto the device the size is changed.
I will investigate further tomorrow.
What do you get if you add the second line:
B4X:
Private Sub xResizeAndCrop1_CropFinished
    Log(xResizeAndCrop1.CroppedImage.Width & " / " & xResizeAndCrop1.CroppedImage.Height)
    Log(xResizeAndCrop1.Image.Width & " / " & xResizeAndCrop1.Image.Height)
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
In my project I found the "error":
B4X:
    Wait For ContentChooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        xResizeAndCrop1.Image = xui.LoadBitmapResize(Dir, FileName, B4XImageView1.mBase.Width, B4XImageView1.mBase.Height, True)

In yours it is:
B4X:
        xResizeAndCrop1.Image = xui.LoadBitmapResize(ImageDir, ImageFileName, 2000, 2000, True)
2000, 2000. That's probably why bigger images...
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…