Android图像处理技术(实现Android中的PS)(二)
来源:互联网 发布:mac 触摸板 鼠标 反向 编辑:程序博客网 时间:2024/04/29 13:05
一.对图像的本质性分析
1.1.抛出问题
我们学习用各种工具改变一张图片,如PS,美图秀秀等图像处理工具,但是这些软件是怎样实现对图片的更改呢,换句话说,我们怎样用代码更改一个图片的外观呢,再者,为什么会改变呢。
2.2本质分析
我们生活中所见的RGB电子图片的色彩,其实是由一个二维矩阵控制的。
我们可以看到,用一个4*5的矩阵乘以颜色矩阵分量,就可以分别得到对应的R,G,B,A,从而调节图片的色彩。
于是,我们通过更改矩阵的值,就可以更改图片的颜色。(至于通过颜色矩阵来计算RGBA和对图片的颜色进行更改,这写都是大牛们的事情了,我们只负责调用接口就好。)
二. 通过颜色矩阵更改图片的色彩
下面我们通过一个Demo来实现通过颜色矩阵更改图片的色彩;
开篇,先试一下效果:
于是我们新建了一个布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >//要更改的图片 <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="2" />//我们会在代码中向GridLayout中添加20个EditText来得到一个颜色矩阵 <GridLayout android:id="@+id/group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:columnCount="5" android:rowCount="4" > </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:text="Change" android:onClick="change" android:layout_weight="1"/> //点击这个按钮对图片进行重置,恢复初始状态。 <Button android:id="@+id/reset" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Reset" android:onClick="reset" android:layout_weight="1"/> </LinearLayout></LinearLayout>
然后就是我们的Activity中的代码了。
public class ColorMatrix extends Activity { //待改变的ImageView private ImageView mImageView; //创建矩阵 private GridLayout mGridLayout; //更改后的图片是个bitmap对象 private Bitmap mBitmap; //矩阵的每个EditText的大小 private int mEtWidth,mExHeight; //装填这20个EditText private EditText [] mExs=new EditText[20]; //颜色矩阵的值,放在一个一维数组中 private float[] mColorMatrix=new float[20]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.colormatrix); //从资源中加载一个图片 mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.photo); //你懂的 mImageView=(ImageView) findViewById(R.id.imageView); mGridLayout=(GridLayout) findViewById(R.id.group); mImageView.setImageBitmap(mBitmap); //绘制完成后执行run()方法的内容 mGridLayout.post(new Runnable() { @Override public void run() { mEtWidth=mGridLayout.getWidth()/5; mExHeight=mGridLayout.getHeight()/4; 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 < 20; i++) { mColorMatrix[i]=Float.valueOf(mExs[i].getText().toString()); } } //设置图片的颜色矩阵 private void setImageMatrix(){ Bitmap bitmap=Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888); Canvas canvas=new Canvas(bitmap); android.graphics.ColorMatrix matrix=new android.graphics.ColorMatrix(); matrix.set(mColorMatrix); Paint paint=new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(matrix)); canvas.drawBitmap(mBitmap, 0, 0,paint); mImageView.setImageBitmap(bitmap); } //向GridLayout中添加EditText private void addEx(){ for (int i = 0; i < 20; i++) { EditText editText=new EditText(ColorMatrix.this); mExs[i]=editText; mGridLayout.addView(editText,mEtWidth,mExHeight); } } //你懂的。。。 private void initMatrix(){ for (int i = 0; i < 20; i++) { if (i%6==0) { mExs[i].setText(String.valueOf(1)); }else { mExs[i].setText(String.valueOf(0)); } } }}
注释很清楚,不说其他的了。
最后,Demo地址 :http://download.csdn.net/detail/nsgsbs/8532461
0 0
- Android图像处理技术(实现Android中的PS)(二)
- Android图像处理技术(实现Android中的PS)(一)
- Android图像处理技术(实现Android中的PS)(三)
- Android图像处理技术(实现Android中的PS)(四)
- Android图像处理技术(实现Android中的PS)(五)
- Android图像处理技术(实现Android中的PS)(六)
- Android图像处理(二) 画画板
- android图像处理(二)
- android中的图像处理
- Android中的图像处理
- photoshop图像处理技术(二)
- 怎样学习图像处理技术?(二)
- 【Android图像处理】毛玻璃(模糊)滤镜(效果)二
- 图像处理中的形态学(二)
- Android中的简单图像处理
- android图像处理(一)
- 技术转载:Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!! .
- Android绘图机制与处理技巧(二)——Android图像处理之色彩特效处理
- C语言小板凳(2)
- jQuery源码之动画详解二
- 函数注意事项
- 谷歌浏览器 Google Chrome v41.0.2272.101 便携增强版
- 100 hours anti boiled water film faced plywood
- Android图像处理技术(实现Android中的PS)(二)
- 第一章7至12题
- 【JAVA设计模式】--之策略模式
- UVA 10474 Where is the Marble?(
- C语言 写一个函数返回参数值二进制为1的个数
- 二维数组的应用之杨辉三角
- hadoop hdfs基础操作
- myeclipse无法找到JPEGImageEncoder问题
- Core Data使用(一)