B4A Library CropIWA

This is a wrap forthis Github project.

CropIwa
Version:
0.5
  • AspectRatio
    Methods:
    • Initialize (w As Int, h As Int)
    • IsInitialized As Boolean
  • CropIwaImageViewConfig
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • apply
    • isImageTranslationEnabled As Boolean
    • setImageInitialPosition (initialPosition As InitialPosition) As CropIwaImageViewConfigwrapper
    • setImageScaleEnabled (scaleEnabled As Boolean) As CropIwaImageViewConfigwrapper
    • setImageTranslationEnabled (imageTranslationEnabled As Boolean) As CropIwaImageViewConfigwrapper
    • setMaxScale (maxScale As Float) As CropIwaImageViewConfigwrapper
    • setMinScale (minScale As Float) As CropIwaImageViewConfigwrapper
    • setScale (scale As Float) As CropIwaImageViewConfigwrapper
    Properties:
    • ImageInitialPosition As InitialPosition [read only]
    • ImageScaleEnabled As Boolean [read only]
    • MaxScale As Float [read only]
    • MinScale As Float [read only]
    • Scale As Float [read only]
  • CropIwaOverlayConfig
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • apply As CropIwaOverlayConfigwrapper
    • setAspectRatio (ratio As AspectRatio) As CropIwaOverlayConfigwrapper
    • setBorderColor (borderColor As Int) As CropIwaOverlayConfigwrapper
    • setBorderStrokeWidth (borderStrokeWidth As Int) As CropIwaOverlayConfigwrapper
    • setCornerColor (cornerColor As Int) As CropIwaOverlayConfigwrapper
    • setCornerStrokeWidth (cornerStrokeWidth As Int) As CropIwaOverlayConfigwrapper
    • setCropShape (cropShape As String) As CropIwaOverlayConfigwrapper
    • setCropShape2 (cropShape As CropIwaShape) As CropIwaOverlayConfigwrapper
    • setDynamicCrop (enabled As Boolean) As CropIwaOverlayConfigwrapper
    • setGridColor (gridColor As Int) As CropIwaOverlayConfigwrapper
    • setGridStrokeWidth (gridStrokeWidth As Int) As CropIwaOverlayConfigwrapper
    • setMinHeight (minHeight As Int) As CropIwaOverlayConfigwrapper
    • setMinWidth (minWidth As Int) As CropIwaOverlayConfigwrapper
    • setOverlayColor (overlayColor As Int) As CropIwaOverlayConfigwrapper
    • setShouldDrawGrid (shouldDrawGrid As Boolean) As CropIwaOverlayConfigwrapper
    Properties:
    • AspectRatio As AspectRatio [read only]
    • BorderColor As Int [read only]
    • BorderStrokeWidth As Int [read only]
    • CornerColor As Int [read only]
    • CornerStrokeWidth As Int [read only]
    • CropShape As CropIwaShape [read only]
    • DynamicCrop As Boolean [read only]
    • GridColor As Int [read only]
    • GridStrokeWidth As Int [read only]
    • MinHeight As Int [read only]
    • MinWidth As Int [read only]
    • OverlayColor As Int [read only]
  • CropIwaResultReceiver
    Events:
    • onCropFailed (error As String)
    • onCropSuccess (Uri As String)
    Methods:
    • Initialize (EventName As String)
    • IsInitialized As Boolean
    • register
    • unregister
  • CropIwaSaveConfig
    Methods:
    • Initialize (uriString As String) As CropIwaSaveConfigwrapper
    • IsInitialized As Boolean
    • build As CropIwaSaveConfig
    • saveToFile (uriString As String) As CropIwaSaveConfigwrapper
    • setCompressFormat (compressFormat As CompressFormat) As CropIwaSaveConfigwrapper
    • setQuality (quality As Int) As CropIwaSaveConfigwrapper
    • setSize (width As Int, height As Int) As CropIwaSaveConfigwrapper
  • CropIwaView
    Events:
    • onCroppedRegionSaved (Uri As String)
    • onError (error As String)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • configureImage As CropIwaImageViewConfig
    • configureOverlay As CropIwaOverlayConfig
    • crop (saveConfig As CropIwaSaveConfig)
    Properties:
    • Background As Drawable
    • CENTER_CROP As InitialPosition [read only]
    • CENTER_INSIDE As InitialPosition [read only]
    • Color As Int [write only]
    • Enabled As Boolean
    • Height As Int
    • IMG_SRC As AspectRatio [read only]
    • Image As Bitmap [write only]
    • ImageUri As String [write only]
    • Left As Int
    • Padding()() As Int
    • Parent As Object [read only]
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int

3J8gYWC.gif


Please note
- that the Animated GIF is from the original github proect. It does not look the same in the B4A Example.
- that you need to adapt the path in the example to get the right Imaged loaded which you want to crop.
- To have the changes in the CropOverlay get apllied your last command in the Builder should be "Apply"

This library is Donationware. You can download the library, you can test the library. But if you want to USE the library in your App you need to Donate for it.
Please click here to donate (You can donate any amount you want to donate for the library (or my work) :)
 

