同事写的图片压缩方法
2013-12-20 10:21
330 查看
if (MessageUtils.ensureImageSizeExceed(this, imgpath)) {
imgpath = MessageUtils.compressPicture(this, imgpath);
}
public static boolean ensureImageSizeExceed (Context context, String filePath) {
if (TextUtils.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
long fileSize = file.length();
// when size exceed 1M
if (fileSize > IMAGE_LIMIT) {
return true;
}
return false;
}
public static final int IMAGE_LIMIT = 300 * 1024;
/**
* @param filePath the file path need compress
* @return the compressed file path
* */
public static String compressImage(Context context, String filePath) {
long time = System.currentTimeMillis();
Uri fileUri = Uri.fromFile(new File(filePath));
InputStream input = null;
int imageWidth = 480;
int imageHeight = 800;
try {
input = context.getContentResolver().openInputStream(fileUri);
Log.i(TAG, "liuqiang123--before file size="+new File(filePath).length());
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, opt);
imageWidth = opt.outWidth;
imageHeight = opt.outHeight;
if (input != null) {
input.close();
}
int widthLimit = MmsConfig.getMaxImageWidth();
int heightLimit = MmsConfig.getMaxImageHeight();
if (imageHeight > imageWidth) {
int temp = widthLimit;
widthLimit = heightLimit;
heightLimit = temp;
}
int limitSize = MmsConfig.getMaxMessageSize() * 1024;
if (limitSize <= 0) {
limitSize = IMAGE_LIMIT;
}
long time1 = System.currentTimeMillis();
byte[] data = UriImage.getResizedImageData(imageWidth, imageHeight, widthLimit,
heightLimit, limitSize, fileUri, context);
Log.i(TAG, "liuqiang123--getResizedImageData pay time="+(System.currentTimeMillis() - time1));
String path = pathForSendFile(context, generateFileName(PICTURE), PICTURE);
FileOutputStream fout = new FileOutputStream(path);
fout.write(data);
fout.close();
Log.i(TAG, "liuqiang123--after file size="+new File(path).length());
Log.i(TAG, "liuqiang123--compressImage() pat time=" + (System.currentTimeMillis() - time));
return path;
} catch (FileNotFoundException e) {
Log.e(TAG, "liuqiang123--compressImage()--FileNotFoundException", e);
} catch (IOException e) {
Log.e(TAG, "liuqiang123--compressImage()--IOException", e);
}
return null;
}
imgpath = MessageUtils.compressPicture(this, imgpath);
}
public static boolean ensureImageSizeExceed (Context context, String filePath) {
if (TextUtils.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
long fileSize = file.length();
// when size exceed 1M
if (fileSize > IMAGE_LIMIT) {
return true;
}
return false;
}
public static final int IMAGE_LIMIT = 300 * 1024;
/**
* @param filePath the file path need compress
* @return the compressed file path
* */
public static String compressImage(Context context, String filePath) {
long time = System.currentTimeMillis();
Uri fileUri = Uri.fromFile(new File(filePath));
InputStream input = null;
int imageWidth = 480;
int imageHeight = 800;
try {
input = context.getContentResolver().openInputStream(fileUri);
Log.i(TAG, "liuqiang123--before file size="+new File(filePath).length());
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, opt);
imageWidth = opt.outWidth;
imageHeight = opt.outHeight;
if (input != null) {
input.close();
}
int widthLimit = MmsConfig.getMaxImageWidth();
int heightLimit = MmsConfig.getMaxImageHeight();
if (imageHeight > imageWidth) {
int temp = widthLimit;
widthLimit = heightLimit;
heightLimit = temp;
}
int limitSize = MmsConfig.getMaxMessageSize() * 1024;
if (limitSize <= 0) {
limitSize = IMAGE_LIMIT;
}
long time1 = System.currentTimeMillis();
byte[] data = UriImage.getResizedImageData(imageWidth, imageHeight, widthLimit,
heightLimit, limitSize, fileUri, context);
Log.i(TAG, "liuqiang123--getResizedImageData pay time="+(System.currentTimeMillis() - time1));
String path = pathForSendFile(context, generateFileName(PICTURE), PICTURE);
FileOutputStream fout = new FileOutputStream(path);
fout.write(data);
fout.close();
Log.i(TAG, "liuqiang123--after file size="+new File(path).length());
Log.i(TAG, "liuqiang123--compressImage() pat time=" + (System.currentTimeMillis() - time));
return path;
} catch (FileNotFoundException e) {
Log.e(TAG, "liuqiang123--compressImage()--FileNotFoundException", e);
} catch (IOException e) {
Log.e(TAG, "liuqiang123--compressImage()--IOException", e);
}
return null;
}
相关文章推荐
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- iOS 用这些方法把图片不压缩,部分显示在imageView上面。。。。
- iOS 图片压缩UIImage方法扩展
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- Android中常用的三种图片压缩方法
- php上传图片并压缩的实现方法
- 一种压缩图片的方法---Machine learning 之 K-Means
- 图片压缩方法收集
- 另一种压缩图片的方法---Machine learning 之 PCA(Principle Component Analysis)
- java中用Thumbnailator对图片各种处理的方法(可做到对原图片压缩仅改变大小)
- android 图片压缩的三种方法,其实还有第四种就是2.2以后提供的压缩类
- 图片压缩方法
- iOS 图片压缩UIImage方法扩展
- Android压缩图片的几种方法
- 压缩图片的方法(压缩图片的质量)
- Android压缩图片的几种方法
- android调用系统相机的方法,并且解决三星手机遇到的问题,图片旋转、压缩
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 图片压缩方法
- Android 图片压缩也即生成缩略图方法