一、什么是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

更多相关文章

  1. Android(安卓)开发中遇到的 bug(13)
  2. Android(安卓)Tween动画之RotateAnimation实现图片不停旋转
  3. android没有插SD卡/TF卡(TF卡其实就是microSD卡)时的图库(拍照的照
  4. Android窗口标题显示操作
  5. android 按钮自定义
  6. Android图片自适应不同分辨率屏幕问题
  7. Android之SQLite数据库操作
  8. android 中asynctask的一些研究
  9. android 串口提权

随机推荐

  1. Android++:为Android(安卓)App开发而生的V
  2. Android(安卓)--- 使用SAX读取xml文件
  3. 创建android文件系统
  4. android系统学习笔记三
  5. Android广播管理二--广播注册(registerRec
  6. 友盟2011第三季度Android数据报告
  7. 【android】:android实现一个加法计算器
  8. android中如何设计触摸屏驱动touch scree
  9. Android(安卓)属性动画详解
  10. Android中定时器的3种实现方法