你好,游客 登录 注册 搜索
背景:
阅读新闻

Android高级教程:使用NDK把彩图转换灰度图

[日期:2014-11-01] 来源:Linux社区  作者:hellogv [字体:大 中 小]

封装NDK函数的JAVA类LibFuns.java的源码如下:

package com.testToGray;
public class LibFuns {
 static {
        System.loadLibrary("ImgToGray");
    }
  /**
    * @param width the current view width
    * @param height the current view height
    */
   
    public static native int[] ImgToGray(int[] buf, int w, int h);
}

彩图转换为灰度图的ImgToGray.cpp源码:

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
extern "C" {
JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray(
  JNIEnv* env, jobject obj, jintArray buf, int w, int h);
}
;
JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray(
  JNIEnv* env, jobject obj, jintArray buf, int w, int h) {
 jint *cbuf;
 cbuf = env->GetIntArrayElements(buf, false);
 if (cbuf == NULL) {
  return 0; /* exception occurred */
 }
 int alpha = 0xFF << 24;
 for (int i = 0; i < h; i++) {
  for (int j = 0; j < w; j++) {
   // 获得像素的颜色
   int color = cbuf[w * i + j];
   int red = ((color & 0x00FF0000) >> 16);
   int green = ((color & 0x0000FF00) >> 8);
   int blue = color & 0x000000FF;
   color = (red + green + blue) / 3;
   color = alpha | (color << 16) | (color << 8) | color;
   cbuf[w * i + j] = color;
  }
 }
 int size=w * h;
 jintArray result = env->NewIntArray(size);
 env->SetIntArrayRegion(result, 0, size, cbuf);
 env->ReleaseIntArrayElements(buf, cbuf, 0);
 return result;
}

Android.mk的源码:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := ImgToGray
LOCAL_SRC_FILES := ImgToGray.cpp
include $(BUILD_SHARED_LIBRARY)

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/108815.htm

linux
相关资讯       Android NDK  灰度图 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款