(裁剪只定宽高的图片)一个简单的图片截取代码、等比例、不失真
2013-05-05 14:52
375 查看
直接上干货
/**
* @param rawPath
* 原图地址
* @param dstPath
* 裁剪后地址
* @param width
* 想要的图片宽(宽不能大于高)
* @param height
* 想要的图片高(宽不能大于高)
* */
public static Bitmap cropBitmap(String rawPath, String dstPath, int width,
int height) {
Bitmap bitmap;
Bitmap cropImage = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(rawPath, options);
int rawWidth = options.outWidth;
int rawHeight = options.outHeight;
boolean isWidth = false;
if (rawWidth > width && rawHeight > height) {
// 宽永远小于高
if (rawWidth > rawHeight) {
isWidth = true;
}
MyLog.d("图片宽高", "===========" + rawWidth + "*" + rawHeight);
int inSamplesize = (rawWidth / width) > (rawHeight / height) ? (rawWidth / width)
: (rawHeight / height);
MyLog.d("缩放比例", "===========" + inSamplesize);
options.inJustDecodeBounds = false;
options.inSampleSize = inSamplesize;
bitmap = BitmapFactory.decodeFile(rawPath, options);
Config config = bitmap.getConfig();
config.compareTo(Config.RGB_565);
rawWidth = options.outWidth;
rawHeight = options.outHeight;
MyLog.d("缩放后宽高", "===========" + rawWidth + "*" + rawHeight);
Rect rect = new Rect();
if (isWidth) {
rect.left = ((rawWidth - height) < 0 ? 0 : (rawWidth - height)) / 2;
rect.top = ((rawHeight - width) < 0 ? 0 : (rawHeight - width)) / 2;
rect.right = (rect.left + height) > rawWidth ? rawWidth
: (rect.left + height);
rect.bottom = (rect.top + width) > rawHeight ? rawHeight
: (rect.top + width);
} else {
rect.left = ((rawWidth - width) < 0 ? 0 : (rawWidth - width)) / 2;
rect.top = ((rawHeight - height) < 0 ? 0 : (rawHeight - height)) / 2;
rect.right = (rect.left + width) > rawWidth ? rawWidth
: (rect.left + width);
rect.bottom = (rect.top + height) > rawHeight ? rawHeight
: (rect.top + height);
}
MyLog.d("图片宽高", "===========左" + rect.left + "上" + rect.top + "右"
+ rect.right + "下" + rect.bottom);
cropImage = Bitmap.createBitmap(rect.width(), rect.height(),
config);
Canvas cvs = new Canvas(cropImage);
Rect dr = new Rect(0, 0, rect.width(), rect.height());
cvs.drawBitmap(bitmap, rect, dr, null);
FileUtils.createFile(new File(dstPath),
ImageUtil.bitmap2Bytes(cropImage));
bitmap.recycle();
} else {
File file = new File(rawPath);
byte[] buffer = new byte[(int) file.length()];
try {
new FileInputStream(new File(rawPath)).read(buffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileUtils.createFile(new File(dstPath), buffer);
}
return cropImage;
}
/**
* @param rawPath
* 原图地址
* @param dstPath
* 裁剪后地址
* @param width
* 想要的图片宽(宽不能大于高)
* @param height
* 想要的图片高(宽不能大于高)
* */
public static Bitmap cropBitmap(String rawPath, String dstPath, int width,
int height) {
Bitmap bitmap;
Bitmap cropImage = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(rawPath, options);
int rawWidth = options.outWidth;
int rawHeight = options.outHeight;
boolean isWidth = false;
if (rawWidth > width && rawHeight > height) {
// 宽永远小于高
if (rawWidth > rawHeight) {
isWidth = true;
}
MyLog.d("图片宽高", "===========" + rawWidth + "*" + rawHeight);
int inSamplesize = (rawWidth / width) > (rawHeight / height) ? (rawWidth / width)
: (rawHeight / height);
MyLog.d("缩放比例", "===========" + inSamplesize);
options.inJustDecodeBounds = false;
options.inSampleSize = inSamplesize;
bitmap = BitmapFactory.decodeFile(rawPath, options);
Config config = bitmap.getConfig();
config.compareTo(Config.RGB_565);
rawWidth = options.outWidth;
rawHeight = options.outHeight;
MyLog.d("缩放后宽高", "===========" + rawWidth + "*" + rawHeight);
Rect rect = new Rect();
if (isWidth) {
rect.left = ((rawWidth - height) < 0 ? 0 : (rawWidth - height)) / 2;
rect.top = ((rawHeight - width) < 0 ? 0 : (rawHeight - width)) / 2;
rect.right = (rect.left + height) > rawWidth ? rawWidth
: (rect.left + height);
rect.bottom = (rect.top + width) > rawHeight ? rawHeight
: (rect.top + width);
} else {
rect.left = ((rawWidth - width) < 0 ? 0 : (rawWidth - width)) / 2;
rect.top = ((rawHeight - height) < 0 ? 0 : (rawHeight - height)) / 2;
rect.right = (rect.left + width) > rawWidth ? rawWidth
: (rect.left + width);
rect.bottom = (rect.top + height) > rawHeight ? rawHeight
: (rect.top + height);
}
MyLog.d("图片宽高", "===========左" + rect.left + "上" + rect.top + "右"
+ rect.right + "下" + rect.bottom);
cropImage = Bitmap.createBitmap(rect.width(), rect.height(),
config);
Canvas cvs = new Canvas(cropImage);
Rect dr = new Rect(0, 0, rect.width(), rect.height());
cvs.drawBitmap(bitmap, rect, dr, null);
FileUtils.createFile(new File(dstPath),
ImageUtil.bitmap2Bytes(cropImage));
bitmap.recycle();
} else {
File file = new File(rawPath);
byte[] buffer = new byte[(int) file.length()];
try {
new FileInputStream(new File(rawPath)).read(buffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileUtils.createFile(new File(dstPath), buffer);
}
return cropImage;
}
相关文章推荐
- php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法
- 一个简单的图片截取代码 java
- 简单的一个CSS菜单,不用图片也很好看_网页代码站(www.webdm.cn)
- 典型又简单的一个首页焦点图片的代码
- 一段简单的图片裁剪代码
- Android 以任意比例裁剪图片代码分享
- jquery图片不完全按比例自动缩小的简单代码
- 网上一堆代码,没有一个真正实现curl传图片成功过的,这儿,我来个成功的,超级简单,扩展性强【广告一下】
- 一个简单的载入图片等待状态的Loading代码
- 一个简单的图片裁剪Activity:android-crop
- 图片根据需要的规格裁剪成相应比例以免失真
- flash 中的简单图片裁剪(拼图代码)
- CSS实用代码:清除浮动、单行省略号、图片宽高比例、display兼容代码、三角形状
- 一个简单的javascript图片放大效果代码
- 一个裁剪图片的小工具类,通过一句代码调用
- jquery图片不完全按比例自动缩小的简单代码
- 一个简单的javascript图片放大效果代码
- 转一个简单的vue.js的图片懒加载的插件代码!
- Android [应用代码] 简单调用系统的图片裁剪功能
- 【代码】微信小程序 简单一个页面,网上图片展示