Learn more about JNI---image grayscale processing on the Android platform

Source: Internet
Author: User

I. Knowledge of JNI programming involvedAn array of Java primitives, all of which are Jarray type formats in JNI. The specific types are as follows: TypeDef jarray JBOOLEANARRAY;TYPEDEF Jarray jbytearray;typedef jarray jchararray;typedef jarray JshortArray; typedef jarray JINTARRAY;TYPEDEF Jarray jlongarray;typedef jarray jfloatarray;typedef jarray jdoubleArray;typedef Jarray Jobjectarray;jarray type JNI defines the type, through which the Java array can be manipulated jnienv, but it is not an array of C + +, so we want to convert the Jarray type to an array in C + +. JNIEnv defines a series of methods to convert a Jarray type to a C + + array, and to convert a C + + array into a jArray1, Java primitive Type array to the corresponding C array type       jboolean*    (*getbooleanarrayelements) (jnienv*, Jbooleanarray, jboolean*);     jbyte *       (*getbytearrayelements) (jnienv*, Jbytearray, jboolean*);     jchar*       (*getchararrayelements) (jnienv*, Jchararray, jboolean*);     jshort*      (*getshortarrayelements) (jnienv*, Jshortarray, jboolean*);     jint*        (*getintarrayelements) (jnienv*, JiNtarray, jboolean*)     jlong*       (*getlongarrayelements) (jnienv* , Jlongarray, jboolean*)     jfloat*      (*getfloatarrayelements) (jnienv* , Jfloatarray, jboolean*);     jdouble*     (*getdoublearrayelements) (JNIEnv*, Jdoublearray, jboolean*); parameter: ENV:JNI interface pointer.        array:java the String object.        iscopy: A pointer to a Boolean value.         return Value:         Returns a pointer to an array element, or if the operation fails; Null. 2. Get the length of the array:      jsize        (*getarraylength) (jnienv*, Jarray); 3. Release the array memory of C + +      void         (* releasebooleanarrayelements) (jnienv*, jbooleanarray,jboolean*, jint);       void         (*releasebytearrayelemenTS) (jnienv*, jbytearray,jbyte*, jint);      void          (*releasechararrayelements) (jnienv*, jchararray,jchar*, jint);      void          (*releaseshortarrayelements) (jnienv*, jshortarray,jshort*, Jint);      void         (*releaseintarrayelements) (jnienv*, jintarray,jint*, Jint)      void         (* releaselongarrayelements) (jnienv*, jlongarray,jlong*, jint);      void          (*releasefloatarrayelements) (jnienv*, jfloatarray,jfloat*, Jint);      void         (*releasedoublearrayelements) (jnienv*, Jdoublearray, jdouble*, jint); parameter: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENV:JNI interface pointer. The        array:java array object.       elems: A pointer to an array element.        mode: Release mode. 4. Constructs an array of Java primitives of a specified length      jbooleanarray (*newbooleanarray) (jnienv*, jsize);      jbytearray     (*newbytearray) (jnienv*, jsize);      jchararray     (*newchararray) (jnienv*, jsize);      jshortarray    (*newshortarray) (jnienv*, jsize);      jintarray      (* Newintarray) (jnienv*, jsize);      jlongarray     (*newlongarray) (JNIEnv*, jsize)      jfloatarray    (*newfloatarray) (jnienv*, jsize);      jdoublearray   (*newdoublearray) (jnienv*, jsize);  5, assigning values to the Java primitive Type array       void   (*setbooleanarrayregion) (jnienv*, Jbooleanarray,jsize, jsize, const jboolean*);    & nbSp; void   (*setbytearrayregion) (jnienv*, Jbytearray,jsize, jsize, const jbyte*);      void   (*setchararrayregion) (jnienv*, Jchararray,jsize, jsize, const jchar*);      void   (*setshortarrayregion) (jnienv*, Jshortarray,jsize, jsize, const jshort*);      void   (*setintarrayregion) (jnienv*, Jintarray,jsize, jsize, const jint*);      void   (*setlongarrayregion) (jnienv*, Jlongarray,jsize, jsize, const jlong*);      void   (*setfloatarrayregion) (jnienv*, Jfloatarray,jsize, jsize, const jfloat*);      void   (*setdoublearrayregion) (jnienv*, Jdoublearray, Jsize, jsize, const jdouble*);  The elements of a specified range in an array of Java primitive types c/c+ + An array of elements to assign parameters to:        env:jni interface pointers.        array:  java Array.        start: Starting subscript.   &Nbsp;    len: The number of elements to copy.        buf: Source buffer.         Thrown:         ArrayIndexOutOfBoundsException: If one of the subscript in the zone is invalid. Note: If it is a C program, use (*env)->  if it is C + + to use env-> under Linux if the. c file with "env->" compilation will not find this structure, you must use "(*env)," or change to a. cpp file to C + + 's way to compile. See the JNI.H code for details. There are also some omitted content that can be referenced in the JNI documentation: Java Native Interface 6.0 specification, which can be found in the JDK documentation. If you want to do more in-depth JNI programming, you need to read this document carefully   Second, image grayscale A well-known psychological formula for color-to-grayscale: Gray = r*0.299 + g*0.587 + b*0.114 in practice to avoid floating-point operations, then there is a shift operation instead. 2 to 20-bit accuracy factor: Gray = (r*1 + g*2 + b*1) >> 2 gray = (r*2 + g*5 + b*1) >> 3 gray = (r*4 + g*10 + b*2) ;> 4 gray = (r*9 + g*19 + b*4) >> 5 gray = (r*19 + g*37 + b*8) >> 6 gray = (r*38 + g*75 + b* >> 7 gray = (r*76 + g*150 + b*30) >> 8 gray = (r*153 + g*300 + b*59) >> 9 gray = (r*3 + g*601 + b*117) >> 10 gray = (r*612 + g*1202 + b*234) >> 11 gray = (r*1224 + g*2405 + b*467) ;> 12 gray = (r*2449 + g*4809 + b*934) >> 13 gray = (r*4898 + g*9618 + b*1868) >> 14 gray = (r*9797 + g*19235 + b*3736) >> 15 gray = (r*19595 + g*38469 + b*7472) >> 16 gray = (r*39190 + g*7693 9 + b*14943) >> 17 gray = (r*78381 + g*153878 + b*29885) >> 18 gray = (r*156762 + g*307757 + b*59769 ) >> 19 gray = (r*313524 + g*615514 + b*119538) >> 20&nbsp Three, grayscale code implementation Jniexport jintarray jnicall Java_org_join_image_util_joinimage_imgtogray (          jnienv* env, Jobject obj, Jintarray buf, int w, int h) {     loge ("= = imgtogray== ");       jint * cbuf;     cbuf = (*env) Getintarrayelements (env, buf, 0); Gets an int array element         int alpha = 0xFF; Opacity value      int I, j, color, red, green, blue;     for (i = 0; i < h; i++) {         for (j = 0; J < W; J + +) {     & Nbsp;       color = cbuf[w * i + j]; Get color value              red = (color >> 16) & 0xFF; Get red value              green = (color >> 8) &Amp 0xFF; Get green Value              blue = color & 0xFF; Get blue value              color = (RED * + Green * 75 + Blue *) >> 7; Grayscale algorithm (7-bit precision under 16-bit operation)              color = (alpha << 24) | (Color << 16) | (Color << 8) | Color The new color value is composed of ARGB              cbuf[w * i + j] = Color Set a new color value          }     }       int size = w * H;     jintarray result = (*env)->newintarray (env, size); Create a new jintarray      (*env)->setintarrayregion (env, result, 0, size, cbuf); Transfer cbuf to result      (*env)->releaseintarrayelements (env, buf, CBUF, 0); Release int array element      return result; }    This article welcome reprint, but please specify the source and author Source:/http Blog.sina.com.cn/staratsky Author: Meteor

Learn more about JNI---image grayscale processing on the Android platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.