栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 滤镜效果不同实现方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 滤镜效果不同实现方式

1.使用 Android 原生画笔 colorMatrix 灰度图片
fun greyBitmap(originBitmap: Bitmap): Bitmap {
        val newBitmap =
            Bitmap.createBitmap(originBitmap.width, originBitmap.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(newBitmap)
        val colorMatrix = ColorMatrix()
        colorMatrix.setSaturation(0f)
        val colorFilter = ColorMatrixColorFilter(colorMatrix)
        val paint = Paint().apply {
            isAntiAlias = true
            setColorFilter(colorFilter)
        }
        canvas.drawBitmap(originBitmap, 0f, 0f, paint)
        return newBitmap
    }
2. 提取bitmap中的像素,对每一个像素分别操作
fun greyByPixel(originBitmap: Bitmap): Bitmap {
        val dstBitmap =
            Bitmap.createBitmap(originBitmap.width, originBitmap.height, Bitmap.Config.ARGB_8888)

        val pixels = IntArray(originBitmap.width * originBitmap.height)
        originBitmap.getPixels(
            pixels,
            0,
            originBitmap.width,
            0,
            0,
            originBitmap.width,
            originBitmap.height
        )
        for (i in pixels.indices) {
            var a = pixels[i].shr(24).and(0xff)
            var r = pixels[i].shr(16).and(0xff)
            var g = pixels[i].shr(8).and(0xff)
            var b = pixels[i].and(0xff)

            //  final float R = 0.213f * invSat;
            //        final float G = 0.715f * invSat;
            //        final float B = 0.072f * invSat;
            var grey = 0.213f * r + 0.715f * g + 0.072f * b

            pixels[i] = a.shl(24).or(grey.toInt().shl(16)).or(grey.toInt().shl(8)).or(grey.toInt())
        }

        dstBitmap.setPixels(
            pixels,
            0,
            originBitmap.width,
            0,
            0,
            originBitmap.width,
            originBitmap.height
        )
        return dstBitmap


    }
3. 在 Native 层 操作 Pixels
JNIEXPORT jint JNICALL
Java_com_cailei_opencvndk_MainActivity_greyByOpenCV(JNIEnv *env, jobject thiz,
                                                    jobject origin_bitmap) {
    AndroidBitmapInfo bitmapInfo;
    // 获取 bitmap 的信息
    int info = AndroidBitmap_getInfo(env, origin_bitmap, &bitmapInfo);
    if (info != 0) {
        return -1;
    }
    
    void *pixel;
    // 拿到 bitmap 的像素
    AndroidBitmap_lockPixels(env, origin_bitmap, &pixel);

    for (int i = 0; i < bitmapInfo.width * bitmapInfo.height; i++) {
        uint32_t *pixel_p = reinterpret_cast(pixel) + i;
        uint32_t pixel = *pixel_p;
        int a = (pixel >> 24) & 0xff;
        int r = (pixel >> 16) & 0xff;
        int g = (pixel >> 8) & 0xff;
        int b = (pixel) & 0xff;

        int grey = (int) (0.231f * r + 0.715f * g + 0.072f * b);
        *pixel_p = (a << 24) | (grey << 16) | (grey << 8) | grey;
    }
    AndroidBitmap_unlockPixels(env, origin_bitmap);
    return 1;
}

注意:需要添加链接 jnigraphics 这个so才能使用andorid/bitmap.h

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号