Android图像处理技术(实现Android中的PS)(四)
来源:互联网 发布:美萍软件介绍 编辑:程序博客网 时间:2024/05/16 00:24
前三个博文都是讲解的图像色彩变换,从本博文开始,我们开始探讨图像变换
图像色彩变换的方法:矩阵法,像素点法
图像变换的方法:矩阵法,像素块法,画笔风格法:
今天我们主要介绍:矩阵法
首先上几张图,刷刷学好数学的重要性:
哈哈,爽不爽,自己看吧,下面的例子就是利用的这些知识。
今天的Demo是通过矩阵法对图像进行变换:
首先,上个效果图:
下面我们开始代码的编写工作,同样的,我在代码中加了大量注释,大家自己看看吧,不再另作解析了。
首先是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >//自定义View,后面会讲到 <com.chillax.test.ImageMatrixView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" />//装填矩阵 <GridLayout android:id="@+id/group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:columnCount="3" android:rowCount="3" > </GridLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="change" android:text="Change" /> <Button android:id="@+id/reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="reset" android:text="Reset" /> </LinearLayout></LinearLayout>
下面是自定义的ImageMatrixView的代码:
/** * * 关于父类构造函数的继承问题,android开发者网站上有相关的说明文档: * public View (Context context)是在java代码创建视图的时候被调用,如果是从xml填充的视图,就不会调用这个 * public View (Context context, AttributeSet attrs)这个是在xml创建但是没有指定style的时候被调用 * public View (Context context, AttributeSet attrs, int defStyle)这个不用说也懂了吧 * */public class ImageMatrixView extends View{ private Bitmap mBitmap; private Matrix mMatrix; public ImageMatrixView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public void initView(){ mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); setImageMatrix(new Matrix()); } public void setImageMatrix(Matrix matrix){ mMatrix=matrix; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画两个图片,便于比较; canvas.drawBitmap(mBitmap, 0,0, null); canvas.drawBitmap(mBitmap, mMatrix, null); }}
接下来是Activity的代码:
public class ImageMatrix extends Activity { // 待改变的ImageView private ImageMatrixView mImageView; // 创建矩阵 private GridLayout mGridLayout; // 更改后的图片是个bitmap对象 private Bitmap mBitmap; // 矩阵的每个EditText的大小 private int mEtWidth, mExHeight; // 装填这9个EditText private EditText[] mExs = new EditText[9]; // 颜色矩阵的值,放在一个一维数组中 private float[] mMatrix = new float[9]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_matrix); // 从资源中加载一个图片 mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); // 你懂的 mImageView = (ImageMatrixView) findViewById(R.id.imageView); mGridLayout = (GridLayout) findViewById(R.id.group); // 绘制完成后执行run()方法的内容 mGridLayout.post(new Runnable() { @Override public void run() { mEtWidth = mGridLayout.getWidth() / 3; mExHeight = mGridLayout.getHeight() / 3; addEx(); initMatrix(); } }); } // change按钮的响应方法 public void change(View view) { getImageMatrix(); setImageMatrix(); } // reset按钮的响应方法 public void reset(View view) { initMatrix(); getImageMatrix(); setImageMatrix(); } // 得到图片的色彩矩阵 private void getImageMatrix() { for (int i = 0; i < 9; i++) { mMatrix[i] = Float.valueOf(mExs[i].getText().toString()); } } // 设置图片的颜色矩阵 private void setImageMatrix() { Matrix matrix = new Matrix(); matrix.setValues(mMatrix); mImageView.setImageMatrix(matrix); mImageView.invalidate(); // 直接设置放大两倍 // matrix.setScale(2, 2); // 直接设置分别向x,y移动200像素: // matrix.postTranslate(200, 200); /* * Rotate 旋转 Translate 平移 Scale 缩放 Skew 错切 Post 矩阵组合 */ } // 向GridLayout中添加EditText private void addEx() { for (int i = 0; i < 9; i++) { EditText editText = new EditText(ImageMatrix.this); mExs[i] = editText; mGridLayout.addView(editText, mEtWidth, mExHeight); } } // 你懂的。。。 private void initMatrix() { for (int i = 0; i < 9; i++) { if (i % 4 == 0) { mExs[i].setText(String.valueOf(1)); } else { mExs[i].setText(String.valueOf(0)); } } }}
注释是最好的老师,Over。
最后,Demo地址:http://download.csdn.net/detail/nsgsbs/8533949
0 0
- Android图像处理技术(实现Android中的PS)(四)
- Android图像处理技术(实现Android中的PS)(一)
- Android图像处理技术(实现Android中的PS)(二)
- Android图像处理技术(实现Android中的PS)(三)
- Android图像处理技术(实现Android中的PS)(五)
- Android图像处理技术(实现Android中的PS)(六)
- android中的图像处理
- Android中的图像处理
- Android图像处理(四)Xfermode和BitmapShader
- Android绘图机制与处理技巧(四)Android图像处理之图形特效处理
- Android中的简单图像处理
- android图像处理(一)
- Android绘图机制与处理技巧(四)——Android图像处理之画笔特效处理
- Halcon学习(四)图像处理中的两个坐标系
- android图像处理系列之四--给图片添加边框(上)
- android图像处理系列之四--给图片添加边框(上)
- android图像处理系列之四--给图片添加边框(上)
- Android的图形与图像处理之四 补间动画(Tween)
- 黑马程序员——Java基础知识——面向对象(二)
- Hadoop学习笔记之Hadoop简介
- 面向对象学习(4)
- 利用旋转矩阵简便旋转二位矢量(SDacm4A题解)
- web项目中导入外部jar
- Android图像处理技术(实现Android中的PS)(四)
- android 仓库下载分析
- arm-linux-gcc安装和配置
- C语言不支持引用传递和函数重载
- Intent和IntentFilter详解
- 逐顶点和逐像素光照
- 二叉树转换为双向链表
- [Understanding Java Garbage Collection]理解Java垃圾收集(二)
- 深入理解Java内存模型(五)——锁