Crop a Bitmap image

前端 未结 4 2004
鱼传尺愫
鱼传尺愫 2020-11-30 01:00

How can i crop a bitmap image? this is my question i have tried some concepts using intents but still fail..

I am having a bitmap image which i want

相关标签:
4条回答
  • 2020-11-30 01:28

    I had similar problem with cropping and after trying numerous approaches I figured out this one which made sense to me. This method only crops the image to square shape, I am still working on the circular shape (Feel free to modify the code to get shape you need).

    So, first you have yout bitmap that you want to crop:

    Bitmap image; //you need to initialize it in your code first of course
    

    The image information is stored in an int [ ] array what is nothing more than an array of integers containing the color value of each pixel, starting at the top left corner of the image with index 0 and ending at the bottom right corner with index N. You can obtain this array with Bitmap.getPixels() method which takes various arguments.

    We need the square shape, therefore we need to shorten the longer of the sides. Also, to keep the image centered the cropping needs to be done at the both sides of the image. Hopefully the image will help you understand what I mean. Visual representation of the cropping. The red dots in the image represent the initial and final pixels that we need and the variable with the dash is numerically equal to the same variable without the dash.

    Now finally the code:

    int imageHeight = image.getHeight(); //get original image height
    int imageWidth = image.getWidth();  //get original image width
    int offset = 0;
    
    int shorterSide = imageWidth < imageHeight ? imageWidth : imageHeight;
    int longerSide = imageWidth < imageHeight ? imageHeight : imageWidth;
    boolean portrait = imageWidth < imageHeight ? true : false;  //find out the image orientation
    //number array positions to allocate for one row of the pixels (+ some blanks - explained in the Bitmap.getPixels() documentation)
    int stride = shorterSide + 1; 
    int lengthToCrop = (longerSide - shorterSide) / 2; //number of pixel to remove from each side 
    //size of the array to hold the pixels (amount of pixels) + (amount of strides after every line)
    int pixelArraySize = (shorterSide * shorterSide) + (shorterImageDimension * 1);
    int pixels = new int[pixelArraySize];
    
    //now fill the pixels with the selected range 
    image.getPixels(pixels, 0, stride, portrait ? 0 : lengthToCrop, portrait ? lengthToCrop : 0, shorterSide, shorterSide);
    
    //save memory
    image.recycle();
    
    //create new bitmap to contain the cropped pixels
    Bitmap croppedBitmap = Bitmap.createBitmap(shorterSide, shorterSide, Bitmap.Config.ARGB_4444);
    croppedBitmap.setPixels(pixels, offset, 0, 0, shorterSide, shorterSide);
    
    //I'd recommend to perform these kind of operations on worker thread
    listener.imageCropped(croppedBitmap);
    
    //Or if you like to live dangerously
    return croppedBitmap;
    
    0 讨论(0)
  • 2020-11-30 01:30

    For cropping bitmap with given width from left and right side simply use this code

    int totalCropWidth = "your total crop width"; int cropingSize = totalCropWidth / 2; Bitmap croppedBitmap = Bitmap.createBitmap(yourSourceBitmap, cropingSize ,0,yourSourceBitmapwidth-totalCropWidth , yourheight);

    0 讨论(0)
  • 2020-11-30 01:37

    I used this method to crop the image and it works perfect:

    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.xyz);
    
    resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight);
    

    createBitmap() takes bitmap,start X,start Y,width & height as parameter

    0 讨论(0)
  • 2020-11-30 01:49

    Using the answer above does not work if you want to slice/crop areas particular out of bounds! Using this code you will always get your desired size - even if the source is smaller.

    //  Here I want to slice a piece "out of bounds" starting at -50, -25
    //  Given an endposition of 150, 75 you will get a result of 200x100px
    Rect rect = new Rect(-50, -25, 150, 75);  
    //  Be sure that there is at least 1px to slice.
    assert(rect.left < rect.right && rect.top < rect.bottom);
    //  Create our resulting image (150--50),(75--25) = 200x100px
    Bitmap resultBmp = Bitmap.createBitmap(rect.right-rect.left, rect.bottom-rect.top, Bitmap.Config.ARGB_8888);
    //  draw source bitmap into resulting image at given position:
    new Canvas(resultBmp).drawBitmap(bmp, -rect.left, -rect.top, null);
    

    ...and you're done!

    0 讨论(0)
提交回复
热议问题