Android手机处理图片兼容性问题

来源:互联网 发布:淘宝做一个首页多少钱 编辑:程序博客网 时间:2024/05/08 00:35

Android图片处理整理总结:

1:Android拍照的时候我们会遇到一些状态,比如用三星手机拍照的话,有可能在拍完照片后,照片会自动旋转,下面这个方面是把旋转的照片还原回来:

Bitmap bitmap = BitmapFactory.decodeFile(Const.ACT_CREATE_PIC_PATH.concat(photoName));
int angle= imageUtils.getExifOrientation(Const.ACT_CREATE_PIC_PATH.concat(photoName));
if(angle!=0){ //如果照片出现了 旋转 那么 就更改旋转度数
Matrix matrix = new Matrix();
matrix.postRotate(angle);
bitmap = Bitmap.createBitmap(bitmap,0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}


2:如果Android应用需要处理和上传图片,那图片的大小是个大问题,因为主流的Android手机拍出的照片至少要3M以上,如果你的图片需要滤镜或者上传,那就会消耗很多时间。其实我们大多数时候是不需要质量那么高的图片的,图片分辨率高的时候只有在查看大图的时候有作用,在手机屏幕上查看图片不需要质量那么高的图片。所以,如果不需要裁剪的时候,可以自己等比例的减小图片的分辨率。示例代码:

public String scaleDown(String path,Context context)
{
Bitmap orignalB=BitmapFactory.decodeFile(path);
float ratio = Math.min((float) 974 / orignalB.getWidth(),(float) 974 / orignalB.getHeight());
int width = Math.round((float) ratio * orignalB.getWidth());
int height = Math.round((float) ratio * orignalB.getHeight());

Bitmap newB = Bitmap.createScaledBitmap(orignalB,width,height, true);
String imgName=path.substring(path.lastIndexOf("/")+1,path.length()-1);
String userId= UserHelper.getUserId(context);
String newpath=createDirectoryAndSaveFile(newB,userId+System.currentTimeMillis()+".jpg");
orignalB.recycle();
newB.recycle();
return newpath;
}

0 0
原创粉丝点击