dev
dev

Reputation: 11309

Unable to crop and scale a bitmap on Android

I am trying to take a screenshot of my app, crop out the top portion and scale it down. But neither crop, nor scale seems to work. Here is my code :

            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap screenShot = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            //crop out 60 px from top and scale down 0.3 times size
            Matrix matrix = new Matrix();
            matrix.postScale(0.3f, 0.3f);
            Bitmap bitmap = Bitmap.createBitmap(screenShot,
                    0, 60,
                    screenShot.getWidth(),
                    screenShot.getHeight()-60,
                    matrix,
                    true);

Upvotes: 0

Views: 619

Answers (2)

Ankita Chopra
Ankita Chopra

Reputation: 329

private void performCrop() {

try {  Intent cropIntent = new Intent("com.android.camera.action.CROP");
          cropIntent.setDataAndType(imageUri, "image/*");
            cropIntent.putExtra("crop", "true");
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX", AppController.convertDpToPx(60));
            cropIntent.putExtra("outputY", AppController.convertDpToPx(60));
            cropIntent.putExtra("return-data", true);
            startActivityForResult(cropIntent, CROP_PIC);} catch (ActivityNotFoundException anfe) {
            //display an error message
           String errorMessage = "Whoops - your device doesn't support the crop action!";
         Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();

        }
    }

// this method will help you to convert dp into px

public static int convertDpToPx(int dp) {
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, AppController.getInstance().getApplicationContext().getResources().getDisplayMetrics());
            return (int) px;
        }

// converting bitmap into uri

   public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
      return Uri.parse(path);
    }

Upvotes: 0

Daniel Martin Shum
Daniel Martin Shum

Reputation: 547

I don't know why your code does not work, but cropping a bitmap is easy

bitmap = Bitmap.createBitmap(
    bitmap,                    //the source
    0,                         //left position to start copy with
    60,                        //top position to start copy with
    bitmap.getWidth(),         //number of pixels in each row to be copied
    bitmap.getHeight() - 60    //number of rows to be copied
);

and scale a bitmap is easy

bitmap = Bitmap.createScaledBitmap(
    bitmap,   //the source
    120,      //destination width
    120,      //destination height
    false     //filter
);

Upvotes: 2

Related Questions