public class BitmapHelper {

/**
* 压缩图片
* @param path 图片路径
* @param widthR 要显示的宽度
* @param heightR 要显示的高度
* @return 压缩后的图片
*/
public static synchronized Bitmap decodeBitmapFromPath(String path, int widthR, int heightR)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateRate(options, widthR, heightR);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}

/**
* 压缩图片
* @param data 图片数据
* @param widthR 要显示的宽度
* @param heightR 要显示的高度
* @return 压缩后的图片
*/
public static synchronized Bitmap decodeBitmapFromPath(byte[] data, int widthR, int heightR)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateRate(options, widthR, heightR);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0,data.length,options);
}

/**
* 获取缩略图
* @param path 图片路径
* @param targetWidth 缩略图宽度
* @param targetHeight 缩略图高度
* @return
*/
public static synchronized Bitmap getThumbnail(String path, int targetWidth , int targetHeight)
{
BitmapFactory.Options bOptions  = new BitmapFactory.Options();  
bOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path,bOptions);
int bmpWidth =bOptions.outWidth;
int bmpHeight= bOptions.outHeight;
int scale = 1;
int widthScale = bmpWidth/targetWidth;
int heightScale =  bmpHeight/targetHeight;
scale = widthScale < heightScale ? widthScale:heightScale;
bOptions.inSampleSize = scale;
bOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(path, bOptions);
   return ThumbnailUtils.extractThumbnail(bmp, targetWidth, targetHeight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}

/**
* 计算图片压缩比
* @param options
* @param widthR
* @param heightR
* @return
*/
private  static int calculateRate(BitmapFactory.Options options, int widthR,int heightR)
{
int width = options.outWidth;
int height = options.outHeight;
int size = 1;
if(width > widthR || height > heightR)
{
final int widthSize = Math.round((float)width / (float)widthR) ;
final int heightSize = Math.round((float)height /(float)heightR);
   size= widthSize < heightSize ? widthSize:heightSize;
}
return size;
}

/**
* 图片装换成Base64字符串
* @param path 路径
* @return Base64字符串
*/
public static  synchronized String imgToBase64String(String path)
{
File file = new File(path);
ByteArrayOutputStream imgData = new ByteArrayOutputStream();
if (!"null".equals(path) && file.exists()) {
try {
InputStream in = new FileInputStream(file);
byte[] buffer = new byte[1024];
int count = 0;
while (( count = in.read(buffer)) > 0) {
imgData.write(buffer, 0, count);
}
in.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

try {
imgData.close();
} catch (IOException e) {
e.printStackTrace();
}


String imgStr = new String(Base64.encode(imgData.toByteArray(), Base64.DEFAULT));
return imgStr;
}
}

更多相关文章

  1. android 图片的 放大 缩小 移动
  2. Android手势控制实现缩放、移动图片
  3. Android源码大放送(实战开发必备)
  4. Android(安卓)RecycerView 中根据图片大小自适应控件大小的实现
  5. 运行时改变Button图片的android:drawableTop
  6. Fresco使用的注意事项
  7. android之Tabhost深入讲解二
  8. 让一个小图片重复出现,形成一张大图片
  9. Android(安卓)studio(AS) svg图片使用

随机推荐

  1. MySQL批量插入和唯一索引问题的解决方法
  2. Mysql的数据库迁移到另一个机器上的方法
  3. mysql的in会不会让索引失效?
  4. 详解SQL注入--安全(二)
  5. window环境下使用VScode连接虚拟机MySQL
  6. Mysql经典高逼格/命令行操作(速成)(推荐)
  7. 详解mysql基本操作详细(二)
  8. 浅谈Mysql、SqlServer、Oracle三大数据库
  9. mysql unique key在查询中的使用与相关问
  10. MySQL数据库定时备份的实现方法