博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jni 写一个简单的photoshop
阅读量:6263 次
发布时间:2019-06-22

本文共 8261 字,大约阅读时间需要 27 分钟。

第一步创建 ui

 

 

第二步,写activity   MainActivity

 

import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.graphics.BitmapFactory;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.view.View;import android.widget.ImageView;public class IBMPhotoPhun extends Activity {    private String tag = "IBMPhotoPhun";    private Bitmap bitmapOrig = null;    private Bitmap bitmapGray = null;    private Bitmap bitmapWip = null;    private ImageView ivDisplay = null;                // NDK STUFF    static {        System.loadLibrary("ibmphotophun");    }    public native void convertToGray(Bitmap bitmapIn,Bitmap bitmapOut);    public native void changeBrightness(int direction,Bitmap bitmap);    public native void findEdges(Bitmap bitmapIn,Bitmap bitmapOut);    // END NDK STUFF                /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Log.i(tag,"before image stuff");        ivDisplay = (ImageView) findViewById(R.id.ivDisplay);                    // load bitmap from resources        BitmapFactory.Options options = new BitmapFactory.Options();        // Make sure it is 24 bit color as our image processing algorithm 		// expects this format        options.inPreferredConfig = Config.ARGB_8888;        bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sampleimage,options);        if (bitmapOrig != null)            ivDisplay.setImageBitmap(bitmapOrig);          }     public void onResetImage(View v) {        Log.i(tag,"onResetImage");        ivDisplay.setImageBitmap(bitmapOrig);            }     public void onFindEdges(View v) {        Log.i(tag,"onFindEdges");        // make sure our target bitmaps are happy        bitmapGray = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);        bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);        // before finding edges, we need to convert this image to gray        convertToGray(bitmapOrig,bitmapGray);        // find edges in the image        findEdges(bitmapGray,bitmapWip);        ivDisplay.setImageBitmap(bitmapWip);            }    public void onConvertToGray(View v) {        Log.i(tag,"onConvertToGray");         bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);        convertToGray(bitmapOrig,bitmapWip);        ivDisplay.setImageBitmap(bitmapWip);    }        public void onDimmer(View v) {        Log.i(tag,"onDimmer");                changeBrightness(2,bitmapWip);        ivDisplay.setImageBitmap(bitmapWip);    }    public void onBrighter(View v) {        Log.i(tag,"onBrighter");          changeBrightness(1,bitmapWip);        ivDisplay.setImageBitmap(bitmapWip);    }   }

 

 

Android.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := ibmphotophunLOCAL_SRC_FILES := ibmphotophun.cLOCAL_LDLIBS    := -llog -ljnigraphicsinclude $(BUILD_SHARED_LIBRARY)

 

增加宏

/* * ibmphotophun.c *  * Author: Frank Ableson * Contact Info: fableson@msiservices.com */#include 
#include
#include
#define LOG_TAG "libibmphotophun"#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)typedef struct { uint8_t alpha; uint8_t red; uint8_t green; uint8_t blue;} argb;/*convertToGrayPixel operation*/JNIEXPORT void JNICALL Java_com_msi_ibm_ndk_IBMPhotoPhun_convertToGray(JNIEnv * env, jobject obj, jobject bitmapcolor,jobject bitmapgray){ AndroidBitmapInfo infocolor; void* pixelscolor; AndroidBitmapInfo infogray; void* pixelsgray; int ret; int y; int x; LOGI("convertToGray"); if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return; } if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return; } LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags); if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { LOGE("Bitmap format is not RGBA_8888 !"); return; } LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags); if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) { LOGE("Bitmap format is not A_8 !"); return; } if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); } if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); } // modify pixels with image processing algorithm for (y=0;y
= 255) { grayline[x] = 255; } else if (v <= 0) { grayline[x] = 0; } else { grayline[x] = (uint8_t) v; } } pixelsgray = (char *) pixelsgray + infogray.stride; } AndroidBitmap_unlockPixels(env, bitmap); }/*findEdgesMatrix operation*/JNIEXPORT void JNICALL Java_com_msi_ibm_ndk_IBMPhotoPhun_findEdges(JNIEnv * env, jobject obj, jobject bitmapgray,jobject bitmapedges){ AndroidBitmapInfo infogray; void* pixelsgray; AndroidBitmapInfo infoedges; void* pixelsedge; int ret; int y; int x; int sumX,sumY,sum; int i,j; int Gx[3][3]; int Gy[3][3]; uint8_t *graydata; uint8_t *edgedata; LOGI("findEdges running"); Gx[0][0] = -1;Gx[0][1] = 0;Gx[0][2] = 1; Gx[1][0] = -2;Gx[1][1] = 0;Gx[1][2] = 2; Gx[2][0] = -1;Gx[2][1] = 0;Gx[2][2] = 1; Gy[0][0] = 1;Gy[0][1] = 2;Gy[0][2] = 1; Gy[1][0] = 0;Gy[1][1] = 0;Gy[1][2] = 0; Gy[2][0] = -1;Gy[2][1] = -2;Gy[2][2] = -1; if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return; } if ((ret = AndroidBitmap_getInfo(env, bitmapedges, &infoedges)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return; } LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is%d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags); if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) { LOGE("Bitmap format is not A_8 !"); return; } LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is%d",infoedges.width,infoedges.height,infoedges.stride,infoedges.format,infoedges.flags); if (infoedges.format != ANDROID_BITMAP_FORMAT_A_8) { LOGE("Bitmap format is not A_8 !"); return; } if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); } if ((ret = AndroidBitmap_lockPixels(env, bitmapedges, &pixelsedge)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); } // modify pixels with image processing algorithm LOGI("time to modify pixels...."); graydata = (uint8_t *) pixelsgray; edgedata = (uint8_t *) pixelsedge; for (y=0;y<=infogray.height - 1;y++) { for (x=0;x
255) sum = 255; if (sum<0) sum = 0; *(edgedata + x + y*infogray.width) = 255 - (uint8_t) sum; } } AndroidBitmap_unlockPixels(env, bitmapgray); AndroidBitmap_unlockPixels(env, bitmapedges); }

 

转载地址:http://udqsa.baihongyu.com/

你可能感兴趣的文章
个人博客四|注册登录退出功能后台开发
查看>>
工作中常用到的ES6语法
查看>>
Django-Signals信号量
查看>>
flac格式转换mp3格式要用什么软件
查看>>
19. Remove Nth Node From End of List
查看>>
最佳在线图表软件
查看>>
Work with Alexa : 智能设备连接到Alexa
查看>>
[sublime系列文章] sublime text 3构建系统
查看>>
995. Minimum Number of K Consecutive Bit Flips
查看>>
for-loop 与 json.Unmarshal 性能分析概要
查看>>
C++中new的三种使用方法说明
查看>>
爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
查看>>
Python中_new_方法详解及使用
查看>>
flutter安装开发环境-问题记录
查看>>
mp4文件如何转换为webm格式
查看>>
如何在线创建数据流图(DFD)?
查看>>
腾讯—最新iOS面试题总结
查看>>
CGI,FASTCGI,PHP-CGI,PHP-FPM 概念
查看>>
DApp引荐机制正式上线 | IOST开发者赏金计划
查看>>
【剑指offer】9.二进制中1的个数
查看>>