前一篇文章讲述了Android拍照、截图、保存并显示在ImageView控件中,该篇文章继续讲述Android图像处理技术,主要操作包括:通过打开相册里的图片,使用Matrix对图像进行缩放、旋转、移动、对比度、亮度、饱和度操作,希望对大家有所帮助.

一. 显示打开图片

    首先,设置activity_main.xml布局如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="wrap_content"tools:context="com.example.cangeimagetest.MainActivity"tools:ignore="MergeRootFrame" ><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" >    <Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择图片" /><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="invisible"android:text="原图显示" /><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_gravity="center_horizontal"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="invisible"android:text="变化后的图片" /><ImageViewandroid:id="@+id/imageView2"android:layout_gravity="center_horizontal"android:layout_marginBottom="20dp" android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_alignParentBottom="true" ><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="缩小" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="放大" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="旋转" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="饱和" /><Buttonandroid:id="@+id/button6"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="对比" /></LinearLayout>
</RelativeLayout>

    然后,在Mainctivity.java中public class MainActivity extends Activity函数添加代码如下:

private Button selectBn;
private ImageView imageShow;
private ImageView imageCreate;
private TextView textview1;
private TextView textview2;
private Bitmap bmp; //原始图片@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);selectBn = (Button) findViewById(R.id.button1);imageShow = (ImageView) findViewById(R.id.imageView1);imageCreate = (ImageView) findViewById(R.id.imageView2);textview1 = (TextView) findViewById(R.id.textView1);textview2 = (TextView) findViewById(R.id.textView2);//选择图片selectBn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(intent, 0 );}});if (savedInstanceState == null) {getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();}
}
//显示两张图片
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  super.onActivityResult(requestCode, resultCode, data);if(resultCode==RESULT_OK) {ShowPhotoByImageView(data);     //显示照片CreatePhotoByImageView();          //创建图片}
}

    再调用自定义函数实现显示图片:

//自定义函数 显示打开的照片在ImageView1中
public void ShowPhotoByImageView(Intent data) {Uri imageFileUri = data.getData();DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int width = dm.widthPixels;    //手机屏幕水平分辨率int height = dm.heightPixels;  //手机屏幕垂直分辨率Log.v("height", ""+height );Log.v("width", ""+width);try {// Load up the image's dimensions not the image itselfBitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();bmpFactoryOptions.inJustDecodeBounds = true;bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);Log.v("bmpheight", ""+bmpFactoryOptions.outHeight);Log.v("bmpheight", ""+bmpFactoryOptions.outWidth);if(heightRatio>1&&widthRatio>1) {if(heightRatio>widthRatio) {bmpFactoryOptions.inSampleSize = heightRatio*2;}else {bmpFactoryOptions.inSampleSize = widthRatio*2;}}//图像真正解码   bmpFactoryOptions.inJustDecodeBounds = false;                 bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);  imageShow.setImageBitmap(bmp); //将剪裁后照片显示出来  textview1.setVisibility(View.VISIBLE);} catch(FileNotFoundException e) {e.printStackTrace();}
}
//创建第二张图片并显示
public void CreatePhotoByImageView() {try {Bitmap createBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());Canvas canvas = new Canvas(createBmp); //画布 传入位图用于绘制Paint paint = new Paint(); //画刷 改变颜色 对比度等属性canvas.drawBitmap(bmp, 0, 0, paint);    //错误:没有图片 因为参数bmp写成createBmpimageCreate.setImageBitmap(createBmp);textview2.setVisibility(View.VISIBLE);} catch(Exception e) {e.printStackTrace();}
}

    显示的效果如下图所示,该图叫莱娜图(Lenna),是图像处理中经常使用的样例图.

二. Matrix操作

   然后通过Matrix对图像进行处理操作,在onCreate函数中添加点击事件:

//缩小图片
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {SmallPicture();}
});
//放大图片Button button3=(Button)findViewById(R.id.button3);button3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {BigPicture();}});//旋转图片
Button button4=(Button)findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {TurnPicture();}
});
//图片饱和度改变
Button button5=(Button)findViewById(R.id.button5);
button5.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {SaturationPicture();}
});
//图片对比度改变
Button button6=(Button)findViewById(R.id.button6);
button6.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {ContrastPicture();}
});

    最后分别自定义函数各操作实现,代码如下:

