android 图片Bitmap的旋转

一、什么是Android中的Bitmap

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。


二、Bitmap的剪切基本操作

位图的旋转也可以借助Matrix或者Canvas来实现。

通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最后用drawBitmap方法绘制到屏幕上,于是就实现了旋转操作。

三、Bitmap剪切的封装

实际使用中,因为项目需要时常需要对基本功能进行封装,下面是一段封装的代码,仅供参考。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

public class BitmapRotating
{

	/**
	 * 通过资源id转化成Bitmap
	 * 
	 * @param context
	 * @param resId
	 * @return
	 */
	public static Bitmap ReadBitmapById(Context context, int resId)
	{
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;

		InputStream is = context.getResources().openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}

	
	/**
	 * Bitmap --> byte[]
	 * 
	 * @param bmp
	 * @return
	 */
	public static byte[] readBitmap(Bitmap bmp)
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos);
		try
		{
			baos.flush();
			baos.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return baos.toByteArray();
	}

	
	/**
	 * 读取图片旋转的角度
	 * 
	 * @param filename
	 * @return
	 */
	public static int readPictureDegree(String filename)
	{
		int rotate = 0;
		try
		{
			ExifInterface exifInterface = new ExifInterface(filename);
			int result = exifInterface.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_UNDEFINED);

			switch (result)
			{
				case ExifInterface.ORIENTATION_ROTATE_90:
					rotate = 90;
					break;
				case ExifInterface.ORIENTATION_ROTATE_180:
					rotate = 180;
					break;
				case ExifInterface.ORIENTATION_ROTATE_270:
					rotate = 270;
					break;
				default:
					break;
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		return rotate;
	}

	/**
	 * 旋转图片
	 * 
	 * @param angle
	 * @param bitmap
	 * @return
	 */
	public static Bitmap rotaingImageView(int angle, Bitmap bitmap)
	{
		// 旋转图片 动作
		Matrix matrix = new Matrix();
		matrix.postRotate(angle);

		// 创建新的图片
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
				bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		if (resizedBitmap != bitmap && bitmap != null && !bitmap.isRecycled())
		{
			bitmap.recycle();
			bitmap = null;
		}

		return resizedBitmap;
	}

	/**
	 * 读取图片旋转的角度
	 * 
	 * @param filename
	 * @return
	 */
	public static void setPictureDegree(String filename, int degree)
	{
		try
		{
			if (degree == 0)
			{
				return;
			}

			int rotate = ExifInterface.ORIENTATION_UNDEFINED;
			switch (degree)
			{
				case 90:
					rotate = ExifInterface.ORIENTATION_ROTATE_90;
					break;
				case 180:
					rotate = ExifInterface.ORIENTATION_ROTATE_180;
					break;
				case 270:
					rotate = ExifInterface.ORIENTATION_ROTATE_270;
					break;
				default:
					break;
			}

			ExifInterface exifInterface = new ExifInterface(filename);
			exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,
					String.valueOf(rotate));
			exifInterface.saveAttributes();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
}

示例代码:http://download.csdn.net/detail/stop_pig/7162695

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值