实际项目中,对图片的压缩处理相当常见,下面总结几种压缩的方式:
/** * 只压缩质量的方法 * @param file 源文件 * @param targetPath 目标路径 * @param quality 质量 取值为0-100 100表示按最大质量,此参数对PNG图片无效,即compress方法的第一个参数为 * Bitmap.CompressFormat.PNG时,quality的设置是无效的。 * **/
public static File scalFile(File file, String targetPath,int quality){
try {
//将文件转换为字节数组
byte[] bytes = getBytesFromFile(file);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap=BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
if(bitmap != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
baos.close();
File targetFile = new File(targetPath);
if(targetFile.exists()){
boolean flag = targetFile.delete();
Log.i("ImageUtils.scalFile()", "flag: " + flag);
}
FileOutputStream fos = new FileOutputStream(targetFile);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
return targetFile;
}
} catch (Exception e) {
e.printStackTrace();
Log.i("ImageUtils.scalFile()",e.getMessage());
}
return null;
}
/** * 压缩到指定分辨率的方法 以1280X960为例 * @param file 源文件 * @param targetPath 目标路径 * * * **/
public static File scalFile(File file, String targetPath){
long fileSize = file.length();
final long fileMaxSize = 200 * 1024;//超过200K的图片需要进行压缩
if(fileSize > fileMaxSize){
try {
byte[] bytes = getBytesFromFile(file);//将文件转换为字节数组
BitmapFactory.Options options = new BitmapFactory.Options();
//仅仅解码边缘区域
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//得到宽高
int width = options.outWidth;
int height = options.outHeight;
float scaleWidth=0f;
float scaleHeight=0f;
Matrix matrix=new Matrix();
if(width>height)
{
scaleWidth=(float)1280/width;
scaleHeight=(float)960/height;
}else{
scaleWidth=(float)960/width;
scaleHeight=(float)1280/height;
}
Bitmap bitmap=BitmapFactory.decodeFile(file.getPath());
matrix.postScale(scaleWidth, scaleHeight);//执行缩放
Bitmap resizeBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
if(resizeBitmap != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//限制压缩后图片最大为200K,否则继续压缩
while (baos.toByteArray().length > fileMaxSize) {
baos.reset();
quality -= 10;
resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
baos.close();
File targetFile = new File(targetPath);
if(targetFile.exists()){
boolean flag = targetFile.delete();
Log.i("ImageUtils.scalFile()", "flag: " + flag);
}
FileOutputStream fos = new FileOutputStream(targetFile);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
return targetFile;
}
} catch (Exception e) {
e.printStackTrace();
Log.i("ImageUtils.scalFile()",e.getMessage());
}
return null;
}else{
return file;
}
}
/*** *等比例压缩方式1,以采样率的方式,以1280X960为例 *@param file 源文件 *@param targetPath 目标路径 * **/
public static File scalFile(File file, String targetPath){
long fileSize = file.length();
final long fileMaxSize = 200 * 1024;
if(fileSize > fileMaxSize){
try {
byte[] bytes = getBytesFromFile(file);//将文件转换为字节数组
BitmapFactory.Options options = new BitmapFactory.Options();
//仅仅解码边缘区域
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//得到宽高
int width = options.outWidth;
int height = options.outHeight;
int sampleSize =1;
if(width>height)
{
sampleSize=Math.max(width/1280, height/960);
}else{
sampleSize=Math.max(width/960, height/1280);
}
if(sampleSize < 1)
sampleSize = 1;
//不再加载图片边缘
options.inJustDecodeBounds = false;
//制定缩放比例
options.inSampleSize = sampleSize;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
if(bitmap != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
while (baos.toByteArray().length > fileMaxSize) {
baos.reset();
quality -= 10;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
baos.close();
File targetFile = new File(targetPath);
if(targetFile.exists()){
boolean flag = targetFile.delete();
Log.i("ImageUtils.scalFile()", "flag: " + flag);
}
FileOutputStream fos = new FileOutputStream(targetFile);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
return targetFile;
}
} catch (Exception e) {
e.printStackTrace();
Log.i("ImageUtils.scalFile()",e.getMessage());
}
return null;
}else{
return file;
}
}
/** * 等比例压缩方式2 以1280X960为例 * @param file 源文件 * @param targetPath 目标路径 * * ***/
public static File scalFile(File file, String targetPath){
long fileSize = file.length();
final long fileMaxSize = 200 * 1024;
if(fileSize > fileMaxSize){
try {
byte[] bytes = getBytesFromFile(file);//将文件转换为字节数组
BitmapFactory.Options options = new BitmapFactory.Options();
//仅仅解码边缘区域
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
//得到宽高
int width = options.outWidth;
int height = options.outHeight;
float scaleWidth=0f;
float scaleHeight=0f;
float scale=0f;
Matrix matrix=new Matrix();
if(width>height)
{
scaleWidth=(float)1280/width;
scaleHeight=(float)960/height;
}else{
scaleWidth=(float)960/width;
scaleHeight=(float)1280/height;
}
//取两者之间较大的比例,这是为了尽可能得到分辨率大的图片
scale=Math.max(scaleWidth, scaleHeight);
Bitmap bitmap=BitmapFactory.decodeFile(file.getPath());
matrix.postScale(scale, scale);
Bitmap resizeBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
if(resizeBitmap != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
while (baos.toByteArray().length > fileMaxSize) {
baos.reset();
quality -= 10;
resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
}
baos.close();
File targetFile = new File(targetPath);
if(targetFile.exists()){
boolean flag = targetFile.delete();
Log.i("ImageUtils.scalFile()", "flag: " + flag);
}
FileOutputStream fos = new FileOutputStream(targetFile);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
return targetFile;
}
} catch (Exception e) {
e.printStackTrace();
Log.i("ImageUtils.scalFile()",e.getMessage());
}
return null;
}else{
return file;
}
}