//缩小图片
private void SmallPicture() {Matrix matrix = new Matrix();//缩放区间 0.5-1.0if(smallbig>0.5f)smallbig=smallbig-0.1f;elsesmallbig=0.5f;//x y坐标同时缩放matrix.setScale(smallbig,smallbig,bmp.getWidth()/2,bmp.getHeight()/2); Bitmap createBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());Canvas canvas = new Canvas(createBmp); //画布 传入位图用于绘制Paint paint = new Paint(); //画刷 改变颜色 对比度等属性canvas.drawBitmap(bmp, matrix, paint);imageCreate.setBackgroundColor(Color.RED);imageCreate.setImageBitmap(createBmp);textview2.setVisibility(View.VISIBLE);}//放大图片
private void BigPicture() {Matrix matrix = new Matrix();//缩放区间 0.5-1.0if(smallbig<1.5f)smallbig=smallbig+0.1f;elsesmallbig=1.5f;//x y坐标同时缩放matrix.setScale(smallbig,smallbig,bmp.getWidth()/2,bmp.getHeight()/2); Bitmap createBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());Canvas canvas = new Canvas(createBmp); Paint paint = new Paint(); canvas.drawBitmap(bmp, matrix, paint);imageCreate.setBackgroundColor(Color.RED);imageCreate.setImageBitmap(createBmp);textview2.setVisibility(View.VISIBLE);
}
//旋转图片
private void TurnPicture() {Matrix matrix = new Matrix();turnRotate=turnRotate+15;//选择角度 饶(0,0)点选择 正数顺时针 负数逆时针 中心旋转matrix.setRotate(turnRotate,bmp.getWidth()/2,bmp.getHeight()/2); Bitmap createBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());Canvas canvas = new Canvas(createBmp); Paint paint = new Paint(); canvas.drawBitmap(bmp, matrix, paint);imageCreate.setBackgroundColor(Color.RED);imageCreate.setImageBitmap(createBmp);textview2.setVisibility(View.VISIBLE);
}
//改变图像饱和度
private void SaturationPicture() {//设置饱和度 0表示灰度图像 大于1饱和度增加 0-1饱和度减小ColorMatrix cm = new ColorMatrix();cm.setSaturation(saturation);Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cm));//显示图片Matrix matrix = new Matrix();Bitmap createBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());Canvas canvas = new Canvas(createBmp); canvas.drawBitmap(bmp, matrix, paint);imageCreate.setImageBitmap(createBmp);textview2.setVisibility(View.VISIBLE);saturation=saturation+0.1f;if(saturation>=1.5f) {saturation=0f;}
}
//设置图片对比度
private void ContrastPicture() {ColorMatrix cm = new ColorMatrix();float brightness = -25;  //亮度float contrast = 2;        //对比度cm.set(new float[] {contrast, 0, 0, 0, brightness,0, contrast, 0, 0, brightness,0, 0, contrast, 0, brightness,0, 0, 0, contrast, 0});Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cm));//显示图片Matrix matrix = new Matrix();Bitmap createBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());Canvas canvas = new Canvas(createBmp); canvas.drawBitmap(bmp, matrix, paint);imageCreate.setImageBitmap(createBmp);textview2.setVisibility(View.VISIBLE);
}

    同时自定义变量如下:

//图片变换参数
private float smallbig=1.0f;   //缩放比例
private int turnRotate=0;       //旋转度数
private float saturation=0f;    //饱和度

    它的运行结果如下图所示:

   

   

   

    需要指出的是:该项目仅仅讲述处理的过程,并没有考虑很多因素,如:有的图像显示可能超出屏幕,没有载入图片点击处理按钮报错,横竖屏切换导致不显示图片,最下面按钮可能被遮挡,图像放大画布没有变,因为为认为显示一张改变后的图片效果更好,而该工程仅仅是对比.图像缩放移动触屏变换更好,下一篇讲述.
    XML布局推荐:http://www.apkbus.com/forum.php?mod=viewthread&tid=44949
    解决画布跟着图片放大:http://www.eoeandroid.com/thread-3162-1-1.html

三. Matrix处理的原理

    Android中可以通过Matrix和ColorMatrix对图像进行处理.
    1.Matrix
    图像空间变换,包括旋转、剪裁、缩放或移动.Matrix类中每个数字都将应用于图像上每个点的3个坐标x\y\z之一.
    如下代码通过setValues设置值.(1,0,0)表示x坐标转换x=1x+0y+0z,同样y=0x+1y+0z,z=0x+0y+1z.该矩阵不做任何变换.
如果第一行改为(.5f,0,0),那么图像在x轴上将图像压缩50%.移动见setTranslate()函数.

Matrix matrix = new Matrix();
matrix.setValues(new float[] {1, 0, 0,0, 1, 0,0, 0, 1
});

    2.ColorMatrix
    在Canvas(画布)对象上绘制时既可使用Matrix方法,也可使用ColorMatrix来改变在Canvas对象上绘制的Paint(画刷)对象.对图像的像素处理时,每个像素由RGBA值组成(Red Green Blue Alpha).具体方法推荐博文:http://www.cnblogs.com/leon19870907/articles/1978065.html
    最后希望该文章对大家有所帮助,尤其是Android初学者.该文章是讲述Android使用Matrix处理图片的基础文章,如果有不足或错误地方,请见谅~参考资料《
Android多媒体开发高级编程 著:Shawn Van Every》
    下载地址:http://download.csdn.net/detail/eastmount/8082043
(By:Eastmount 2014-10-26 夜2点 http://blog.csdn.net/eastmount)

[Android] 使用Matrix矩阵类对图像进行缩放、旋转、对比度、亮度处理相关推荐

  1. JavaScript实现完整的matrix矩阵类(附完整源码)

    JavaScript实现完整的matrix矩阵类(附完整源码) matrix.js完整源代码 matrix.js完整源代码 export const shape = (m) => {const ...

  2. R语言使用magick包的image_rotate函数、image_flip函数、image_flop函数对图像进行缩放旋转、镜像、翻转(Rotate or mirror the image)

    R语言使用magick包的image_rotate函数.image_flip函数.image_flop函数对图像进行缩放旋转.镜像.翻转(Rotate or mirror the image) 目录

  3. Android开发--Matrix(二)--实现图片的旋转

    Matrix功能很是强大,利用这个类提供的一系列方法,我们可以实现图片的旋转. 下面以一个例子说明实现方法. 首先,我们看下实现的截图: 下面给出具体的实现代码: 1.xml布局文件 <?xml ...

  4. 一、Android Matrix 矩阵

    一.Android矩阵 大学学的线性代数和矩阵基本忘记的差不多了,理解起矩阵Matrix着实有点费劲,记了一次笔记还把左乘右乘记错了. 1.1 使用场景 项目中会使用到矩阵的场景: 背景图片,指定位置 ...

  5. Android自定义控件系列——Paint类全解析

    Paint 常量 常量名 作用 ANTI_ALIAS_FLAG 抗锯齿标志 DITHER_FLAG 防抖动标志 EMBEDDED_BITMAP_TEXT_FLAG 绘制标记,在绘制文本时使用位图字体. ...

  6. 7-67 使用二维数组实现Matrix(矩阵)。 (60 分)

    使用二维数组实现Matrix(矩阵). 定义Matrix(矩阵)类,要求如下: a) 变量:matrix(int型二维数组),row(行数),column(列数): b) 方法:实现两个矩阵的乘法,所 ...

  7. python定义一个1xn矩阵_Python实现的矩阵类实例

    本文实例讲述了Python实现的矩阵类.分享给大家供大家参考,具体如下: 科学计算离不开矩阵的运算.当然,python已经有非常好的现成的库: 我写这个矩阵类,并不是打算重新造一个轮子,只是作为一个练 ...

  8. Android 自定义View ——Matrix (矩阵)

    Matrix的作用: Matrix类包含一个3x3矩阵,用于转换坐标 Matrix (矩阵) 的原理很遗憾自己目前也是含糊的很,这里就不说了,记录自己在项目使用的方法, 这里就简单的记录下Matrix ...

  9. android开发图片格式,Android程序开发如何处理图像格式类及图像转换

    在Android程序开发过程中,明确哪些图像格式类(ImageFormat.PixelFormat及BitmapConfig等)及图像(JPG.PNG及BMP等)的转换方式非常重要,在以后的程序开发过 ...

最新文章

  1. vue 传递多行数据_vue 数据传递的方法
  2. Latex注释快捷键
  3. java 正则表达式 table_Java 使用正则表达式
  4. 磁力计简单水平较准算法
  5. Xamarin字体设置
  6. HTML5七夕情人节表白网页制作【花瓣图片表白】HTML+CSS+JavaScript html生日快乐祝福网页制作
  7. Qt 常见错误及坑锦集(更新中....)
  8. 猿辅导python资源_2020猿辅导(小猿搜题)高中辅导全资源合集百度网盘下载
  9. 你真的懂Linux内核中的阻塞和异步通知机制吗?(花了五天整理,墙裂推荐!)
  10. 计算机教育学研究方法,广西师大 教育学 孙杰远《教育研究方法》知识点笔记1.pdf...
  11. 获取win10锁屏壁纸
  12. OFDM技术与FDM技术区别
  13. 手把手教你用 tornado 设计 web 项目
  14. wps思维导图聚焦模式是灰色不能用的解决方案
  15. 蓝桥杯 ADV-201 算法提高 我们的征途是星辰大海
  16. Android数字华容道代码,Android源码 之《最强大脑》“数字华容道”
  17. 分享一下自己用的SQLite数据库密码操作小工具(含源码)
  18. 从爬取豆瓣影评到基于朴素贝叶斯的电影评论情感分析(上)
  19. Jina AI创始人肖涵博士解读多模态AI的范式变革
  20. idea如何配置显示多行tab标签

热门文章

  1. Oracle Awr
  2. git的忽略文件语法规范
  3. 2 -13 作业需求
  4. php-fpm.conf 解析
  5. mousedown(function(){ return false; })作用
  6. libevent(1)
  7. 最大熵学习笔记(六)优缺点分析
  8. Jquery创建JSON对象
  9. 数据仓库:Oracle Exadata和Netezza的比较
  10. 设计一个简单的缓存容器