`

Bitmap常用图片处理方法

阅读更多
public class BitmapTool {

	/**
	 * 获得圆角图片
	 * 
	 * @param bitmap
	 * @param roundPx
	 *            圆角参数
	 * @return 圆角图片
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, w, h);
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}

	/**
	 * 获得带倒影的图片
	 * 
	 * @param bitmap
	 * @return 带倒影的图片
	 */
	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
		final int reflectionGap = 4;
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);

		Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
				h / 2, matrix, false);
		Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
				Config.ARGB_8888);

		Canvas canvas = new Canvas(bitmapWithReflection);
		canvas.drawBitmap(bitmap, 0, 0, null);
		Paint deafalutPaint = new Paint();
		canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
		canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
				bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
				0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		// Set the Transfer mode to be porter duff and destination in
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
				+ reflectionGap, paint);

		return bitmapWithReflection;
	}

	/**
	 * 壓縮圖片,并返回指定格式Bitmap
	 * 
	 * @param pImage
	 *            源圖片
	 * @return 壓縮后的圖片
	 */
	public static Bitmap compressImage(Bitmap pImage, CompressFormat format) {
		if (pImage == null) {
			return null;
		}
		Bitmap bitmap = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// 質量壓縮方法,100表示不壓縮,把壓縮后的數據存放到baos中
			pImage.compress(format, 100, baos);
			int options = 100;
			// 選一判斷如果壓縮后圖片大于200k,繼續壓縮
			while (baos.toByteArray().length / 1024 > 200) {

				baos.reset();
				options -= 10;
				pImage.compress(format, options, baos);
			}
			ByteArrayInputStream isBm = new ByteArrayInputStream(
					baos.toByteArray());
			bitmap = BitmapFactory.decodeStream(isBm);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	/**
	 * 使用Base64將字符串轉換成Bitmap類型
	 * 
	 * @param string
	 *            圖片數據
	 * @return 轉換后的圖片
	 */
	public static Bitmap stringtoBitmap(String pString) {
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(pString, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	/**
	 * 等比例縮放圖圖片,以最小邊長為標準
	 * 
	 * @param pImage
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public static Bitmap zoomImage(Bitmap pImage, int pNewWidth, int pNewHeight) {
		if (pImage == null) {
			return null;
		}
		int width = pImage.getWidth();
		int height = pImage.getHeight();

		if (width <= 0 || height <= 0 || pNewWidth <= 0 || pNewHeight <= 0) {
			return pImage;
		}

		Matrix matrix = new Matrix();
		float scale = 0f;
		float scaleWidth = ((float) pNewWidth) / width;
		float scaleHeight = ((float) pNewHeight) / height;
		scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
		matrix.postScale(scale, scale);
		Bitmap bitmap = Bitmap.createBitmap(pImage, 0, 0, width, height,
				matrix, true);
		return bitmap;
	}

	/**
	 * Bitmap to byte[]
	 * 
	 * @param pBmp
	 *            源圖片
	 * @return 圖片二進制數組
	 */
	static public byte[] BitmapToBytes(Bitmap pBmp) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		pBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

	/**
	 * byte[] to Bitmap
	 * 
	 * @param pByte
	 *            圖片二進制數據
	 * @return Bitmap圖片
	 */
	static public Bitmap BytesToBimap(byte[] pByte) {
		if (pByte.length != 0) {
			return BitmapFactory.decodeByteArray(pByte, 0, pByte.length);
		} else {
			return null;
		}
	}

	/**
	 * 照片转byte二进制
	 * 
	 * @param inStream
	 *            圖片輸入流
	 * @return 已经转成的byte
	 * @throws Exception
	 */
	public static byte[] readStream(InputStream inStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		return data;
	}

	/**
	 * Bitmap Base64編碼
	 * 
	 * @param bitmap
	 *            源圖片
	 * @return 編碼后的字符串
	 */
	static public String bitmaptoString(Bitmap bitmap) {
		// 将Bitmap转换成字符串
		String string = null;
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.WEBP, 100, bStream);
		byte[] bytes = bStream.toByteArray();
		string = Base64.encodeToString(bytes, Base64.DEFAULT);
		return string;
	}

}

 

分享到:
评论

相关推荐

    Android常用的图片加载库

    图片加载涉及到图片的缓存、图片的处理、图片的显示等。四种常用的图片加载框架,分别是Fresco、ImageLoader、 Picasso、 Glide,包括他们各自的优缺点、使用步骤...

    自己收集整理的一些常用的工具类

    BitmapUtil 获取Bitmap和对Bitmap的操作 ChannelUtil 获取市场号 Colors 颜色工具类 包括常用的色值 DES DES加密解密类 DataCleanManager 本应用数据清除管理器 DatabaseExportUtils 应用数据库导出工具类 DateUtil ...

    Android 常用六大框架

    (4) 图片缓存模块:通过FinalBitmap,imageview加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象。 FinalBitmap可以配置线程加载线程数量,缓存大小,缓存路径,...

    C#全能速查宝典

    分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用的属性、方法、类和各种技术,...

    Android开发之超强图片工具类BitmapUtil完整实例

    说明:为了方便大家使用,本人把大家常用的图片处理代码集中到这个类里 使用了LruCache与SoftReference /** * 图片加载及转化工具 ----------------------------------------------------------------------- 延伸...

    CoconutUtil:一个水果儿味儿的工具类

    BitmapUtils 对图片缩放进行了处理,加载本地图片不会OOM异常了 RequestUtils 初始化网络请求、获得一个请求队列、添加一个请求队列等方法 控制反转 ViewUtils 控制反转&依赖注入,快速开发,极少代码来findviewbyid...

    Android 常见的图片加载框架详细介绍

    Android 常见的图片加载框架 图片加载涉及到图片的缓存、图片的处理、图片的显示等。而随着市面上手机设备的硬件水平飞速发展,对图片的...本文主要介绍四种常用的图片加载框架,分别是Fresco、ImageLoader、 Picasso、

    免费.Net条形码组件:Spire.BarCode for .NET

    Spire.BarCode支持包括Bitmap, JPG, PNG, EMF, TIFF, GIF and WMF等众多常用的图片格式。 主要功能: 1.强大的一维、二维条形码识别和生成功能 支持常用的线性条形码,二维条形码,邮政条形码,能从任何方向检测...

    推荐一个界面库:RingSDK

    这是我自己写的界面库,编程一直用他,包含了界面库和图象库。这个库并不是有意为之,而是我多年编程过程中积累下来的。最初只是把一些经常要用到的功能封装成一些函数,建了一个静态库,方便自己编程,后来慢慢积累...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--图片之BitMap、Drawable、inputStream及byte[] 互转 |--图片之保存图片至SD卡 |--图片之删除40%最近没有被使用的 |--图片之的本地缓存至SD卡 |--图片之网络异步下载图片 |--图片之获取SD卡所有及边界可调及压缩和...

    Android典型技术模块开发详解

    目录 第一篇 Android开发初步 第1章 Android初识 1.1 Android简介 1.1.1 认识Android ...16.5.2 普通的图片缩放方法 16.5.3 Dalvik虚拟机的堆内存分配 16.5.4 Bitmap对象及时释放 16.6 多分辨率适应 16.7 本章小结

    多媒体信息的类型.pdf

    图像可以用图 像编辑处理软件(如 Windows 画图、photoshop 等)获得,也可以用扫描仪扫描图 片或使用相机拍照获得。图片常见的格式有:.bmp,.gif,.jpeg,.tiff 等。 2.4 声音 声音,在计算中常称为音频,音频属于...

    Windows环境下32位汇编语言程序设计 第2版(罗文斌) 完整光盘

    用 Bitmap 图片做背景的模拟时钟程序 Chapter07\TestObject ;一些常见的绘图操作 Chapter08\CommDlg ;使用通用对话框 Chapter09\Toolbar ;使用工具栏 Chapter09\StatusBar ;使用状态栏 Chapter09\Richedit ;使用...

    ringSDK环境配置

    这是我自己写的界面库,编程一直用他,包含了界面库和图象库。这个库并不是有意为之,而是我多年编程过程中积累下来的。最初只是把一些经常要用到的功能封装成一些函数,建了一个静态库,方便自己编程,后来慢慢积累...

    Windows环境下32位汇编语言程序设计(最新琢石成器版)附属光盘

    用 Bitmap 图片做背景的模拟时钟程序 Chapter07\TestObject ;一些常见的绘图操作 Chapter08\CommDlg ;使用通用对话框 Chapter09\Toolbar ;使用工具栏 Chapter09\StatusBar ;使用状态栏 Chapter09\Richedit ;...

    Windows环境下32位汇编语言程序设计_随书光盘

    用 Bitmap 图片做背景的模拟时钟程序 Chapter07\TestObject ;一些常见的绘图操作 Chapter08\CommDlg ;使用通用对话框 Chapter09\Toolbar ;使用工具栏 Chapter09\StatusBar ;使用状态栏 Chapter09\Richedit ;使用...

    疯狂Android讲义源码

     7.1.2 Bitmap和BitmapFactory 260  7.2 绘图 263  7.2.1 Android绘图基础:Canvas、  Paint等 263  7.2.2 Path类 267  7.2.3 绘制游戏动画 270  7.3 图形特效处理 278  7.3.1 使用Matrix控制变换 278  ...

    疯狂Android讲义.part2

    7.1.2 Bitmap和BitmapFactory 260 7.2 绘图 263 7.2.1 Android绘图基础:Canvas、 Paint等 263 7.2.2 Path类 267 7.2.3 绘制游戏动画 270 7.3 图形特效处理 278 7.3.1 使用Matrix控制变换 278 7.3.2 使用...

    疯狂Android讲义.part1

    7.1.2 Bitmap和BitmapFactory 260 7.2 绘图 263 7.2.1 Android绘图基础:Canvas、 Paint等 263 7.2.2 Path类 267 7.2.3 绘制游戏动画 270 7.3 图形特效处理 278 7.3.1 使用Matrix控制变换 278 7.3.2 使用...

Global site tag (gtag.js) - Google Analytics