android将给定图片维持宽高比缩放后,截取正中间的正方形部分
2012-09-07 15:35
555 查看
/**
* 将给定图片维持宽高比缩放后,截取正中间的正方形部分。
* @param bitmap 原图
* @param edgeLength 希望得到的正方形部分的边长
* @return 缩放截取正中部分后的位图。
*/
public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength)
{
if(null == bitmap || edgeLength <= 0)
{
return null;
}
Bitmap result = bitmap;
int widthOrg = bitmap.getWidth();
int heightOrg = bitmap.getHeight();
if(widthOrg > edgeLength && heightOrg > edgeLength)
{
//压缩到一个最小长度是edgeLength的bitmap
int longerEdge = (int)(edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
Bitmap scaledBitmap;
try{
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
}
catch(Exception e){
return null;
}
//从图中截取正中间的正方形部分。
int xTopLeft = (scaledWidth - edgeLength) / 2;
int yTopLeft = (scaledHeight - edgeLength) / 2;
try{
result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
scaledBitmap.recycle();
}
catch(Exception e){
return null;
}
}
return result;
}
* 将给定图片维持宽高比缩放后,截取正中间的正方形部分。
* @param bitmap 原图
* @param edgeLength 希望得到的正方形部分的边长
* @return 缩放截取正中部分后的位图。
*/
public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength)
{
if(null == bitmap || edgeLength <= 0)
{
return null;
}
Bitmap result = bitmap;
int widthOrg = bitmap.getWidth();
int heightOrg = bitmap.getHeight();
if(widthOrg > edgeLength && heightOrg > edgeLength)
{
//压缩到一个最小长度是edgeLength的bitmap
int longerEdge = (int)(edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
Bitmap scaledBitmap;
try{
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
}
catch(Exception e){
return null;
}
//从图中截取正中间的正方形部分。
int xTopLeft = (scaledWidth - edgeLength) / 2;
int yTopLeft = (scaledHeight - edgeLength) / 2;
try{
result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
scaledBitmap.recycle();
}
catch(Exception e){
return null;
}
}
return result;
}
相关文章推荐
- android将给定图片维持宽高比缩放后,截取正中间的正方形部分
- android图片裁剪截取中间正方形部分
- iOS 截取图片中间部分、图片缩放到宽度
- Bitmap截取中间正方形并取出圆形图片
- 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
- android 图片处理(截取,缩放)
- android开发步步为营之81:android图片处理技术之一(截取与缩放)
- 把一张图片压缩并截取中间部分
- 图片超过边框大小时截取图片中间部分
- Android基础第九天易忘部分(加载大图片和缩放图片)
- 图片超过边框大小时截取图片中间部分
- ios 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
- php GD 截取图片 自动截取为规定大小且图片的中间部分
- 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示(转)
- 图片超过边框大小时截取图片中间部分
- 按宽/高缩放图片到指定大小并进行裁剪得到中间部分图片 返回Bitmap
- 根据size截取图片中间矩形区域的图片 这里的size是正方形
- 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
- bitmap缩放剪裁图片(等宽裁中间部分高)
- android 图片处理(截取,缩放)