Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3878598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:37:51+00:00 2026-05-19T22:37:51+00:00

I have some image processing Java code in Android that acts upon two large

  • 0

I have some image processing Java code in Android that acts upon two large int arrays. Most of the time, Java is fast enough but I need to use C via JNI and the NDK to speed up a few operations.

The only way I know that I can pass the data from the int arrays to C is to use ByteBuffer.allocateDirect to create a new buffer, copy the data to that and then make the C code act upon the buffer.

However, I cannot see any way I can manipulate the data in this buffer in Java as if the buffer was an int[] or a byte[]. For example, a call to ByteBuffer.array() will fail on the newly created buffer. Is there any way to make this work?

I have limited memory and want to reduce how many arrays/buffers I need. For example, it would be nice if I could use IntBuffer.wrap(new int[…]) to create the buffer and then manipulate the array backing the buffer directly in Java but I cannot do this because the only thing that seems to work here for JNI is ByteBuffer.allocateDirect.

Are there any other ways to send data back and forth between C and Java? Can I somehow allocate memory on the C side and have Java send data directly to there?

Edit: A benchmark comparing buffer use to int[] use:

int size = 1000;
IntBuffer allocateDirect = java.nio.ByteBuffer.allocateDirect(4 * size).asIntBuffer();
for (int i = 0; i < 100; ++i)
{
  for (int x = 0; x < size; ++x)
  {
    int v = allocateDirect.get(x);
    allocateDirect.put(x, v + 1);
  }
}

int[] intArray = new int[size];
for (int i = 0; i < 100; ++i)
{
  for (int x = 0; x < size; ++x)
  {
    int v = intArray[x];
    intArray[x] = v + 1;
  }
}

On a Droid phone, the buffer version takes ~10 seconds to finish and the array version takes ~0.01 seconds.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:37:51+00:00Added an answer on May 19, 2026 at 10:37 pm

    From http://java.sun.com/docs/books/jni/html/objtypes.html, use JNI’s Get/Release<TYPE>ArrayElements(...)

    In this example, I will pass an array ( for argument’s sake, it’s int array = new int[10] and then fill it with 0-9

     JNIEXPORT jint JNICALL 
     Java_IntArray_doStuffArray(JNIEnv *env, jobject obj, jintArray arr)
     {
    
         // initializations, declarations, etc
         jint *c_array;
         jint i = 0;
    
         // get a pointer to the array
         c_array = (*env)->GetIntArrayElements(env, arr, NULL);
    
         // do some exception checking
         if (c_array == NULL) {
             return -1; /* exception occurred */
         }
    
         // do stuff to the array
         for (i=0; i<10; i++) {
             c_array[i] = i;
         }
    
         // release the memory so java can have it again
         (*env)->ReleaseIntArrayElements(env, arr, c_array, 0);
    
         // return something, or not.. it's up to you
         return 0;
     }
    

    Study section 3.3, and specifically 3.3.2 — this will allow you to get a pointer to the array in java’s memory, modify it, and release it, in effect allowing you to modify the array in native code.

    I’ve just used it in my own project (with short arrays) and it works great 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some image processing code that loops through 2 multi-dimensional byte arrays (of
I have a Java application that is performing some realtime image processing, with the
I have some image processing code that runs on a background thread and updates
I have some code that downloads an image from a remote server $data =
I have some code that loads an image file off the web and puts
I have an application that does some image processing using rmagick and imagemagick. This
Hii all,, I have some project that requires me to do Image processing, I
I'm using ruby-opencv to do some image processing. I have an IplImage object that
I have an application that does some image processing on each new frame, recently
So I have some cool Image Processing algorithm. I have written it in OCaml.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.