BitmapHelper

提供一些获取本地缩略图,获取网络图片。dp与px的相互转换等方法。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.Base64; public class BitmapHelper { private static BitmapHelper util;
private static Context context = App.getInstance(); public static BitmapHelper getInstance() { if (util == null) {
util = new BitmapHelper(); }
return util; } private BitmapHelper() {
super();
} /**
* 将bitmap转为base64
*
* @param bitmap
* @param imgFormat
* @return
*/
@SuppressLint("NewApi")
public String bitmapToBase64(Bitmap bitmap, String imgFormat) { if (null == bitmap) { return null;
}
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
if (imgFormat.equalsIgnoreCase("png"))
compressFormat = Bitmap.CompressFormat.PNG;
bitmap.compress(compressFormat, 85, out);
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
return Base64.encodeToString(imgBytes, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} /**
* 将图片转为base64
*
* @param imgPath
* @param imgFormat
* @return
*/
@SuppressLint("NewApi")
public String urlToBase64(String imgPath, String imgFormat) {
Bitmap bitmap = null;
if (imgPath != null && imgPath.length() > 0) {
bitmap = readBitmap(imgPath);
}
return bitmapToBase64(bitmap, imgFormat); } private Bitmap readBitmap(String imgPath) {
try {
return BitmapFactory.decodeFile(imgPath);
} catch (Exception e) {
return null;
}
} /**
* 得到圆角的bitmap
*
* @param bitmap
* @param corner
* 以长或宽的比例为半径,2表示二分之中的一个。8表示八分之中的一个
* @return
*/
public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int corner) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / corner;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / corner;
float clip = (width - height) / corner;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
} /**
* 设置Bitmap圆角
*
* @param image
* 传入的Bitmap
* @param outerRadiusRat
* 圆角半径
* @return 返回处理后的Bitmap
*/
public Bitmap getRoundedBitmap(Bitmap image, int outerRadiusRat) {
int x = image.getWidth();
int y = image.getHeight();
// 依据源文件新建一个darwable对象
Drawable imageDrawable = new BitmapDrawable(image); // 新建一个新的输出图片
Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output); // 新建一个矩形
RectF outerRect = new RectF(0, 0, x, y); // 产生一个红色的圆角矩形
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint); // 将源图片绘制到这个圆角矩形上
// 具体解释见http://lipeng88213.iteye.com/blog/1189452
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
imageDrawable.setBounds(0, 0, x, y);
canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
imageDrawable.draw(canvas);
canvas.restore(); return output;
} /**
* 图片压缩
*
* @param image
* @return
*/
public Bitmap getCompressBitmap(Bitmap image, int minSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > minSize) {
options -= 10;
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
} /**
* 得到本地缩略图
*
* @param srcPath
* @return
*/
public Bitmap getCompressBitmapByLocal(String srcPath, int ww, int hh) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight; int 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;
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return getCompressBitmap(bitmap, 100);
} /**
* 放大缩小图片
*
* @param bitmap
* @param w
* @param h
* @return
*/
public Bitmap getZoomBitmap(Bitmap bitmap, int w, int h) {
Bitmap newbmp = null;
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidht, scaleHeight);
newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
}
return newbmp;
} /**
* 得到网络图片
*
* @param imgUrl
* @return
*/
public Bitmap getHttpBitmap(String imgUrl) { Bitmap bitmap = null;
if (!PhoneHelper.getInstance().isNetworkConnected()) { return null;
} InputStream is = getHttpPicInputStream(imgUrl);
if (is != null) { bitmap = BitmapFactory.decodeStream(is); try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } return bitmap; } /**
* 得到网络图片的输入流
*
* @param url
* @return
*/
public InputStream getHttpPicInputStream(String url) {
InputStream is = null;
URL url1 = null;
try {
url1 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setRequestMethod("GET");
conn.connect(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = conn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return is;
} /**
* Bitmap转化为drawable
*
* @param bitmap
* @return
*/
public Drawable bitmap2Drawable(Bitmap bitmap) {
return new BitmapDrawable(bitmap);
} /**
* Drawable 转 bitmap
*
* @param drawable
* @return
*/
public Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
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);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
} /**
* 将bitmap存为本地图片
* @param bitmap
* @param path
* @return
*/
public boolean saveBitmapForSdCard(Bitmap bitmap, String path) {
File f = new File(path);
File parent = new File(f.getParent());
FileOutputStream out = null;
if (!parent.exists()) {
parent.mkdirs();
}
try {
f.createNewFile();
out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close(); return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* dp转px
*
* @param context
* @param dpValue
* @return
*/
public int dp2Px(float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
} /**
* px转dp
*/
public int px2Dp(float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f);
} /**
* 依据网络状态获取本地图片缩略图
*
* @param picPath
* @return
*/
public Bitmap getBitmapByNetWork(String picPath) {
int networkType = PhoneHelper.getInstance().getNetType(); Bitmap tagBm = null;
ByteArrayOutputStream decodeBitmap = null;
int size = 100;
switch (networkType) {
case 1:
size = 150;
break;
case 2:
size = 80;
break;
case 3:
case 4:
size = 100;
break; default:
return null; }
decodeBitmap = getBitmapByteArray(picPath, size); if (decodeBitmap != null) {
byte[] array = decodeBitmap.toByteArray();
try {
tagBm = BitmapFactory.decodeByteArray(array, 0, array.length);
} catch (OutOfMemoryError oError) {
oError.printStackTrace();
} catch (Exception e) {
} } return tagBm; } /**
* 将本地图片转为输出流
* @param path
* @param size
* @return
*/
public ByteArrayOutputStream getBitmapByteArray(String path, int size) { BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,仅仅获取bitmap宽高
BitmapFactory.decodeFile(path, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800); opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,由于之前我们将其设置成了true
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inDither = false;
opts.inPurgeable = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inTempStorage = new byte[100 * 1024];
FileInputStream is = null;
Bitmap bmp = null; ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(path);
bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts); baos = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > size && options > 4) { // 循环推断假设压缩后图片是否大于100kb,大于继续压缩
baos.reset();// 重置baos即清空baos
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%。把压缩后的数据存放到baos中
options -= 2;// 每次都降低10
}
bmp.recycle();
return baos;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (OutOfMemoryError oError) {
oError.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (baos != null) {
baos.close();
} } catch (IOException e) {
e.printStackTrace();
}
System.gc();
}
return baos;
} /**
* 为了得到恰当的inSampleSize,Android提供了一种动态计算的方法
* @param options
* @param minSideLength
* @param maxNumOfPixels
* @return
*/
private int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels); int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
} return roundedSize;
} private int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) {
return lowerBound;
} if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
} }

