Create a cropped bitmap from an original bitmap

In my android application I have a Bitmap and I want to create another bitmap which is cropped out of this bitmap. In other words I want to get a particular portion from the original Bitmap.

I am using Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) but the image which I get in return appears to be zoomed in.

What could be the reason, and how can I correct this? Please help


Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) is definitely the correct method. I suspect you're just using the parameters incorrectly.

E.g. To get the centre 50x50 pixels of a 100x100 bitmap named img, you'd use:

Bitmap.createBitmap(img, 25, 25, 50, 50);

My advice:

r = this.getContext().getResources();
Drawable copyFrom= r.getDrawable(R.drawable.OriginalPNG);
Bitmap b1 = Bitmap.createBitmap(IMAGES_WIDTH, IMAGES_HEIGHT,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b1);

copyFrom.setBounds(0, 0, IMAGES_WIDTH, IMAGES_HEIGHT);

Bitmap copyTo;  

copyTo = Bitmap.createBitmap(copyFrom, x, y, W, H);

where IMAGES_WIDTH and IMAGES_HEIGHT are the dimensions of the original PNG and W,H are the dimensions of the area you want to copy. x and y specify the point on the original PNG from where to begin to copy. Putting x,y both to zero means starting from the upper-left corner.

Here you go

  Rect re=new Rect(350, 150, 350, 150);

    public void takePicture(final String fileName) {
    Log.i(TAG, "Tacking picture");
    PictureCallback callback = new PictureCallback() {

        private String mPictureFileName = fileName;

        public void onPictureTaken(byte[] data, Camera camera) {
            Log.i(TAG, "Saving a bitmap to file");
            Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
           Bitmap finalBitmap = Bitmap.createBitmap(picture, 850, 500, 960, 720);

            try {

                FileOutputStream out = new FileOutputStream(mPictureFileName);
                finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            } catch (Exception e) {

    mCamera.takePicture(null, null, callback);


Need Your Help

Deploy Java Web App Rest Spring Heroku Port Message error R10

java hibernate heroku

I cant deploy my java web app on Heroku, my app use: Java RESTful Web Services with Spring Boot, Jersey and Hibernate...

passing text from an Array to UIWebView

iphone objective-c uiwebview

i want to pass some html i have saved as string and stored in an array into a UIWebView.