刚刚学习了android下调用系统裁剪对图片裁剪,和图片压缩的一些功能。整理一下发表出来,有不足之处,望批评指正。
一、图片裁剪:调用系统裁剪。
outputX和outputY填写长宽的值,实际中并没有什么用。是哪里的参数冲突或者是哪个参数没有设置的缘故,待查。其他的都注释在代码中了。
private static Intent cut(Uri uri, float output, String path) {
L.d("ImageCut", "output=" + output);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
//设置图片长宽比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//设置裁剪图片的长宽
intent.putExtra("outputX", output);
intent.putExtra("outputY", output);
intent.putExtra("return-data", false);
//要裁剪的图片的URI
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
// 输出地址
intent.putExtra("output", Uri.fromFile(new File(path)));
// 返回格式
intent.putExtra("outputFormat", "JPEG");
return intent;
}
二、将图片裁剪成需要的长宽。
/**
* 缩放图片到指定宽高
* @param path
* @param newWidth
* @param newHeight
* @return Bitmap
*/
public static Bitmap zoomImg(String path, int newWidth,int newHeight) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / w;
float scaleHeight = ((float) newHeight) / h;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbm = null;
// 得到新的图片
try {
newbm = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
} catch (Exception e) {
}
return newbm;
}
三、图片质量压缩—不改变大小的情况下对图片进行压缩,通过降低bitmap.compress的quality参数来降低质量。这里进行了递归,有可能无法压缩到需要的大小,这时返回错误,要求对图片重新进行裁剪。
private static int RATIO = 15;//(不要低于15,否则可能出现越压缩越大,导致程序无限循环)超过30失真将会比较明显
/**
* 递归压缩图片
* @param path
* @param from
*/
public static boolean Compression(String path, int maxsize,int ratio) {
if(ratio>=85){
return false;
}
Bitmap bitmap = BitmapFactory.decodeFile(path);
FileOutputStream b = null;
try {
b = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100-RATIO-ratio, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 释放资源
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
// 判断是否超出限制,是则进行压缩
if (getFlieLengen(path) / 1024 > maxsize)
return Compression(path, maxsize,ratio+5);
else
return true;
}
四、一些获取图片长宽和文件大小的方法,获取长宽用的是BitmapFactory.Options,不会导致OOM。
/**
* 计算图片的宽度
*
* @param path
* @return
*/
public static int getWidth(String path) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, newOpts);
newOpts.inJustDecodeBounds = false;
return newOpts.outWidth;
}
/**
* 计算图片的高度
*
* @param path
* @return
*/
public static int getHeight(String path) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, newOpts);
newOpts.inJustDecodeBounds = false;
return newOpts.outHeight;
}
//获取文件大小
public static int getFlieLengen(String path) {
File dF = new File(path);
int fileLen = 0;
FileInputStream fis;
try {
fis = new FileInputStream(dF);
fileLen = fis.available();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileLen;
}
最后,代码都很简单,也做了部分注释,有问题欢迎提问,本人也是菜鸟一只,共同学习。关于文章开头提出的图片剪切设置长宽无效的原因,如果知道请告诉我,万分感谢。