Android Question Make a correct crop with panel

Douglas Farias

Expert
Licensed User
Longtime User
Hi all, sory for question this again :(
i need to make a crop with move image

user can move image on the screen, make zoom etc and have a fixed crop at the center.
Ok i tryed and i make this but the image croped is crazy

Original image = 1000x563
The Croped image is = 720x1184 when save the croped image o_O

the image is correct if you run this on a square
but the width and heigth is very wrong why this *-*

Im using on this Touchimageview and canvas with panel to make a crop
TouchImageView

attached is a example with log width and height


what can i make to move image on the screen and make a center crop when press a button?
i m using Touchimageview because i dont find any way to move the image *-*

thx to all
 

Attachments

  • Crop Sample.zip
    240 KB · Views: 274

Erel

B4X founder
Staff member
Licensed User
Longtime User
Draw on a mutable bitmap instead:
B4X:
Sub Button1_Click
   
   Dim cvs As Canvas
   Dim bmp As Bitmap
   bmp.InitializeMutable(Panel2.Width, Panel2.Height)
   cvs.Initialize2(bmp)
   
  Dim Bitmap1 As Bitmap
   Bitmap1.Initialize3(TouchImageView1.TransformedBitmap)

  Dim SrcRect As Rect
  SrcRect.Initialize( Panel2.Left , Panel2.Top , Panel2.Left + Panel2.Width ,Panel2.Top + Panel2.Height)

  Dim DestRect As Rect
  DestRect.Initialize(0%x,0%y,bmp.Width, bmp.Height)
   
  cvs.DrawBitmap(Bitmap1,SrcRect,DestRect)
   Dim out As OutputStream
   Dim filename As String
   filename = "testecrop.png"
  out = File.OpenOutput (File.DirDefaultExternal, filename ,  False)
   Log(cvs.Bitmap.Width & "x" & cvs.Bitmap.Height)
  cvs.Bitmap.WriteToStream(out,  100, "PNG")  
  out.Close
   
   If File.Exists(File.DirDefaultExternal, "testecrop.png") Then
   ToastMessageShow("Image Croped Saved on File.DirDefaultExternal & testecrop.png ", True)
   TouchImageView1.BackgroundImage = LoadBitmap(File.DirDefaultExternal,"testecrop.png") 'CARREGA A IMAGEM QUE FOI FEITO CROP NO LUGAR DO FUNDO PRETO
   End If
End Sub
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
hi erel and thx for u help.
I have found another solution, but i m not sure if this dont go give problems at diferent android versions

B4X:
Sub Button1_Click
    Dim cvs As Canvas
    cvs.Initialize(TouchImageView1)   
   
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize3(TouchImageView1.TransformedBitmap)

    Dim SrcRect As Rect
    SrcRect.Initialize( Panel2.Left , Panel2.Top , Panel2.Left + Panel2.Width ,Panel2.Top + Panel2.Height)

    Dim DestRect As Rect
    DestRect.Initialize(0%x,0%y,100%x,100%y)
   
    cvs.DrawBitmap(Bitmap1,SrcRect,DestRect)
    Dim out As OutputStream
    Dim filename As String
    filename = "testecrop.png"
    out = File.OpenOutput (File.DirRootExternal, filename ,  False)
    Dim caca As Bitmap = CreateScaledBitmap(cvs.Bitmap, 200, 200, True)
    Log(caca.Width & "x" & caca.Height)
    caca.WriteToStream(out,  100, "PNG") 
    out.Close
   
    If File.Exists(File.DirRootExternal, "testecrop.png") Then
    ToastMessageShow("Image Croped Saved on File.DirDefaultExternal & testecrop.png ", True)
    TouchImageView1.BackgroundImage = LoadBitmap(File.DirRootExternal,"testecrop.png") 'CARREGA A IMAGEM QUE FOI FEITO CROP NO LUGAR DO FUNDO PRETO
    End If
End Sub

Im using a
B4X:
  Dim caca As Bitmap = CreateScaledBitmap(cvs.Bitmap, 200, 200, True)

B4X:
Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
  Dim r As Reflector
  Dim b As Bitmap
  b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
      Array As Object(Original, Width, Height, Filter), _
      Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
  Return b
End Sub

have os restriction? for example android 2.3 + api 14+ for use CreateScaledBitmap ?
 
Upvote 0
Top