OpenCV-Android 滤镜效果处理

本文介绍了如何在Java和NativeC++中使用ColorMatrix对Bitmap进行颜色处理,包括灰度、底片效果,以及ARGB_8888和RGB_565格式的区别。同时提到提升编程技能和职业规划的重要性。
摘要由CSDN通过智能技术生成

1.ColorMatrix 矩阵运算

色彩:针对每个像素做处理

/**
     * ColorMatrix 操作像素
     * @param src
     * @return
     */
    public static Bitmap gray(Bitmap src) {

        Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        Canvas canvas = new Canvas(dst);
        Paint paint = new Paint();
        paint.setDither(true);
        paint.setAntiAlias(true);
//        final float R = 0.213f * invSat;
//        final float G = 0.715f * invSat;
//        final float B = 0.072f * invSat;


        // 4 * 5 的矩阵 同setSaturation同样效果
//        ColorMatrix colorMatrix = new ColorMatrix(new float[]{
//                0.213f, 0.715f,0.072f, 0, 0,
//                0.213f, 0.715f,0.072f, 0, 0,
//                0.213f, 0.715f,0.072f, 0, 0,
//                0, 0, 0, 1, 0
//        });

        //原图效果
//        ColorMatrix colorMatrix = new ColorMatrix(new float[]{
//                1, 0, 0, 0, 0,
//                0, 1, 0, 0, 0,
//                0, 0, 1, 0, 0,
//                0, 0, 0, 1, 0
//        });

        //底片效果
        ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                -1, 0, 0, 0, 255,
                0, -1, 0, 0, 255,
                0, 0, -1, 0, 255,
                0, 0, 0, 1, 0
        });
        // 灰度效果
//        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
        paint.setColorFilter(colorMatrixColorFilter);

        canvas.drawBitmap(src, 0, 0, paint);
        return dst;
    }

2.Bitmap 获取像素操作

/**
     * Bitmap操作像素
     * @param src
     * @return
     */
    public static Bitmap gray2(Bitmap src) {
        Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        int[] pixels = new int[src.getWidth() * src.getHeight()];
        src.getPixels(pixels, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());

        for (int i = 0; i < pixels.length; i++) {
            int p = pixels[i];

            int a = (p >> 24) & 0xff;
            int r = (p >> 16) & 0xff;
            int g = (p >> 8) & 0xff;
            int b = p & 0xff;

            int gray = (int) (0.213f * r + 0.715f * g + 0.072f * b);

            pixels[i] = a << 24 | gray << 16 | gray << 8 | gray;
        }

        dst.setPixels(pixels, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());

        return dst;
    }

3.Native 层操作像素指针

#include <jni.h>
#include <string>
#include <android/bitmap.h>

extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapplication_BitmapUtils_gray3(JNIEnv *env, jclass clazz, jobject bitmap) {
    AndroidBitmapInfo bitmapInfo;
    int result = AndroidBitmap_getInfo(env, bitmap, &bitmapInfo);
    if (result != 0) {
        return;
    }
    void *pixels;
    AndroidBitmap_lockPixels(env, bitmap, &pixels);

    for (int i = 0; i < bitmapInfo.width * bitmapInfo.height; ++i) {
        uint32_t *pixel_p = reinterpret_cast<uint32_t *>(pixels) + i;
        uint32_t p = *pixel_p;

        int a = (p >> 24) & 0xff;
        int r = (p >> 16) & 0xff;
        int g = (p >> 8) & 0xff;
        int b = p & 0xff;

        int gray = (int) (0.213f * r + 0.715f * g + 0.072f * b);

        *pixel_p = a << 24 | gray << 16 | gray << 8 | gray;
    }

    AndroidBitmap_unlockPixels(env, bitmap);
}

使用的时候可能报错

需要在CMakeLists.txt 文件中target_link_libraries下添加jnigraphics
例如

4.ARGB_8888 和 RGB_565

ARGB_8888 的大小是RGB_565的两倍
RGB_565 没有alpha通道rgb各占5、6、5位一个色素占两字节
ARGB_8888 一个色素占四字节

最后

如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。

如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
img
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。

欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓(文末还有ChatGPT机器人小福利哦,大家千万不要错过)

PS:群里还设有ChatGPT机器人,可以解答大家在工作上或者是技术上的问题
图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值