图片的处理包括以下:
1, 缩放
2, 倾斜
3, 旋转
4, 缩放+
5, 平移
6, 镜像
代码如下:
- package com.mike.activity;
- import java.io.FileNotFoundException;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.BitmapFactory.Options;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Display;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class ImageDemoActivity extends Activity implements OnClickListener {
- /** Called when the activity is first created. */
- public static String TAG = "IMAGE";
- public static int REQUEST_CODE = 0;
- private ImageView mImageShow;
- private ImageView mImageAltered;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button p_w_picpathSelectBtn = (Button) findViewById(R.id.p_w_picpathSelectBtn);
- mImageShow = (ImageView) findViewById(R.id.p_w_picpathShow);
- mImageAltered = (ImageView) findViewById(R.id.p_w_picpathAltered);
- p_w_picpathSelectBtn.setOnClickListener(this);
- }
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 启动照片Gallery
- startActivityForResult(intent, 0);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- Intent intent) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, intent);
- if (resultCode == RESULT_OK) {// 操作成功
- Uri imgFileUri = intent.getData();// 获得所选照片的信息
- Log.d(TAG, "imgFileUri is :" + imgFileUri);
- // 由于返回的图像可能太大而无法完全加载到内存中。系统有限制,需要处理。
- Display currentDisplay = getWindowManager().getDefaultDisplay();
- int defaultHeight = currentDisplay.getHeight();
- int defaultWidth = currentDisplay.getWidth();
- BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
- bitmapFactoryOptions.inJustDecodeBounds = false;// /只是为获取原始图片的尺寸,而不返回Bitmap对象
- // 注上:If set to true, the decoder will return null (no bitmap), but
- // the out... fields will still be set,
- // allowing the caller to query the bitmap without having to
- // allocate the memory for its pixels
- try {
- Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
- .openInputStream(imgFileUri), null,
- bitmapFactoryOptions);
- int outHeight = bitmapFactoryOptions.outHeight;
- int outWidth = bitmapFactoryOptions.outWidth;
- int heightRatio = (int) Math.ceil((float) outHeight
- / defaultHeight);
- int widthRatio = (int) Math.ceil((float) outWidth
- / defaultWidth);
- if (heightRatio > 1 || widthRatio > 1) {
- if (heightRatio > widthRatio) {
- bitmapFactoryOptions.inSampleSize = heightRatio;
- } else {
- bitmapFactoryOptions.inSampleSize = widthRatio;
- }
- }
- bitmapFactoryOptions.inJustDecodeBounds = false;
- bitmap = BitmapFactory.decodeStream(getContentResolver()
- .openInputStream(imgFileUri), null,
- bitmapFactoryOptions);
- mImageShow.setImageBitmap(bitmap);
- // /*
- // * 在位图上绘制位图
- // */
- //
- // Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(),
- // bitmap.getHeight(), bitmap.getConfig());
- //
- // Canvas canvas = new
- // Canvas(bitmapAltered);//bitmap提供了画布,只在此提供了大小尺寸,偏移后并未有背景显示出来
- //
- //
- // Paint paint = new Paint();
- //
- // canvas.drawBitmap(bitmap, 0, 0, paint);//绘制的图片和之前的一模一样
- //
- // mImageAltered.setImageBitmap(bitmapAltered);
- /*
- * 使用矩阵
- */
- Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(),
- bitmap.getHeight(), bitmap.getConfig());// 缺点是限定画布大小,处理后的图片可能被截断,可引入矩阵构造方法,动态设定画布大小
- Canvas canvas = new Canvas(bitmapAltered);// bitmap提供了画布,只在此提供了大小尺寸,偏移后并未有背景显示出来,
- Paint paint = new Paint();
- Matrix matrix = new Matrix();
- // 1:缩放
- // matrix.setValues(new float[] {//涉及线性代数的知识,可以翻翻相乘的基础知识
- // 1,0,0,//算出x的值
- // 0,1,0,//算出y的值
- // 0,0,1//二维图用不到
- // });
- // 2:倾斜
- // matrix.setValues(new float[] {//涉及线性代数的知识,可以翻翻相乘的基础知识
- // 1,.5f,0,//算出x的值,x,y值相关
- // 0,1,0,//算出y的值
- // 0,0,1//二维图用不到
- // });
- // 3:旋转
- // matrix.setRotate(45);//设置旋转角度,(0,0)点,顺时针旋转45度
- // 4,缩放plus:使用Matrix类方法//需要修改画布宽度>1.5倍* 原始宽度
- // matrix.setScale(1.5f, 1);
- // 5,平移
- // matrix.setTranslate(10, 0);
- // 6,镜像
- // eg1:x轴镜像
- // matrix.setScale(-1, 1);//向左绘制,原始图片沿y轴对称
- // matrix.postTranslate(bitmap.getWidth(), 0);//注意是post即后移动
- // eg2:y轴镜像
- matrix.setScale(1, -1);// 向上绘制,原始图片沿x轴对称,图像在第四象限
- matrix.postTranslate(0, bitmap.getHeight());// 注意是post即后移动
- //~optimize~因为指定画布大小bitmapAltered,所以处理后的图片可能被截断,可以通过矩阵动态改变bitmapAltered大小
- //eg: matrix.setRotate(degrees, px, py)
- //bitmapAltered = Bitmap.createBitmap(source, x, y, width, height, m, filter);//显然矩阵影响其大小
- //
- // 注:for more details :Wikipedia Transformation Matrix
- // http://en.wikipedia.org/wiki/Transformation_matrix
- canvas.drawBitmap(bitmap, matrix, paint);// 绘制的图片和之前的一模一样
- mImageAltered.setImageBitmap(bitmapAltered);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
转载于:https://blog.51cto.com/mikewang/830119