正文

图像处理(1)

Android多媒体开发高级编程 作者:(美)艾佛瑞


类似于在Canvas对象上绘制时使用Matrix对象的方法,也可以使用一个ColorMatrix对象来改变用于在Canvas对象上绘制的Paint对象。

同样,ColorMatrix以类似的方式工作。它是一个数字数组,可以对图像的像素进行操作。然而不同于操作x、y和z坐标,它操作颜色值——每个像素的Red(红)、Green(绿)、Blue(蓝)和Alpha值。

通过调用其没有任何参数的构造函数,我们可以构建一个默认的ColorMatrix对象。

ColorMatrix cm = new ColorMatrix();

使用ColorMatrix对象构建一个ColorMatrixColorFilter对象,并将它应用于Paint对象,从而使得这个ColorMatrix对象可用来改变Canvas对象上的绘制内容。

paint.setColorFilter(new ColorMatrixColorFilter(cm));

为了能够对ColorMatrix对象进行实验,可以简单地将它插入到“选择图片”示例中的绘图部分。

Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().

openInputStream(imageFileUri), null, bmpFactoryOptions);

Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),

bmp.getHeight(),bmp.getConfig());

Canvas canvas = new Canvas(alteredBitmap);

Paint paint = new Paint();

ColorMatrix cm = new ColorMatrix();

paint.setColorFilter(new ColorMatrixColorFilter(cm));

Matrix matrix = new Matrix();

canvas.drawBitmap(bmp, matrix, paint);

alteredImageView.setImageBitmap(alteredBitmap);

chosenImageView.setImageBitmap(bmp);


上一章目录下一章

Copyright © 读书网 www.dushu.com 2005-2020, All Rights Reserved.
鄂ICP备15019699号 鄂公网安备 42010302001612号