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;
}
}
/**
* 压缩图片
* @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;
}
}