B4A Library [Library] RSCrop - Crop images with gallery

XverhelstX

Well-Known Member
Licensed User
Hey everyone,

For an app i'm developing, I needed to let the user crop a piece of a bitmap to use in my app.
I stumbled on this thread: http://www.basic4ppc.com/forum/basic4android-updates-questions/19550-resize-crop.html
but it lacked some features like pinching, rectangular cropping, resizing, zoom and panning and more.

After some googling, i figured out that you could send an intent to the default media gallery to let it crop your images.

It opens your gallery where you select your image and then the cropping activity opens. After the cropping is done, an event is raised with the Path and a success boolean.
3 Functions are needed to get you going:

B4X:
Sub mnuCrop_Click
Dim CropImage As RSCrop
CropImage.Initialize("CropImage", "image.jpg")
CropImage.CropImage2(True, False, False, False)
End Sub

Sub CropImage_Cropped (Success As Boolean, Pathname As String)
   If Success = True Then
      ToastMessageShow("Successfully cropped!", True)
      ToastMessageShow("Path: " & Pathname, True)
   Else
      ToastMessageShow(Pathname, False)
   End If
End Sub
  • RSCrop
    Permissions:
    • android.permission.WRITE_SECURE_SETTINGS
    • android.permission.READ_PHONE_STATE
    Methods:
    • Initialize (EventName As String, FileName As String)
      Initializes the RSCrop Library.
      EventName - Events subs prefix.
      Events: "Cropped (Success As Boolean, Path as String)"
    • cropImage (aspectX As Int, aspectY As Int, outputX As Int, outputY As Int, scale As Boolean, return_data As Boolean, faceDetection As Boolean, circleCrop As Boolean)
      Crops an image with the standard media gallery.
      aspectX: - Set this to define the X aspect ratio of the shape
      aspectY: - Set this to define the Y aspect ratio of the shape
      outputX: - Set this to define the max size of the output bitmap
      outputY: - Set this to define the max size of the output bitmap
      scale: - should it scale
      return_data: - Return the bitmap with Action=inline-data by using the data
      faceDetection:
      circleCrop: - if this string is not null, it will provide some circular crop
    • cropImage2 (scale As Boolean, return_data As Boolean, faceDetection As Boolean, circleCrop As Boolean)
      Crops an image with the standard media gallery.
      Difference is that you can use a rectangular crop with this.
      scale: - should it scale
      return_data: - Return the bitmap with Action=inline-data by using the data
      faceDetection:
      circleCrop: - if this string is not null, it will provide some circular crop
    • cropImage3 (scale As Boolean, return_data As Boolean, faceDetection As Boolean, circleCrop As Boolean, btmp As Bitmap)
      Crops an image with the standard media gallery.
      Difference is that you can preselect a bitmap with this.
      scale: - should it scale
      return_data: - Return the bitmap with Action=inline-data by using the data
      faceDetection:
      circleCrop: - if this string is not null, it will provide some circular crop
After the cropping is done, the image is saved to your file.dirExternal and the filename is the one you specified in your initialize method.

Regards,
Tomas

Edit: If people would like to test:
I would like to know on which devices this work: API level (android version) and phone
Preferably I need Galaxy Note support ;)
 

Attachments

Last edited:

vb1992

Well-Known Member
Licensed User
I couldn't get the face detection working?
or maybe I just don't know what it does when set to true...?

I would assume it highlights a face? or zooms into the face detected

I am on 2.3 gingerbread
 

vb1992

Well-Known Member
Licensed User
That was weird, i just tested again, and face detection found 3 faces
so that works on images that have faces that are detectable, the
previous test, was with faces that were too small i believe.

Now I found the check mark / OK
in the top right hand corner instead of the buttons.

I click OK and it keeps spinning on "saving picture"
 

NJDude

Expert
Licensed User
Ok, I tried your lib using only the sample code you posted:

1- HTC EVO - Froyo = Works
2- Nexus 7 - Jelly Bean = Works
3- HTC EVO LTE - ICS = Works
4- Chinese Tab - Froyo = Works
5- Galaxy Tab 10.1 - HC = Doesn't work (same issue as described by vb1992 above)

Maybe Samsung doesn't like your lib :D
 
Last edited:

synapse

Member
Licensed User
Does the library allow one to specify a particular image to be passed in cropimage, rather than just opening a gallery please? I see you can specify the output image...
 

DonManfred

Expert
Licensed User
Edit: If people would like to test:
I would like to know on which devices this work: API level (android version) and phone
Preferably I need Galaxy Note support ;)
I have made a little example and have tested it on my Galaxy Note 3 - Phone
Facturer: Samsung
Model: SM-N9005
Android: 4.3

and i got two Toastmessages after croppting an image...
Successfully cropped
and
Path /storage/emulated/0/image.jpg

But HOW can i copy this file to a place where i can find it if my phone is connected to pc?
Sorry, i´m not familar which "such problems" :)
 

systems1

Member
Licensed User
Hi XverhelstX

Great work, I have tested this library in many devices including Samsung, HTC, Sony and others. It's working well in almost all devices.

I want to fix the size of cropping image. I mean i want to fix height and width for the image. I have got max height and width limit for image to be cropped. So it should not allow me to crop above that limit.
How can i do this...?

Thanks in advance
 

systems1

Member
Licensed User
Hi DonManfred,

Thanks for the reply
But how can i get the ImageWidth and ImageHeight of image. I can assign some value to LimitedWidth and LimitedHeight.
But how can i get the value of ImageWidth and ImageHeight. Because it is getting the value from the library right...

what i want to do is this,
when i open some image from gallery and drag the four corners to fix the height and width to crop the image, here i want to fix max width and height. It should not allow me to drag and select the image more than what i specified in LimitedWidth and LimitedHeight

Thanks
 
Last edited:

boten

Active Member
Licensed User
I like this lib a lot. One question:
When calling CropImage I get a menu to select which app to use for cropping. Gallery is only one option. Other options include, for example, Astro file manager.
I want to use ONLY gallery, without displaying the selection menu.
How is this possible? - so that when CropImage is called it will automatically open the image in Gallery instead of giving the user a selection.
 

coslad

Well-Known Member
Licensed User
Hi ,

i'm trying to use this library , but when the crop function start , it asks to complete the action with the gallery twice ,it's annoying thing , i tried on a galaxy note 3 and a galaxy s , any idea ?
 
Top