//下面是主程序代码

import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.ImageView;public class PhotoShop extends Activity {private String tag = "IBMPhotoPhun";private Bitmap bitmapOrig = null;private Bitmap bitmapGray = null;private Bitmap bitmapWip = null;private ImageView ivDisplay = null;// NDK STUFFstatic {try{System.loadLibrary("haha");}catch(Exception e) {System.out.println("dd");}}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. */@Overridepublic 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 resourcesBitmapFactory.Options options = new BitmapFactory.Options();// Make sure it is 24 bit color as our image processing algorithm// expects this formatoptions.inPreferredConfig = Config.ARGB_8888;bitmapOrig = BitmapFactory.decodeResource(this.getResources(),R.drawable.sample, 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 happybitmapGray = 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 grayconvertToGray(bitmapOrig, bitmapGray);// find edges in the imagefindEdges(bitmapGray, bitmapWip);ivDisplay.setImageBitmap(bitmapWip);}public void onConvertToGray(View v) {Log.i(tag, "onConvertToGray");//创建一个和原图宽和高相等的灰色(一个像素是一个字节)图片,内容不确定或者为空(bitmapWip != null),就是说造好了格子只等填充数据bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(), Config.ALPHA_8);/** * java中图片的config(相当于格式) *  ALPHA_8     (2),        RGB_565     (4),        ARGB_4444   (5),        ARGB_8888   (6);        与android不同:        ANDROID_BITMAP_FORMAT_RGBA_8888:1ANDROID_BITMAP_FORMAT_A_8:8 */convertToGray(bitmapOrig, bitmapWip);ivDisplay.setImageBitmap(bitmapWip);}//先要获得bitmapWip才能处理,否则setImageBitmap(byll)虽然不会报错,但是空白的public void onDimmer(View v) {Log.i(tag, "onDimmer");changeBrightness(2, bitmapWip);ivDisplay.setImageBitmap(bitmapWip);}//先要获得bitmapWip才能处理,否则setImageBitmap(byll)虽然不会报错,但是空白的public void onBrighter(View v) {Log.i(tag, "onBrighter");changeBrightness(1, bitmapWip);ivDisplay.setImageBitmap(bitmapWip);}}
//下面是JNI层的代码
//====================================================
include <jni.h>#include <android/log.h>#include <android/bitmap.h>//【导入】#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 operationbitmapcolor:24位(实际是32)彩色图片;格式:ANDROID_BITMAP_FORMAT_RGBA_8888:bitmapgray灰色图片(8位);格式:ANDROID_BITMAP_FORMAT_A_8根据彩色图片填充灰色图片数据*/JNIEXPORT void JNICALL Java_com_msi_ibm_ndk_IBMPhotoPhun_convertToGray(JNIEnv* env, jobject obj, jobject bitmapcolor,jobject bitmapgray){AndroidBitmapInfoinfocolor;//AndroidBitmapInfo图片信息void*pixelscolor;//图片地址AndroidBitmapInfoinfogray;void*pixelsgray;intret;inty;intx;LOGI("convertToGray");//AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)得到图片信息//AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)得到图片地址if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {//【AndroidBitmap_getInfo】没有图片信息(宽高等等)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) {//不是24位图片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) {//不是8位图片LOGE("Bitmap format is not A_8 !");return;}if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {//【锁定AndroidBitmap_lockPixels】//锁定之后,pixelscolor指向图片的首地址 LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);}if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) {//锁定之后,pixelscolor指向图片的首地址LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);}// modify pixels with image processing algorithmfor (y=0;y<infocolor.height;y++) {//infocolor.height获得图片的高度//这是首次使用argbargb * line = (argb *) pixelscolor;//每一行的首地址(刚开始是图片的首地址,即第一行的首地址,下面会换行)uint8_t * grayline = (uint8_t *) pixelsgray;//每一行的首地址(数据类型为一个无符号字节)for (x=0;x<infocolor.width;x++) {grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;}pixelscolor = (char *)pixelscolor + infocolor.stride;//换行:每行的首地址 + 每行的跨度 = 下一行的首地址pixelsgray = (char *) pixelsgray + infogray.stride;//换行:每行的首地址 + 每行的跨度 = 下一行的首地址}LOGI("unlocking pixels");AndroidBitmap_unlockPixels(env, bitmapcolor);AndroidBitmap_unlockPixels(env, bitmapgray);}/**The AndroidBitmapInfo structure, defined in bitmap.h, is helpful forlearning about a Bitmap object.The AndroidBitmap_getInfo function, found in the jnigraphics library,obtains information about a specific Bitmap object.a char represents a signed 8-bit value, so a char pointer (char *) allows you to reference an 8-bitvalue and perform operations through that pointer. The image data is represented as uint8_t, which means an unsigned 8-bit value, where each byte holds a value ranging from 0 to 255. A collection of three 8-bit unsigned values represents a pixel of image data for a 24-bit image.图象数据使用unit8_t(8位无符号,0-255),24-bit的图象就是3个8比特无符号数。Working through an image involves working on the individual rows of data andmoving across the columns. The Bitmap structure contains a member known as thestride. The stride represents the width, in bytes, of a row of image data. Forexample, a 24-bit color plus alpha channel image has 32 bits, or 4 bytes, per pixel.So an image with a width of 320 pixels has a stride of 320*4 or 1,280 bytes. An 8-bitgrayscale image has 8 bits, or 1 byte, per pixel. A grayscale bitmap with a width of320 pixels has a stride of 320*1 or simply 320 bytes. With this information in mind,let's look at the image processing algorithm for converting a color image to agrayscale image:Bitmap结构有一个属性stride(跨度):表示一行数据的长度。例如,24比特的图象每个像素有包含32位数据(红绿蓝三种颜色,再加上alpha属性),所以宽度为320像素的图片的跨度(stride)为320*4字节。而rayscale image每个像素只有8比特,就是一个字节,如果320像素,那么stride为320字节。下面是原作者解释:1. When the image data is "locked," the base address of the image data isreferenced by a pointer named pixelscolor for the input color imageand pixelsgray for the output grayscale image.2. Two for-next loops allow you to iterate over the entire image.1. First, you iterate over the height of the image, one pass per "row."Use the infocolor.height value to get the count of the rows.2. On each pass through the rows a pointer is set up to the memorylocation corresponding to the first "column" of image data for therow.3. As you iterate over the columns for a particular row, you converteach pixel of color data to a single value representing thegrayscale value.4. When the complete row is converted you need to advance thepointers to the next row. This is done by jumping forward inmemory by the stride value.*//*changeBrightnessPixel Operation改变亮度:direction=1表示增加亮度,direction=2减少亮度bitmap只接受一个灰色图片(一个像素一个字节,格式ANDROID_BITMAP_FORMAT_A_8)将灰色图片的像素值改变*/JNIEXPORT void JNICALL Java_com_msi_ibm_ndk_IBMPhotoPhun_changeBrightness(JNIEnv* env, jobject obj, int direction,jobject bitmap){AndroidBitmapInfoinfogray;void*pixelsgray;intret;inty;intx;uint8_tsave;//好像没有用到//=======================if ((ret = AndroidBitmap_getInfo(env, bitmap, &infogray)) < 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;}if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixelsgray)) < 0) {LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);}// modify pixels with image processing algorithmLOGI("time to modify pixels....");for (y=0;y<infogray.height;y++) {//每个像素uint8_t * grayline = (uint8_t *) pixelsgray;int v;for (x=0;x<infogray.width;x++) {v = (int) grayline[x];if (direction == 1)//与上面不同,灰色图片每个像素是一个字节,所以只需要改变一个字节    v -=5;else    v += 5;if (v >= 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利用bitmapgray填充bitmapedges*/JNIEXPORT void JNICALL Java_com_msi_ibm_ndk_IBMPhotoPhun_findEdges(JNIEnv* env, jobject obj, jobject bitmapgray,jobject bitmapedges){AndroidBitmapInfoinfogray;void*    pixelsgray;AndroidBitmapInfoinfoedges;void*    pixelsedge;intret;inty;intx;intsumX,sumY,sum;inti,j;intGx[3][3];intGy[3][3];uint8_t *graydata;uint8_t *edgedata;LOGI("findEdges running");/**[  -1    0    1  ]Gx:[  -2 0    2   ][  -1    0    1  ][   1    2    1  ]Gy:[   0 0    0   ][  -1   -2   -1  ]*/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 algorithmLOGI("time to modify pixels....");graydata = (uint8_t *) pixelsgray;edgedata = (uint8_t *) pixelsedge;for (y=0;y<=infogray.height - 1;y++) {for (x=0;x<infogray.width -1;x++) {sumX = 0;sumY = 0;// check boundariesif (y==0 || y == infogray.height-1) {sum = 0;} else if (x == 0 || x == infogray.width -1) {sum = 0;} else {// calc X gradientfor (i=-1;i<=1;i++) {for (j=-1;j<=1;j++) {sumX += (int) ( (*(graydata + x + i + (y + j)* infogray.stride)) * Gx[i+1][j+1]);}}// calc Y gradientfor (i=-1;i<=1;i++) {for (j=-1;j<=1;j++) {sumY += (int) ( (*(graydata + x + i + (y + j)* infogray.stride)) * Gy[i+1][j+1]);}}sum = abs(sumX) + abs(sumY);}if (sum>255) sum = 255;if (sum<0) sum = 0;*(edgedata + x + y*infogray.width) = 255 - (uint8_t) sum;//给边界图片bitmapedges填充数据}}AndroidBitmap_unlockPixels(env, bitmapgray);AndroidBitmap_unlockPixels(env, bitmapedges);
}//结束


    
  

  



更多相关文章

  1. Android(安卓)图片旋转(使用Matrix.setRotate方法)
  2. Android根据电量变化为不同图片的方法【电池电量提示】
  3. Android日常应用记录
  4. 实现调用Android手机的拍照功能
  5. android 网络图片查看器
  6. Android(安卓)volley框架加载网络图片
  7. android 处理图片工具
  8. Android之关于手势操作图片的缩放与移动
  9. Android(安卓)AES 文件加密解密

随机推荐

  1. 正则表达式匹配wordpress类似的短代码,用
  2. 在php项目中, mysql视图常用吗?
  3. mysql_fetch_array返回一个数组,数字为“1
  4. 如何创建自己的wiki-Dokuwiki
  5. 如何在php数组中插入新的键值对?
  6. 如何使用PHP在表单中找到移动复制
  7. Wordpress查询另一个wordpress数据库for
  8. PHP 10个常见面试题及答案
  9. PHP写的在线计算器,欢迎指正
  10. PHP结合memcacheq消息队列解决并发问题