Java Question Copy part of bitmap

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hello,

I need to copy a part of a bitmap and save it in another bitmap (without the use of the canvas.)
I'm using the following code to get the pixels of the current bitmap and save them in a new bitmap.

B4X:
public Bitmap extractBitmap(Bitmap source, int x, int y, int width, int height) {
       //Create a same size Bitmap
        Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        
      int[] pixels = new int[width * height];
      
      source.getPixels(pixels, 0, width, x, y, width, height);
       newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
       
       return newBitmap;
   }

But i keep getting a java.lang.illegalargumentexception: y+height must be <= bitmap.height OR x+width must be <= bitmap.width.

B4X:
btmpTilt = image.extractBitmap(btmpTilt, x, y, w, h)

x: 44
y: 93
w: 214
h: 209

What I want to do is extract a part of the bitmap (from left: 44, top: 93, with; 214, h 209) and place them on a new bitmap with left: 0 top: 0, width 214, height; 209.)

Kind regards,
Tomas

HELP ME! ;)
 
Last edited:

XverhelstX

Well-Known Member
Licensed User
Longtime User
Yea, i already know that.
I made a preview of what i try to achieve:

bitmap.png

Here's my B4A code:

B4X:
Sub mnuTilt(x As Int, y As Int, w As Int, h As Int)
   
   Dim btmpBlur As Bitmap
   btmpBlur = canvas1.Bitmap
   btmpBlur = effects.Blur(btmpBlur, 16,0)
   btmpTilt = image.extractBitmap(btmpTilt, x, y, w, h)
   
   btmpFlower = blend.blend(btmpBlur, btmpTilt, x, y)
   Panel1.SetBackgroundImage(btmpFlower)
End Sub

Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
   
   If Action = Activity.ACTION_DOWN Then
        sx = X
        sy = Y
    Else
        If Action = Activity.ACTION_MOVE Then
            'Draw the new rectangle
            Rect1.Left = Min(sx, X)
            Rect1.Right = Max(sx, X)
            Rect1.Top = Min(sy, Y)
            Rect1.Bottom = Max(sy, Y)
        End If
        
    End If
   
   If Action = Activity.ACTION_UP Then
      canvas1.DrawRect(Rect1, Colors.White, False, 1)
      
      Panel1.Invalidate
      mnuTilt(Rect1.Left, Rect1.Top, Rect1.Right, Rect1.Bottom)
   End If


End Sub

It draws a rectangle on the panel, get the x, y w and h and calls mnuTilt.
I get the error on extractBitmap.

Tomas
 

klaus

Expert
Licensed User
Longtime User
In the mnuTilt routine you are using x, y w width and h height.

The Rect1 definition is x1, y1, x2, y2 !

I think that the call to the mnuTilt routine should be :
B4X:
mnuTilt(Rect1.Left, Rect1.Top, Rect1.Right - Rect1.Left, Rect1.Bottom - Rect1.Top)
Best regards.
 

BarrySumpter

Active Member
Licensed User
Longtime User
What do you mean by What do you mean? ;p
ExtractBitmap
Yes there is and Answered in another thread.

All my best
 
Top