BitmapUtil
来源:互联网 发布:天下霸唱抄袭证据知乎 编辑:程序博客网 时间:2024/05/17 08:22
/**
* Bitmap工具类
*/
public class BitmapUtil {
private static BitmapUtil bitmapUtil = new BitmapUtil();
public static BitmapUtil getInstance() { if (bitmapUtil == null) { synchronized (BitmapUtil.class) { if (bitmapUtil == null) { bitmapUtil = new BitmapUtil(); } } } return bitmapUtil;}public boolean savePic(Bitmap bm, String str) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File dir = new File(str); if (!dir.getParentFile().exists()) { dir.getParentFile().mkdirs(); } File file = new File(str); FileOutputStream out = null; try { out = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { StreamUtils.closeStream(out); } return false; } return false;}public Bitmap GetLocalOrNetBitmap(String url) { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream(), 2 * 1024); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, 2 * 1024); copy(in, out); out.flush(); byte[] data = dataStream.toByteArray(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); data = null; return bitmap; } catch (IOException e) { e.printStackTrace(); return null; }}private void copy(InputStream in, OutputStream out) throws IOException { byte[] b = new byte[2 * 1024]; int read; while ((read = in.read(b)) != -1) { out.write(b, 0, read); }}public Bitmap createBitmap(String path) { try { if (TextUtils.isEmpty(path) || !FileUtil.getInstance().isFileExist(path)) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options);// 此时返回bm为空 int scale = 1; while ((options.outWidth / scale > 480)) { scale += 1; } options.inJustDecodeBounds = false; options.inSampleSize = scale; return BitmapFactory.decodeFile(path, options); } catch (Exception e) { return null; }}//将Bitmap转换成字符串public String bitmapToString(Bitmap bitmap) { String string = null; ByteArrayOutputStream bStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream); byte[] bytes = bStream.toByteArray(); string = Base64.encodeToString(bytes, Base64.DEFAULT); return string.trim();}public Bitmap stringtoBitmap(String string) { //将字符串转换成Bitmap类型 Bitmap bitmap = null; try { byte[] bitmapArray; bitmapArray = Base64.decode(string, Base64.DEFAULT); bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length); } catch (Exception e) { e.printStackTrace(); } return bitmap;}public InputStream Bitmap2InputStream(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); InputStream is = new ByteArrayInputStream(baos.toByteArray()); return is;}
}
阅读全文
0 0
- BitmapUtil
- 图片下载工具类:BitmapUtil
- 图片下载工具类:BitmapUtil
- 图片下载工具类:BitmapUtil
- android BitmapUtil 工具类
- 图片下载工具类:BitmapUtil
- 图片下载工具类:BitmapUtil
- 位图工具BitmapUtil
- BitmapUtil 工具类
- Android 图片下载工具类:BitmapUtil
- BitmapUtil 很多有用的方法
- Android 图片下载工具类:BitmapUtil
- Android工具类系列-BitmapUtil
- Android 图片下载工具类:BitmapUtil
- Android 图片转化工具类BitmapUtil
- Android 超强图片工具类BitmapUtil
- 图片下载工具类:BitmapUtil 等小工具类
- Android 各种工具类 图片下载工具类:BitmapUtil
- 【C++】动态规划:最长递增子序列和建桥问题
- 阻止GDB调试
- 多线程master-worker设计
- K-means算法、高斯混合模型 matlab
- 【程序员周边】程序员系列表情包
- BitmapUtil
- 驱动编程(一),NT
- postgresql----继承表INHERITS PARENT TABLE
- 小程序页面布局之Flexbox布局
- 8. 类型声明,方法,接口
- QQ第三方登录
- 用分层结构打造微 MVC 框架
- 关于Qt的QAxWidget
- PL/SQL 快捷键设计以及添加快捷键 .