Attachments

  • CropIwaEx.zip
    26.4 KB · Views: 617
  • CropIwaV0.5.zip
    70.2 KB · Views: 609
Last edited:

DonManfred

Expert
Licensed User
Longtime User

kostefar

Active Member
Licensed User
Longtime User
Hi Manfred,

How do I maintain one fixed aspect ratio?

I tried this:

B4X:
Dim overlay As CropIwaOverlayConfig = Crop.configureOverlay
    Dim asp As AspectRatio
    asp.Initialize (1,1)
    overlay.setAspectRatio(asp)

It starts with the right ratio, but the selection rectangle still can be stretched by the user.
Also, how can I zoom out? If I load an image taken by my digital camera, so with a high resolution, it will be zoomed in on a very small area.
 
Last edited:

kostefar

Active Member
Licensed User
Longtime User
Ok, for showing any size of image so that it fits the screen, I made the below. Perhaps it could be done more elegantly. Still looking for a way to not let the user change the aspect ratio of the selection rectangle:

B4X:
    Dim b As Bitmap
    b.Initialize (dirname,filename)
    If b.height > b.Width Then
    Dim minmax As Float = Activity.Height / b.Height
    Else
    Dim minmax As Float = Activity.width / b.width
    End If
    Log ("act h " & Activity.Height & " bmap h " & b.Height & " act W " & Activity.WIDTH & " bmap W " & b.WIDTH)
    cfgimg.setMinScale (minmax)
    cfgimg.setMaxScale (minmax)

Obviously, dirname and filename refers to the image that´s chosen for cropping.

.. came a bit closer: .setDynamicCrop(False) makes it impossible to change the shape of the rectangle. Unfortunately, it also makes it impossible to redimension it.

Ok, solution: setImageScaleEnabled(True) I had changed to false. Setting it to true made it possible to do what I wanted in sort of a reverse way where it´s the image I´ll be changing the size of by pinching, and not the rectangle. I guess that´ll do :)

EDIT: One more finding:

I had to to add

#AdditionalRes: ..\res

to

#Region Activity Attributes

(just like in the sample project)
and copy the res folder from the sample project over to my own project folder for this to work. Maybe obvious for some, but for sure not for everybody.
 
Last edited:

kostefar

Active Member
Licensed User
Longtime User
This doesn't looks like that works on Android N. Tried there and didn't work.

Did you try on a device that´s not Android N? Have you changed the path in the example to point at a file which exists on your device?

I tried on genymotion with 7.0 where it works fine. Never 100% the same with an emu, but still..
 

scsjc

Well-Known Member
Licensed User
Longtime User
Hello DonManfred, nice library !!!
is possible add rotate options ????

thanks
 

kostefar

Active Member
Licensed User
Longtime User
I did not manage to set an oval cropshape with .setCropShape, although it should be possible according to the original project. Any idea?
 

kostefar

Active Member
Licensed User
Longtime User
One thing to keep in mind: Setcropshape - and perhaps other methods - won´t work if not in release mode. Sat for an hour trying to figure out why it works in your demo project but not in another one.
 

FrankBerra

Active Member
Licensed User
Longtime User
I don't understand how to make this library working.

Is use the following code taken almost from the demo example:

B4X:
croprec.Initialize("CropRec")
Crop.Initialize("Crop")
Dim cfgimg As CropIwaImageViewConfig = Crop.configureImage
cfgimg.setImageInitialPosition(Crop.CENTER_INSIDE).setImageScaleEnabled(True).setScale(2)
 
Dim overlay As CropIwaOverlayConfig = Crop.configureOverlay
            overlay.setAspectRatio(Crop.IMG_SRC).setCornerColor(Colors.Red).setBorderColor(Colors.Blue).setGridColor(Colors.Yellow).setShouldDrawGrid(True).setCropShape("Rect").apply
 
Dim cfg As CropIwaSaveConfig
cfg.Initialize("file://" & File.Combine(File.DirDefaultExternal, Filename)).setQuality(100).setSize(640,480)

Crop.crop(cfg.build)

But always i get this error:
B4X:
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:325)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
    at com.steelkiwi.cropiwa.image.CropIwaBitmapManager.isWebUri(CropIwaBitmapManager.java:285)
    at com.steelkiwi.cropiwa.image.CropIwaBitmapManager.toLocalUri(CropIwaBitmapManager.java:150)
    at com.steelkiwi.cropiwa.image.CropIwaBitmapManager.loadToMemory(CropIwaBitmapManager.java:117)
    at com.steelkiwi.cropiwa.image.CropImageTask.doInBackground(CropImageTask.java:41)
    at com.steelkiwi.cropiwa.image.CropImageTask.doInBackground(CropImageTask.java:20)
    at android.os.AsyncTask$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    ... 4 more

The Demo example gives me the same error.
Can someone explain me how to solve this issue?

Thanks in advance
 
Last edited:

FrankBerra

Active Member
Licensed User
Longtime User
Solved: After a lot of tests i learned that the sub btnLoadImage_Click instead of loading something it SAVES the cropped image.......
 
Top