【Android】图像像素点理解

    科技2022-08-12  107

    学而不思则罔,思而不学则殆

    【Android】图像像素点理解

    前言图像处理 -ARGB初始原图AlphaR值测试G值测试B值测试非R值测试非G值测试非B值测试 高级图片处理取反1取反2灰阶处理 Demo


    前言

    我们知道图像是有像素点组成,那么什么是像素点呢?

    查看一张图片的属性,该图片的宽度是690像素,高度是460像素。 每一个像素点是一个数字,表示颜色的具体值。 像素值如下: 比如【#AA03DAC5】

    像素说明举例A透明度AARR值03GG值DABB值C5

    图像处理 -ARGB初始

    原图

    Bitmap ycy = BitmapFactory.decodeResource(getResources(), R.drawable.ycy);

    设置给ImageView,展示如下: 图像处理代码,主要把像素点的值根据传入的flag做转换处理。

    private Bitmap pictureProcessing(Bitmap bitmap, int flag) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Log.d("zhangyu", "width:" + width + " height:" + height);//690 460 Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { int c = bitmap.getPixel(w, h); newBitmap.setPixel(w, h, c & flag); //处理像素点 } } show(newBitmap); return newBitmap; }

    Alpha

    只保留透明度

    Bitmap bitmap = pictureProcessing(ycy, 0XFF00_0000);

    R值测试

    保留透明度和R值

    Bitmap bitmap = pictureProcessing(ycy, 0XFFFF_000);

    G值测试

    保留透明度和G值

    Bitmap bitmap = pictureProcessing(ycy, 0XFF00_FF00);

    B值测试

    保留透明度和B值

    Bitmap bitmap = pictureProcessing(ycy, 0XFF00_00FF);

    非R值测试

    只清除R值

    Bitmap bitmap = pictureProcessing(ycy, 0XFF00_FFFF);

    非G值测试

    Bitmap bitmap = pictureProcessing(ycy, 0XFFFF_00FF);

    非B值测试

    Bitmap bitmap = pictureProcessing(ycy, 0XFFFF_FF00);

    高级图片处理

    取反1

    //图片取反1 private Bitmap negation1(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { int c = bitmap.getPixel(w, h); int a = c & 0xff00_0000 >> 24; int r = c & 0x00ff_0000 >> 16; int g = c & 0x0000_ff00 >> 8; int b = c & 0x0000_00ff; newBitmap.setPixel(w, h, Color.argb(255 - a, 255 - r, 255 - g, 255 - b)); } } return newBitmap; }

    取反2

    //图片取反2 private Bitmap negation2(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { int c = bitmap.getPixel(w, h); int newC = ~c; newC = newC | 0xff00_0000; newBitmap.setPixel(w, h, newC); } } return newBitmap; }

    灰阶处理

    //灰阶处理1 public static Bitmap grayLevelOne(Bitmap originBitmap) { //【图片灰阶处理】 //生成新的Bitmap Bitmap grayBitmap = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(), Bitmap.Config.ARGB_8888); //创建画布 Canvas canvas = new Canvas(grayBitmap); Paint mPaint = new Paint(); //创建颜色变换矩阵 ColorMatrix colorMatrix = new ColorMatrix(); //设置饱和度为0,实现灰阶效果 colorMatrix.setSaturation(0); //创建颜色过滤矩阵 ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); //设置画笔的颜色过滤矩阵 mPaint.setColorFilter(colorFilter); //使用处理后的画笔绘制图像 canvas.drawBitmap(originBitmap, 0, 0, mPaint); return grayBitmap; }

    灰阶处理应用见:【Android】应用黑白显示 Android图像处理见ColorMatrix,这个类主要是处理像素点的,通过一个长度为20的数组表示矩阵,用来处理像素点运算。

    Demo

    https://github.com/aJanefish/BitmapDemo

    Processed: 0.011, SQL: 8