How to cut part of a bitmap?

3

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

For example:

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

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

The result should look like this:

I've mounted this algorithm, but it takes a long time to execute:

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;
}

Is there a faster way to do this using Canvas or some other class that works with Images?

    
asked by anonymous 04.10.2017 / 21:06

1 answer

4

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);
    
04.10.2017 / 21:28