Android Jni用bitmap形式实现Image图片的黑白滤镜

在Android的开发中,我们有时会对性能要求比较高。Android通过NDK为我们提供了c++开发的方式。我们可以通过c++完成核心的耗时的计算然后通过JNI的方式将处理完成的数据传给Java层。
今天,我们就用jni的方式对Bitmap进行处理,来实践NDK开发的方式。开发一个图片滤镜。
效果图:
在这里插入图片描述
1.CMakeLists.txt 配置

target_link_libraries( # Specifies the target library.
                       native-lib 
                       jnigraphics//android提供的图像处理库
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

2.代码部分

Activity 部分:

 private void initview(){
        source_img = (ImageView)findViewById(R.id.source_img);
        operation_img = (ImageView)findViewById(R.id.operation_img);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.newpic);
//        operation_img.setImageBitmap(rotateBitmap(bitmap));
        operation_img.setImageBitmap(handleBitmap(bitmap));
    }
    /**
     * 处理图片,此方法中会调用nativeProcessBitmap
     * @param bitmap
     * @return
     */
    private Bitmap handleBitmap(Bitmap bitmap) {
        Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        nativeProcessBitmap(bmp);
        return bmp;
    }
        /**
     * 黑白特效
     */
    public native void nativeProcessBitmap(Bitmap bitmap);

2.jni 部分:

#include <jni.h>
#include <string>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include "AndroidLog.h"
#include <android/bitmap.h>
#define MAKE_RGB565(r,g,b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
#define MAKE_ARGB(a,r,g,b) ((a&0xff)<<24) | ((r&0xff)<<16) | ((g&0xff)<<8) | (b&0xff)

#define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)
#define RGB565_G(p) ((((p) & 0x7E0 ) >> 5)  << 2)
#define RGB565_B(p) ( ((p) & 0x1F  )        << 3)

#define RGB8888_A(p) (p & (0xff<<24) >> 24 )
#define RGB8888_R(p) (p & (0xff<<16) >> 16 )
#define RGB8888_G(p) (p & (0xff<<8)  >> 8 )
#define RGB8888_B(p) (p & (0xff) )

#define RGBA_A(p) (((p) & 0xFF000000) >> 24)
#define RGBA_R(p) (((p) & 0x00FF0000) >> 16)
#define RGBA_G(p) (((p) & 0x0000FF00) >>  8)
#define RGBA_B(p)  ((p) & 0x000000FF)
#define MAKE_RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))

extern "C" JNIEXPORT void JNICALL
Java_com_xinrui_ndkapp_MainActivity_nativeProcessBitmap(JNIEnv *env,
                                                                        jobject instance,
                                                                        jobject bitmap) {

    if (bitmap == NULL) {
        LOGE("bitmap is null\n");
        return;
    }

    AndroidBitmapInfo bitmapInfo;
    memset(&bitmapInfo , 0 , sizeof(bitmapInfo));
    // Need add "jnigraphics" into target_link_libraries in CMakeLists.txt
    AndroidBitmap_getInfo(env , bitmap , &bitmapInfo);

    // Lock the bitmap to get the buffer
    void * pixels = NULL;
    int res = AndroidBitmap_lockPixels(env, bitmap, &pixels);

    // From top to bottom
    int x = 0, y = 0;
    for (y = 0; y < bitmapInfo.height; ++y) {
        // From left to right
        for (x = 0; x < bitmapInfo.width; ++x) {
            int a = 0, r = 0, g = 0, b = 0;
            void *pixel = NULL;
            // Get each pixel by format

            if(bitmapInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
            {
                pixel = ((uint32_t *)pixels) + y * bitmapInfo.width + x;
                int r,g,b;
                uint32_t v = *((uint32_t *)pixel);
                r = RGB8888_R(v);
                g = RGB8888_G(v);
                b = RGB8888_B(v);
                int sum = r+g+b;
                *((uint32_t *)pixel) = MAKE_ARGB(0xff , sum/3, sum/3, sum/3);
            }
            else if (bitmapInfo.format == ANDROID_BITMAP_FORMAT_RGB_565) {
                pixel = ((uint16_t *)pixels) + y * bitmapInfo.width + x;
                int r,g,b;
                uint16_t v = *((uint16_t *)pixel);
                r = RGB565_R(v);
                g = RGB565_G(v);
                b = RGB565_B(v);
                int sum = r+g+b;
                *((uint16_t *)pixel) = MAKE_RGB565(sum/3, sum/3, sum/3);  }
        }
    }
    AndroidBitmap_unlockPixels(env, bitmap);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值