androidbitmapcompress(图片压缩)代码
android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1.照小图片;2.压缩大图片。照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些;压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
finalBitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath,options);
//CalculateinSampleSize
options.inSampleSize=calculateInSampleSize(options,480,800);
//DecodebitmapwithinSampleSizeset
options.inJustDecodeBounds=false;
Bitmapbm=BitmapFactory.decodeFile(filePath,options);
finalBitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath,options);
//CalculateinSampleSize
options.inSampleSize=calculateInSampleSize(options,480,800);
//DecodebitmapwithinSampleSizeset
options.inJustDecodeBounds=false;
Bitmapbm=BitmapFactory.decodeFile(filePath,options);
2.2处理图片旋转
[java]
intdegree=readPictureDegree(filePath);
bm=rotateBitmap(bm,degree);
intdegree=readPictureDegree(filePath);
bm=rotateBitmap(bm,degree);[java]viewplaincopyprint?privatestaticintreadPictureDegree(Stringpath){
intdegree =0;
try{
ExifInterfaceexifInterface=newExifInterface(path);
intorientation=exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
switch(orientation){
caseExifInterface.ORIENTATION_ROTATE_90:
degree=90;
break;
caseExifInterface.ORIENTATION_ROTATE_180:
degree=180;
break;
caseExifInterface.ORIENTATION_ROTATE_270:
degree=270;
break;
}
}catch(IOExceptione){
e.printStackTrace();
}
returndegree;
}
privatestaticintreadPictureDegree(Stringpath){
intdegree =0;
try{
ExifInterfaceexifInterface=newExifInterface(path);
intorientation=exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
switch(orientation){
caseExifInterface.ORIENTATION_ROTATE_90:
degree=90;
break;
caseExifInterface.ORIENTATION_ROTATE_180:
degree=180;
break;
caseExifInterface.ORIENTATION_ROTATE_270:
degree=270;
break;
}
}catch(IOExceptione){
e.printStackTrace();
}
returndegree;
}
[java]
viewplaincopyprint?privatestaticBitmaprotateBitmap(Bitmapbitmap,introtate){
if(bitmap==null)
returnnull;
intw=bitmap.getWidth();
inth=bitmap.getHeight();
//Settingpostrotateto90
Matrixmtx=newMatrix();
mtx.postRotate(rotate);
returnBitmap.createBitmap(bitmap,0,0,w,h,mtx,true);
}
privatestaticBitmaprotateBitmap(Bitmapbitmap,introtate){
if(bitmap==null)
returnnull;
intw=bitmap.getWidth();
inth=bitmap.getHeight();
//Settingpostrotateto90
Matrixmtx=newMatrix();
mtx.postRotate(rotate);
returnBitmap.createBitmap(bitmap,0,0,w,h,mtx,true);
}
bm.compress(Bitmap.CompressFormat.JPEG,30,baos);//30是压缩率,表示压缩70%;如果不压缩是100,表示压缩率为0 publicstaticBitmapgetSmallBitmap(StringfilePath){ //CalculateinSampleSize //DecodebitmapwithinSampleSizeset } privatestaticintcalculateInSampleSize(BitmapFactory.Optionsoptions, if(height>reqHeight||width>reqWidth){ //Calculateratiosofheightandwidthtorequestedheightand //ChoosethesmallestratioasinSampleSizevalue,thiswill returninSampleSize;
2.3压缩图片
[java]
bm.compress(Bitmap.CompressFormat.JPEG,30,baos);//30是压缩率,表示压缩70%;如果不压缩是100,表示压缩率为0
完整的方法代码:
[java]
publicstaticBitmapgetSmallBitmap(StringfilePath){
finalBitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath,options);
//CalculateinSampleSize
options.inSampleSize=calculateInSampleSize(options,480,800);
//DecodebitmapwithinSampleSizeset
options.inJustDecodeBounds=false;
Bitmapbm=BitmapFactory.decodeFile(filePath,options);
if(bm==null){
return null;
}
intdegree=readPictureDegree(filePath);
bm=rotateBitmap(bm,degree);
ByteArrayOutputStreambaos=null;
try{
baos=newByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,30,baos);
}finally{
try{
if(baos!=null)
baos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnbm;
}
finalBitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath,options);
options.inSampleSize=calculateInSampleSize(options,480,800);
options.inJustDecodeBounds=false;
Bitmapbm=BitmapFactory.decodeFile(filePath,options);
if(bm==null){
return null;
}
intdegree=readPictureDegree(filePath);
bm=rotateBitmap(bm,degree);
ByteArrayOutputStreambaos=null;
try{
baos=newByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,30,baos);
}finally{
try{
if(baos!=null)
baos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnbm;
[java]
viewplaincopyprint?privatestaticintcalculateInSampleSize(BitmapFactory.Optionsoptions,
intreqWidth,intreqHeight){
//Rawheightandwidthofimage
finalintheight=options.outHeight;
finalintwidth=options.outWidth;
intinSampleSize=1;
if(height>reqHeight||width>reqWidth){
//Calculateratiosofheightandwidthtorequestedheightand
//width
finalintheightRatio=Math.round((float)height
/(float)reqHeight);
finalintwidthRatio=Math.round((float)width/(float)reqWidth);
//ChoosethesmallestratioasinSampleSizevalue,thiswill
//guarantee
//afinalimagewithbothdimensionslargerthanorequaltothe
//requestedheightandwidth.
inSampleSize=heightRatio<widthRatio?widthRatio:heightRatio;
}
returninSampleSize;
}
intreqWidth,intreqHeight){
//Rawheightandwidthofimage
finalintheight=options.outHeight;
finalintwidth=options.outWidth;
intinSampleSize=1;
//width
finalintheightRatio=Math.round((float)height
/(float)reqHeight);
finalintwidthRatio=Math.round((float)width/(float)reqWidth);
//guarantee
//afinalimagewithbothdimensionslargerthanorequaltothe
//requestedheightandwidth.
inSampleSize=heightRatio<widthRatio?widthRatio:heightRatio;
}
}相关文章