PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] How to crop bitmap image from both top and bottom?

 July 27, 2022     android-bitmap, crop, java     No comments   

Issue

I have my view converted to bitmap image, and I want to cut part from bottom and top. My problem is that I can do one of them, but when I want to do another, it annuls previous work. Here is my code:

bitImage=getBitmapFromView(_root);
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmapOptions.inTargetDensity = 1;
                    bitImage.setDensity(Bitmap.DENSITY_NONE);
                    Bitmap crop=Bitmap.createBitmap(bitImage,0,0,bitImage.getWidth(),((int)(bitImage.getHeight()*0.8)));
                    crop=Bitmap.createBitmap(anything here will annul previous line);
                    crop.compress(Bitmap.CompressFormat.PNG,100,out); 

And here is the picture, so you can better see what I want to cut (arrows show those parts). Thank you.

picture


Solution

This will cut you Bitmap from top and bottom and takes the center path and the condition if (srcBmp.getWidth() >= srcBmp.getHeight()) tells if width greater than height then it will cut horizontally .

if (srcBmp.getWidth() >= srcBmp.getHeight()){

  dstBmp = Bitmap.createBitmap(
     srcBmp, 
     srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
     0,
     srcBmp.getHeight(), 
     srcBmp.getHeight()
     );

}else{

  dstBmp = Bitmap.createBitmap(
     srcBmp,
     0, 
     srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
     srcBmp.getWidth(),
     srcBmp.getWidth() 
     );
}


Answered By - Asad Ali
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing