B4A Library CropImageView V1.0

CropImageView
Version:
1

This is a wrapper for this GitHub Project.

CropImageView001.png


  • CropImageView
    Fields:
    • ba As BA
    Methods:
    • AddToParent (Parent As ViewGroup, left As Int, top As Int, width As Int, height As Int)
    • 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)
    • rotateImage (degrees As Int)
      Rotates image by the specified number of degrees clockwise. Cycles from 0 to 360
      degrees.
    • setAspectRatio (aspectRatioX As Int, aspectRatioY As Int)
      Sets the both the X and Y values of the aspectRatio.
    Properties:
    • ActualCropRect As RectF [read only]
      Gets the crop window's position relative to the source Bitmap (not the image
      displayed in the CropImageView).
    • Background As Drawable
    • Color As Int [write only]
    • CroppedImage As Bitmap [read only]
      Gets the cropped image based on the current crop window.
    • Enabled As Boolean
    • FixedAspectRatio As Boolean [write only]
      Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while
      false allows it to be changed.
    • Guidelines As Int [write only]
      Sets the guidelines for the CropOverlayView to be either on, off, or to show when
      resizing the application.
    • Height As Int
    • ImageBitmap As Bitmap [write only]
      Sets a Bitmap as the content of the CropImageView.
    • Left As Int
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int

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

  • CroppedImageViewEx.zip
    74.3 KB · Views: 1,032
  • libCropImageView1.0.0.zip
    27.1 KB · Views: 1,036
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
this works perfect, i go make u a donation when i launch my app thx
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi DonManfred,

Thank you for the library. I have an image that is 1364 X 2048 size. I want to fix the crop window only to my device height and size. How do you do that?

Regards,
SK
 

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Hello I'm trying to use CroppedImage method combined with StringUtils.EncodeBase64, but it's resulting in a not valid Base64 string.

Do you have any idea what is happening?

Thanks
 

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
No. I dont remember having a base64 routine inside this lib.... So; without seeing your code it is hard to give advices.
You're right Manfred. I don't use any code from this library to encode the image into a Base64 format.

I don't have the code right now, but I do something like this:

B4X:
Dim su As StringUtils
Dim str As String = su.EncodeBase64(img.CroppedImage)

img is declared as CropImageView.

The issue here is, when I persist my str and try to decode them into a ImageView I got an exception. Even trying to convert using an online Base64 parser, I got the same message, that the String is a invalid Base64 string.

I'm trying to figure out if the problem is related with the way CroppedImage handle to create the new Bitmap. Make sense?
 

DonManfred

Expert
Licensed User
Longtime User
It will not work the way you do
B4X:
img.CroppedImage
will return a BITMAP, not a byte array

You first need to convert the bitmap into a byte array

B4X:
Dim out As OutputStream
out.InitializeToBytesArray(0)
dim bmp as Bitmap = img.CroppedImage
bmp.WriteToStream(out, 100, "PNG")
out.Close
Dim data() As Byte = out.ToBytesArray

and then

B4X:
Dim su As StringUtils
Dim str As String = su.EncodeBase64(data)

something like that... Have not tested it
 

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
It will not work the way you do
B4X:
img.CroppedImage
will return a BITMAP, not a byte array

You first need to convert the bitmap into a byte array

B4X:
Dim out As OutputStream
out.InitializeToBytesArray(0)
dim bmp as Bitmap = img.CroppedImage
bmp.WriteToStream(out, 100, "PNG")
out.Close
Dim data() As Byte = out.ToBytesArray

and then

B4X:
Dim su As StringUtils
Dim str As String = su.EncodeBase64(data)

something like that... Have not tested it

Hey Manfred,

no luck at all.

Here is my code:

B4X:
Sub btnChoosePhoto_Click
    Dim Chooser As ContentChooser
    Chooser.Initialize("chooser")
    Chooser.Show("image/*", "Selecione uma nova foto para o aluno")
End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        imageToCrop.ImageBitmap = LoadBitmap(Dir,FileName)
    Else
        ToastMessageShow("Nenhuma foto selecionada", True)
    End If
End Sub

B4X:
    Dim u As Utils
    u.Initialize
    Dim bmp as Bitmap = imageToCrop.CroppedImage
    Dim photo as String= u.ImageToBase64(bmp)

B4X:
Public Sub ImageToBase64(img As Bitmap) As String
   Dim su As StringUtils
   Dim out As OutputStream
   out.InitializeToBytesArray(0)
   img.WriteToStream(out, 75, "JPEG")
   out.Close
   Return su.EncodeBase64(out.ToBytesArray)
End Sub

When I paste the result of photo in a Base64 decoder like http://base64online.org/decode/#, I got the message It seems like image file is broken.

I also observed that photo variable has some blank spaces into that. This looks to be an issue when I try to decode Base64 format.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
When I paste the result of photo in a Base64 decoder like http://base64online.org/decode/#, I got the message It seems like image file is broken.
imagecrop0004.png

I´m using exact the code i wrote above....

B4X:
Sub Button1_Click
    Dim bmp As Bitmap = civ.CroppedImage
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    bmp.WriteToStream(out, 100, "PNG")
    out.Close
    Dim data() As Byte = out.ToBytesArray
    ImageView1.Bitmap = bmp
    Dim su As StringUtils
    Dim str As String = su.EncodeBase64(data)
    File.WriteString(File.DirRootExternal,"base64.uue",str)
    'Log(str)
End Sub
 

Gorazdspajzer

Member
Licensed User
Longtime User
Great Lib!
One question. Why I cant access all parts of the image (there is some margin and then inside is crop panel). Because maybe I want to crop part of the image that is in some corner.

Thanks
 

Dogbonesix

Active Member
Licensed User
Longtime User
Exactly what I needed Except: I can not reload another image without exiting the app and reloading - don't know why. I would like to browse several pictures and crop them in the same folder. Very difficult when I have start the app for every image to crop.
Also, some images won't display but will still crop - memory?
 
Top