Android Bitmap图像效果处理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Android中图像的效果的处理无非是Matrix和ColorMatrix,以及PortialXformed,其中Matrix使用在旋转,镜像方面,而ColorMatrix用于跳转色相,最后一种用于合成


1色相涉及难度较高,这里主要讲解ColorMatrix色相
Bitmap bitmap = Bitmap.createBitmap(srcBmp.getWith(),srcBmp.getHeight(),Bitmap.Config.ARGB_8888);
Canvas  canvas = new Bitmap(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);


//设置色相
ColorMatrix hueMatrix = new ColorMatrix();
hueMatrix.setRotation(0,20);//0表示 红色,参数二 表示色度
hueMatrix.setRotation(1,20);//1表示 绿色,参数二 表示色度
hueMatrix.setRotation(2,20);//2表示 蓝色,参数二 表示色度

//饱和度
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix .setSaturation(40f);

//亮度
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(50,50,50,1);//参数:红,绿,蓝,透明度

//融合以上三种
ColorMatrix imgMatrix = new ColorMatrix();
imgMatrix.postConcat(hueMatrix);
imgMatrix.postConcat(saturationMatrix );
imgMatrix.postConcat(lumMatrix);

//设置到画笔
paint.setColorFilter(new ColorMatrixColorFilter(imgMatrix));

canvas.drawBitmap(bitmap ,0,0,paint);
2对于ColorMatrix也可以使用矩阵进行处理
{1,0,0,0,0}   //红     {R}   
{0,1,0,0,0}   //绿   乘 {G}  
{0,0,1,0,0}   //蓝      {B}
{0,0,0,1,0}   //alpha   {A}

其实是矩阵的乘法

ColorMatrix imgMatrix = new ColorMatrix();
imgMatrix.set(int[4][5] cm);
3使用像素点,获取特定像素点所占面积
int[] pixelSize = new int[bmp.getWidth()*bmp.getHeight()];
bmp.getPixels(pixelSize,0, bmp.getWidth(),0,0,bmp.getWidth(),bmp.getHeight());

//对于刮刮卡计算刮开涂层的面积可以使用 
int alphaSize = 0;
int totalSize  = bmp.getWidth()*bmp.getHeight();

 for(int i=0;i60)
{
}
4.获取像素点三元素数据,修改像素数据

(实际开发中某些Bitmap不能修改像素点,不能调用setPixels,请参考5解决)

//定义目标像素点
int[] pixelSize = new int[bmp.getWidth()*bmp.getHeight()];
bmp.getPixels(pixelSize,0, bmp.getWidth(),0,0,bmp.getWidth(),bmp.getHeight());
int[] dstPixelSize = new int[bmp.getWidth()*bmp.getHeight()];

 for(int i=0;i
5.将immutable Bitmap转为mutable Bitmap

实际开发中,有些Bitmap不能修改像素点,因此需要转化

mBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888);
	Canvas canvas = new Canvas(mBitmap);
	canvas.drawBitmap(bmp, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG)); //转为可修改
		
	int[] pixelSize = new int[mBitmap.getWidth()*mBitmap.getHeight()];
	int[] dstPixelSize = new int[mBitmap.getWidth()*mBitmap.getHeight()];
		 
	mBitmap.getPixels(pixelSize,0, mBitmap.getWidth(),0,0,mBitmap.getWidth(),mBitmap.getHeight());
		
	for(int i=0;i=225)
			{
			    	alpha = 255;
			    	dstPixelSize[i] = Color.TRANSPARENT;
			}else{
			        r = 255-r;
				g = 255-g;
				b = 255-b;
				r = Math.min(255,Math.max(0,r));//保证范围在0-255
				g = Math.min(255,Math.max(0,g));
				b = Math.min(255,Math.max(0,b));
			    dstPixelSize[i] = Color.argb(alpha,r,g,b);
			}
    }
		 mBitmap.setPixels(dstPixelSize,0, mBitmap.getWidth(),0,0,mBitmap.getWidth(),mBitmap.getHeight());
		 setImageBitmap(mBitmap);
		 
		 bmp.recycle();
		 

Android Bitmap图像效果处理_第1张图片Android Bitmap图像效果处理_第2张图片

转载于:https://my.oschina.net/ososchina/blog/386881

你可能感兴趣的:(Android Bitmap图像效果处理)