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

  • SEARCH
  • Home
  • 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 8704451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:06:31+00:00 2026-06-13T03:06:31+00:00

I am new to android ndk.I have started learning through the image processing example

  • 0

I am new to android ndk.I have started learning through the image processing example by
ruckus and by IBM blog. I am trying to graying out an image.
Here is the code i am using

An xml file for the layout shown

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
  >
    <ImageView
      android:id="@+id/gimageView1"
      android:layout_width="400px"
      android:src="@drawable/wallace"
      android:layout_height="266px"
    />

    <Button
      android:id="@+id/gbutton"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Go Gray"
    />

    <ImageView
      android:id="@+id/gimageView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    />
  </LinearLayout>

and the java code is

package com.example;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class GrayClass extends Activity {
      private ImageView imageView;
      private Bitmap bitmap;
      private Button button;
      private Bitmap original;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gray);
      original  = BitmapFactory.decodeResource(getResources(), R.drawable.wallace);
      bitmap    = BitmapFactory.decodeResource(getResources(), R.drawable.wallace);
      button    = (Button) findViewById(R.id.gbutton);
      imageView = (ImageView) findViewById(R.id.gimageView2);
      button.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
            ((ImageView)findViewById(R.id.gimageView1)).setVisibility(View.GONE);
            button.setVisibility(View.GONE);
              GoGray();     
      }

    });

  }

  private void GoGray() {
        Bitmap oBitmap = original.copy(Bitmap.Config.ARGB_8888, true);
        Bitmap gBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        goGrayWithNative(oBitmap,gBitmap );
        imageView.setImageBitmap(gBitmap);

  }

  public native void goGrayWithNative(Bitmap bmp1, Bitmap bmp2);
}

and here is the .c file where i have written the code for gray out logic

/*
convertToGray
Pixel operation
*/
JNIEXPORT void JNICALL Java_com_example_GrayClass_goGrayWithNative(JNIEnv
* env, jobject  obj, jobject bitmapcolor,jobject bitmapgray)
{
    AndroidBitmapInfo  infocolor;
    void*              pixelscolor;
    AndroidBitmapInfo  infogray;
    void*              pixelsgray;
    int                ret;
    int             y;
    int             x;

    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;
    }

    if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        LOGE("Bitmap format is not RGBA_8888 !");
        return;
    }


LOGE("Bitmap format is not RGBA_8888 !====%d==", infocolor.format ) ;




    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
    LOGI("unlocking pixels height = %d",infocolor.height);
    for(y=0;y<infocolor.height;y++) {
    LOGI("unlocking pixels height = %d",infocolor.width);
        argb * 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 code is running fine but output i am getting is different see the picsenter image description here

after clicking the GoGray button it shows image like this

enter image description here

Can anyone tell me where the mistake is ?

  • 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-06-13T03:06:33+00:00Added an answer on June 13, 2026 at 3:06 am

    As far as i know, Android bitmaps can only handle 32-bit-per-pixel images, so you have to store the grayscale result as if it where a color image, by repeating the gray value in the red, green and blue channels, setting the alpha channel to fully opaque.

    By the way if you analyze your screenshots, you’ll realize that the grayscale version has a width of precisely 1/4 of the color image, which tends to show this is the problem.

    Using this code in the C++ part should do the job:

    // modify pixels with image processing algorithm
    LOGI("unlocking pixels height = %d",infocolor.height);
    for(y=0;y<infocolor.height;y++) {
    LOGI("unlocking pixels height = %d",infocolor.width);
        argb * line = (argb *) pixelscolor;
        argb * grayline = (argb *) pixelsgray;
    
        for(x=0;x<infocolor.width;x++) {
    
            uint8_t v = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;
            grayline[x].red = v;
            grayline[x].green = v;
            grayline[x].blue = v;
            grayline[x].alpha = line[x].alpha
        }
        pixelscolor = (char *)pixelscolor + infocolor.stride;
        pixelsgray = (char *) pixelsgray + infogray.stride;
    }
    

    Hope this helps!

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

Sidebar

Related Questions

I am new to android ndk.I have started learning through the image processing example
I have started teaching myself about the Android NDK and I have followed this
I have some image processing Java code in Android that acts upon two large
I'm new to Android and am trying to create a simple SDK+NDK concept. I
I am trying to use the new android NDK plugin that comes with ADT
i am creating a new android application.i am using the table layout. I have
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
Hi I am new to android NDK Devlopment.I downloaded Android NDK 4 for windows.I
Hi I am new to android Programming and NDK.Yet i am not clear with
I am new to both openCV and Android. I have to detect objects in

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.