android 压缩图片、生成缩略图 和转换Base64格式的字符串
16lz
2021-01-24
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;
}
}
更多相关文章
- android 图片的 放大 缩小 移动
- Android手势控制实现缩放、移动图片
- Android源码大放送(实战开发必备)
- Android(安卓)RecycerView 中根据图片大小自适应控件大小的实现
- 运行时改变Button图片的android:drawableTop
- Fresco使用的注意事项
- android之Tabhost深入讲解二
- 让一个小图片重复出现,形成一张大图片
- Android(安卓)studio(AS) svg图片使用