Bitmap工具类BitmapHelper的更多相关文章

  1. Bitmap工具类

    一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...

  2. Android BitmapUtils工具类

    Bitmap工具类 public final class BitmapUtils { public static final String TAG = "BitmapUtil"; ...

  3. Android-BitmapUtil工具类

    Bitmap工具类,获取Bitmap对象 public class BitmapUtil { private BitmapUtil(){} /** * 根据资源id获取指定大小的Bitmap对象 * ...

  4. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  5. Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

  6. java工具类

    1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...

  7. 随笔分类 - Android之工具类

    Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...

  8. Android 系统工具类SystemUtils

    包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...

  9. 转:工具类之SpannableStringUtils(相信你会爱上它)

    这个工具类真是构思了良久才设计出来,采用了建造者模式,然后你们就可以用链式调用了,talk is cheap, let me show the demo. demo code 有没有心动一下哈,下面就 ...

随机推荐

  1. mysql若干问题

    一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...

  2. C++ const学习

    概念 const就是为了直接表达“不变化的值”这一概念.也就是说该值只可读,不可直接写. 由于不可以修改,所以const常量在声明的时候必须初始化 const int a; //error exter ...

  3. poj1778 All Discs Considered

    思路: 拓扑排序.贪心. 实现: #include <bits/stdc++.h> using namespace std; vector<]; int n1, n2; inline ...

  4. P2668 斗地主 dp+深搜版

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  5. Python 将中文转拼音

    文字转拼音 import os.path class PinYin(object): def __init__(self): self.word_dict = {} def load_word(sel ...

  6. typeloadexception 方法实现中引用的声明不能是final方法

    问题描述: 1. 修改了DVSNetClient项目,其依赖类库CameraDSP没有改动.CameraDSP_DVSNetClient.dll的版本编号和文件编号由1.0.0.0变为1.0.1.0. ...

  7. c++中的类型转换--reinterpret_cast

    原文链接:  浅析c++中的类型转换--reinterpret_cast转换 reinterpret_cast作用为: 允许将任何指针转换为任何其他指针类型. 也允许将任何整数类型转换为任何指针类型以 ...

  8. 世界上最受欢迎的10个Linux发行版

    帮助新的Linux用户在越来越多的Linux发行版中选择最合适的操作系统,是创建这个网页的原因.它列出了迄今为止最流行的10个Linux发行版(另外增加的是FreeBSD,到目前为止最为流行的BSD系 ...

  9. CAD把当前图上数据保存为一个二进流对象(com接口VB语言)

    主要用到函数说明: MxDrawXCustomFunction::WriteBinStreamEx 把当前图上数据保存为一个二进流对象,详细说明如下: 参数 说明 LPCTSTR pszPasswor ...

  10. 梦想CAD控件网页版扩展数据

    随着基于CAD的应用软件飞速发展,经常需要保存一些与图形可视性无关的数据,即非图形参数.例如在绘制化验样图中包含品位数据.MxCAD定义一类新的参数——实体扩展数据.扩展数据与实体的可视性无关,而是用 ...