cainiao00007
2017-04-14 11:19:26
android开发关于图片处理的工具类方法,再也不用为OOM发愁了
public class BitmapUtils {
// compile 'com.yolanda.nohttp:nohttp:1.0.4'
/**
* 从本地读取图片
*
* @param path
* @return
*/
public static Bitmap getBitmapForPath(String path) {
try {
FileInputStream in = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
return bitmap;
} catch (Exception e) {
}
return null;
}
/**
* 获取资源文件中的图片
*
* @param context
* @param resourcesId
* @return
*/
public static Drawable getDrawableFormResources(Context context, int resourcesId) {
Resources resources = context.getResources();
return new BitmapDrawable(resources, BitmapFactory.decodeResource(resources, resourcesId));
}
/**
* 从资源文件中获取bitmap对象
*
* @param context
* @param resourcesId
* @return
*/
public static Bitmap getBitmapFromResources(Context context, int resourcesId) {
return BitmapFactory.decodeResource(context.getResources(), resourcesId);
}
/**
* bitmap转byte数组
*
* @param bitmap
* @return
*/
public static byte[] getBitmapbyte(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] datas = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return datas;
}
/**
* bitmap转byte数组
*
* @param bitmap
* @return
*/
public static String getBitmapBase64byte(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] datas = baos.toByteArray();
String encodeToString = Base64.encodeToString(datas, Base64.DEFAULT);
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return encodeToString;
}
/**
* byte转bitmap数组
*
* @param b
* @return
*/
public static Bitmap getBitmaoFrombyte(byte[] b) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
/**
* 压缩0
*
* @param srcPath
* @return
*/
public static Bitmap getimageIcon(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 312f;//这里设置高度为800f
float ww = 650f;//这里设置宽度为480f
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
}
/**
* 压缩1
*
* @param srcPath
* @return
*/
public static Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;//这里设置高度为800f
float ww = 480f;//这里设置宽度为480f
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
}
//把bitmap转换成String
// public static String bitmapToString(String filePath) {
//
// Bitmap bm = getSmallBitmap(filePath);
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// byte[] b = baos.toByteArray();
// return Base64.encodeToString(b, Base64.DEFAULT);
// }
/**
* 压缩2
*
* @param image
* @return
*/
public static Bitmap comp(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if (baos.toByteArray().length / 1024 > 1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, 30, baos);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;//这里设置高度为800f
float ww = 480f;//这里设置宽度为480f
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;//降低图片从ARGB888到RGB565
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
isBm = new ByteArrayInputStream(baos.toByteArray());
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
}
/**
* 质量压缩
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
options -= 20;//每次都减少10
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
/**
* 获取图片大小
*
* @param bitmap
* @return
*/
public static long getBitmapsize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
return bitmap.getByteCount();
}
return bitmap.getRowBytes() * bitmap.getHeight();
}
/**
* 对图片进行模糊处理
*
* @param bitmap
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
RenderScript rs = RenderScript.create(context.getApplicationContext());
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
blurScript.setRadius(25f);
blurScript.setInput(allIn);
blurScript.forEach(allOut);
allOut.copyTo(outBitmap);
bitmap.recycle();
rs.destroy();
return outBitmap;
}
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
/**
* 水平方向模糊度
*/
private static float hRadius = 10;
/**
* 竖直方向模糊度
*/
private static float vRadius = 10;
/**
* 模糊迭代度
*/
private static int iterations = 7;
private static float a = 1.3f;
/**
* 模糊图片
* @param bmp
* @return
*/
public static Drawable BoxBlurFilter(Bitmap bmp) {
hRadius = hRadius * a;
vRadius = vRadius * a;
iterations = (int) (iterations * a);
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] inPixels = new int[width * height];
int[] outPixels = new int[width * height];
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.getPixels(inPixels, 0, width, 0, 0, width, height);
for (int i = 0; i < iterations; i++) {
blur(inPixels,
outPixels, width, height, hRadius);
blur(outPixels,
inPixels, height, width, vRadius);
}
blurFractional(inPixels,
outPixels, width, height, hRadius);
blurFractional(outPixels,
inPixels, height, width, vRadius);
bitmap.setPixels(inPixels,
0,
width, 0,
0,
width, height);
Drawable drawable = new BitmapDrawable(bitmap);
return drawable;
}
public static void blur(int[] in, int[] out, int width, int height, float radius) {
int widthMinus1 = width - 1;
int r = (int) radius;
int tableSize = 2 * r + 1;
int divide[] = new int[256 * tableSize];
for (int i = 0; i < 256 * tableSize; i++)
divide[i] = i / tableSize;
int inIndex = 0;
for (int y = 0; y < height; y++) {
int outIndex = y;
int ta = 0, tr = 0, tg = 0, tb = 0;
for (int i = -r; i <= r; i++) {
int rgb = in[inIndex + clamp(i, 0, width - 1)];
ta += (rgb >> 24) & 0xff;
tr += (rgb >> 16) & 0xff;
tg += (rgb >> 8) & 0xff;
tb += rgb & 0xff;
}
for (int x = 0; x < width; x++) {
out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8)
| divide[tb];
int i1 = x + r + 1;
if (i1 > widthMinus1)
i1 = widthMinus1;
int i2 = x - r;
if (i2 < 0)
i2 = 0;
int rgb1 = in[inIndex + i1];
int rgb2 = in[inIndex + i2];
ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);
tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;
tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;
tb += (rgb1 & 0xff) - (rgb2 & 0xff);
outIndex += height;
}
inIndex += width;
}
}
public static void blurFractional(int[] in, int[] out, int width, int height, float radius) {
radius -= (int) radius;
float f = 1.0f / (1 + 2 * radius);
int inIndex = 0;
for (int y = 0; y < height; y++) {
int outIndex = y;
out[outIndex] = in[0];
outIndex += height;
for (int x = 1; x < width - 1; x++) {
int i = inIndex + x;
int rgb1 = in[i - 1];
int rgb2 = in[i];
int rgb3 = in[i + 1];
int a1 = (rgb1 >> 24)
& 0xff;
int r1
= (rgb1 >> 16)
& 0xff;
int g1
= (rgb1 >> 8)
& 0xff;
int b1
= rgb1 & 0xff;
int a2
= (rgb2 >> 24)
& 0xff;
int r2
= (rgb2 >> 16)
& 0xff;
int g2
= (rgb2 >> 8)
& 0xff;
int b2
= rgb2 & 0xff;
int a3
= (rgb3 >> 24)
& 0xff;
int r3
= (rgb3 >> 16)
& 0xff;
int g3
= (rgb3 >> 8)
& 0xff;
int b3
= rgb3 & 0xff;
a1
= a2 + (int)
((a1 + a3) * radius);
r1
= r2 + (int)
((r1 + r3) * radius);
g1
= g2 + (int)
((g1 + g3) * radius);
b1
= b2 + (int)
((b1 + b3) * radius);
a1
*= f;
r1
*= f;
g1
*= f;
b1
*= f;
out[outIndex]
= (a1 << 24)
| (r1 << 16)
| (g1 << 8)
| b1;
outIndex
+= height;
}
out[outIndex]
= in[width - 1];
inIndex
+= width;
}
}
public static int clamp(int x,
int a,
int b) {
return (x
< a) ? a : (x > b) ? b : x;
}
public static String getImageUrl(Context context, Uri photoUri){
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(photoUri, proj, null, null, null);
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
//bitmap到文件
public static File getBitmapFile(Bitmap bitmap,String path, String fileName) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
byte[] result = output.toByteArray();//转换成功了
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
File file = BytesToFileUtils.getFile(result, path, fileName);
return file;
}
}
猜你喜欢
- opencv+javacv+eclipse+as局域网下实现安卓端控制电脑摄像头打开,定时获取摄像头捕获图片
- android自定义相册实现上传图片
- Android图片文件上传实例
- android获取音频视频图片信息工具包
- android手势检测开发实战,打造支持缩放平移的图片预览效果
- Android中Gallery图片浏览
- android通过LayoutParams.setMargins方法实现图片移动到鼠标点击坐标的特效
- Android开发中在webview访问的html页面中添加js响应图片点击事件
- android 动态放大缩小ImageView里的图片
- android自定义相册实例,支持大量相册图片读取秒开
- android TTS 语音,利用系统自带的API来语音提示,不支持中文
- Android波形发生器
请下载代码后再发表评论
相关代码
- android TTS 语音,利用系统自带的API来语音提示,不支持中文
- Android波形发生器
- PhoneGap+HTML5+jqueryMobile 开发安卓-DATE控件
- Android闹钟程序
- Android开发WIFI局域网对讲机WifiTalk
- android记事本源代码下载,很简单的代码实例,绝对给力
- android手机软件android-PSS进销存管理系统源代码下载
- 原 Android学习开发的个人简单APP
- 原 Android简单计算器程序
- android开发实例课程表源代码下载
- android上吹一吹功能的实现代码
- android系统中获取imei号 手机号码 imsi号码的工具类
最近下载
最近浏览
yyyyyyzh LV8
2023年6月11日
微信网友_5976099117568000 LV1
2022年5月27日
969334274 LV1
2021年12月14日
xierongsong LV1
2021年11月28日
jkysll LV7
2021年11月14日
wxwxy1111 LV1
2021年7月2日
SoulDream LV7
2021年6月25日
Kermit_Tangible LV3
2021年6月6日
tomfish000 LV2
2021年5月31日
Qxjh00 LV2
2021年3月4日



