当前位置:首页 > 开发 > 移动开发 > 正文

Bitmap常用图片处理方法

发表于: 2015-05-08   作者:elviszhao   来源:转载   浏览次数:
摘要: public class BitmapTool { /** * 获得圆角图片 * * @param bitmap * @param roundPx * 圆角参数 * @return 圆角图片 */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, flo
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;
	}

}

 

Bitmap常用图片处理方法

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Winform下的图像处理比较郁闷,动不动就蹦出这个 OutOfMemory 异常而不给具体原因。刚才谈新客户,
Java代码 //压缩图片大小 public static Bitmap compressImage(
前言 美术和程序的配合,需要程序能够很快抓住问题重点并提出解决方案. 步骤 准备 美术提供的数字图
Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos
// http://www.3lian.com/edu/2014/06-18/149890.html 今天想把图片背景做成透明效果,以前叫美工的
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos
前言 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视平
转自: http://gundumw100.iteye.com/blog/849729 Java代码 //压缩图片大小 public static Bitmap c
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号