大致思路,根据后台上传图片限制,自己对图片进行处理
一般后台会限制图片宽高,对于超出限制的图片要进行宽高比压缩(减少图片的宽高) 核心代码
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;//只读大小,不读数据
BitmapFactory.decodeFile(path, op);
int height = op.outHeight;
int width = op.outWidth;
LogUtil.e("scal_from_size" + outputFile.length() + "宽度为" + height + "高度为" + height, PhotoUtil.class);
Bitmap bitmap = null;
while (height > maxHeight || width > maxWidth) {// 如果宽度大的话根据宽度固定大小缩放
double scaleH = (float) height / maxHeight;
double scaleW = (float) width / maxWidth;
options.inSampleSize = (int) Math.ceil(Math.max(scaleH, scaleW)); // 1表示不缩放
bitmap = BitmapFactory.decodeFile(outputFile.getPath(), options);
LogUtil.e("宽度压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight +
"scal_ok_size" + outputFile.length(), PhotoUtil.class);
}
当图片大小大于服务器限制大小时就要进行质量压缩
try { while (fileSize >= fileMaxSize) {//如果图片大小 > 服务器要求大小 options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(path, options); fos = new FileOutputStream(outputFile); quality -= 10; bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos); fos.close(); fileSize = outputFile.length(); LogUtil.e("采样率压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight + "scal_ok_size" + outputFile.length(), PhotoUtil.class); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }全部代码
static final int maxHeight = 1023;
static final int maxWidth = 2047;
static long fileMaxSize = 1024 * 1024;
/**
* 压缩图片
*
* @param path 图片地址
* @return 压缩后的图片地址
*/
public static File scal(String path, int scalSize) {
File outputFile = new File(path);
long fileSize = outputFile.length();
long fileMaxSize = scalSize;
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, op);
int height = op.outHeight;
int width = op.outWidth;
LogUtil.e("scal_from_size" + outputFile.length() + "宽度为" + height + "高度为" + height, PhotoUtil.class);
Bitmap bitmap = null;
if (fileSize >= fileMaxSize || height > maxHeight || width > maxWidth) {
BitmapFactory.Options options = new BitmapFactory.Options();
while (height > maxHeight || width > maxWidth) {// 如果宽度大的话根据宽度固定大小缩放
double scaleH = (float) height / maxHeight;
double scaleW = (float) width / maxWidth;
options.inSampleSize = (int) Math.ceil(Math.max(scaleH, scaleW)); // 1表示不缩放
BitmapFactory.decodeFile(outputFile.getPath(), options);
LogUtil.e("宽度压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight + "scal_ok_size" + outputFile.length(), PhotoUtil.class);
}
FileOutputStream fos = null;
int quality = 100;
try {
while (fileSize >= fileMaxSize) {//如果图片大小 > 服务器要求大小
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path, options);
fos = new FileOutputStream(outputFile);
quality -= 10;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
fos.close();
fileSize = outputFile.length();
LogUtil.e("采样率压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight
+ "scal_ok_size" + outputFile.length(), PhotoUtil.class);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return outputFile;
}