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;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值