How to cut part of a bitmap?

Asked

Viewed 321 times

4

I want to create a Bitmap that its content is a part of another Bitmap


For example:

I have a Bitmap (400x900)
width = 400
height = 900

I want to cut it in the dimensions (400x400) in order to leave the height equal to the width ignoring the rest of the image.


The result should stay this way:

inserir a descrição da imagem aqui


I built this algorithm, but it takes too long running:

private Bitmap reparteImage(Bitmap bitmap) {
    int w = bitmap.getWidth();
    //PASSA WIDTH PARA WIDTH E HEIGHT
    Bitmap bmp = Bitmap.createBitmap(w, w, Bitmap.Config.ARGB_8888); // this creates a MUTABLE bitmap
    for(int x=0;x<bmp.getWidth();x++){
        for(int y=0;y<bmp.getHeight();y++){
            bmp.setPixel(x, y, bitmap.getPixel(x, y));
        }
    }
    return bmp;
}

There is a faster way to perform this procedure using Canvas or some other class that works with Images?

1 answer

5


Use the static method createBitmap() of the Bitmap class:

Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)

In your case use it like this:

Bitmap croppedBitmap = Bitmap.createBitmap(bitmapSource, 0, 0, 400, 400);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.