Android improves the method of using NDK to convert a color map to a grayscale image _android

Source: Internet
Author: User
Tags int size

In general, the use of Java to implement a color map to grayscale graphics on Android, similar to the implementation method on J2ME, but encounter frequent conversion or large image conversion, you must use NDK to improve speed. This paper mainly through the Java and NDK two ways to achieve color map conversion to gray-scale map, and give a comparison of speed, for everyone's reference.

Let's start with a simple introduction to Android's NDK steps:

Take NDK R4 As an example, perhaps the later version of the NDK use a slightly different method.
1, download support for C + + Android-ndk-r4-crystax, support C + + words can play more strong.

2, download Cygwin, choose ftp://mirrors.kernel.org this mirror, search Devel Install install gcc and make tools;

As shown in the figure:

To search for GCC and make separately in the search box, you must be Devel the Install column.

3, Cygwin installation directory, find the Home/username directory of the. bash_profile file, open the file in the final add:
Ndk=/cygdrive/d:cygwin/android-ndk-r4-crystax
Export NDK
PS: Suppose installed in D:/cygwin/android-ndk-r4-crystax.
4, run Cygwin, through the CD command to go to the ndk/samples/example directory/, run $ndk/ndk-build to compile the directory Android.mk

Here's a personal habit.

5, install Eclipse CDT, the official Download CDT installation package, decompression after plugins and feagures copy overlay to eclipse folder can

6, go to the System Properties-> environment variable->path add "D:/cygwin/bin" (assuming Cygwin installed in D: under) and "D:/cygwin/android-ndk-r4-crystax", restart the computer, You can then build a cygwin-C + + project in Eclipse to verify that the NDK program compiles successfully and then the 4th step to generate the so file.

Let's take a look at how this program works:

NDK is really a lot shorter than Java in terms of time consuming to convert a grayscale image.

Main.xml source code is as follows:

<?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 "> 
<button android:layout_height=" wrap_content "android:layout_width=" fill_parent "android:id=" @+id/ Btnjava "android:text=" using Java Transform grayscale "/>  
<button android:layout_height=" Wrap_content "android:layout_width = "Fill_parent" android:id= "@+id/btnndk" android:text= "use NDK convert grayscale map"/> <imageview  
"android:id= ImageView01 "android:layout_width=" fill_parent "android:layout_height=" fill_parent "/> </linearlayout  
> 

Main program Testtogray.java source code is as follows:

Package Com.testtogray; 
Import android.app.Activity; 
Import Android.graphics.Bitmap; 
Import Android.graphics.Bitmap.Config; 
Import android.graphics.drawable.BitmapDrawable; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.ImageView; 
  The public class Testtogray extends activity {/** called the ' when ' is the ' The activity ' is a-a-created. 
  ImageView Imgview; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.main); 
    This.settitle ("Use NDK to convert grayscale---hellogv"); 
    Btnjava= (Button) This.findviewbyid (R.id.btnjava); 
     
    Btnjava.setonclicklistener (New Clickevent ()); 
    btnndk= (Button) This.findviewbyid (R.ID.BTNNDK); 
    Btnndk.setonclicklistener (New Clickevent ()); 
  imgview= (ImageView) This.findviewbyid (R.ID.IMAGEVIEW01); Class Clickevent implements view.onclicklistener{@Override public void OnClick(View v) 
        {if (V==btnjava) {long current=system.currenttimemillis (); 
        Bitmap img=convertgrayimg (R.DRAWABLE.CAT); 
        Long Performance=system.currenttimemillis ()-current; 
        Displays grayscale Imgview.setimagebitmap (IMG); TestToGray.this.setTitle ("W:" +string.valueof (Img.getwidth ()) + ", H:" +string.valueof (Img.getheight ()) + Java time consuming 
      "+string.valueof (performance) +" millisecond "); 
        else if (V==BTNNDK) {long current=system.currenttimemillis (); 
        First open the image and read the pixel Bitmap img1= (bitmapdrawable) getresources (). getdrawable (R.drawable.cat)). Getbitmap (); 
        int W=img1.getwidth (), H=img1.getheight (); 
        int[] pix = new int[w * h]; 
        Img1.getpixels (pix, 0, W, 0, 0, W, h); 
        converting color pixels to grayscale pixels via imgtogray.so int[] Resultint=libfuns.imgtogray (pix, W, h); 
        Bitmap Resultimg=bitmap.createbitmap (W, H, config.rgb_565); 
  Resultimg.setpixels (resultint, 0, W, 0, 0,w, h);      Long Performance=system.currenttimemillis ()-current; 
        Displays grayscale Imgview.setimagebitmap (resultimg);  TestToGray.this.setTitle ("W:" +string.valueof (Img1.getwidth ()) + ", H:" +string.valueof (Img1.getheight ()) + "NDK time consuming 
      "+string.valueof (performance) +" millisecond ");  /** * Convert resource picture to grayscale * @param resid Resource ID * @return/public Bitmap convertgrayimg (int 
     
    RESID) {Bitmap img1= (bitmapdrawable) getresources (). getdrawable (Resid)). Getbitmap (); 
    int W=img1.getwidth (), H=img1.getheight (); 
    int[] pix = new int[w * h]; 
    Img1.getpixels (pix, 0, W, 0, 0, W, h); 
    int alpha=0xff<<24;  for (int i = 0; i < H; i++) {for (int j = 0; J < W; J +) {//get pixel color int color = Pix[w  
        * i + j];  
        int red = ((color & 0x00ff0000) >> 16);  
        int green = ((color & 0x0000ff00) >> 8);  
        int blue = color & 0x000000ff; color = (rEd + green + blue)/3; color = Alpha | (Color << 16) | (Color << 8) |  
        Color 
      Pix[w * i + j] = color; 
    } Bitmap Result=bitmap.createbitmap (W, H, config.rgb_565); 
    Result.setpixels (pix, 0, W, 0, 0,w, h); 
  return result;

 } 
}

The source code for the Java class Libfuns.java that encapsulates the NDK function is as follows:

Package Com.testtogray; 
public class Libfuns { 
  static { 
    system.loadlibrary ("Imgtogray"); 
  } 
  /** 
  * @param width The current view width 
  * @param height The current view height */public 
  static native Int[] Imgtogray (int[] buf, int w, int h); 
}

Color map conversion to gray-scale ImgToGray.cpp source code:

#include <jni.h> #include <stdio.h> #include <stdlib.h> extern "C" {jniexport jintarray jnicall Ja 
Va_com_testtogray_libfuns_imgtogray (jnienv* env, Jobject obj, Jintarray buf, int w, int h); 
} 
; Jniexport jintarray jnicall Java_com_testtogray_libfuns_imgtogray (jnienv* env, Jobject obj, JintArray buf, int W, in 
  t h) {Jint *cbuf; 
  Cbuf = Env->getintarrayelements (buf, false); 
  if (cbuf = = NULL) {return 0;/* exception occurred/} int alpha = 0xFF << 24; for (int i = 0; i < H; i++) {for (int j = 0; J < W; J +) {//get pixel color int color = Cbuf[w * i + j 
      ]; 
      int red = ((color & 0x00ff0000) >> 16); 
      int green = ((color & 0x0000ff00) >> 8); 
      int blue = color & 0x000000ff; 
      color = (red + green + blue)/3; color = Alpha | (Color << 16) | (Color << 8) | 
      Color 
    Cbuf[w * i + j] = color; 
  } int size=w * h; Jintarray RESult = Env->newintarray (size); 
  Env->setintarrayregion (result, 0, size, cbuf); 
  Env->releaseintarrayelements (buf, cbuf, 0); 
return result;

 }

Android.mk's source code:

Local_path:= $ (call My-dir) 
include $ (clear_vars) 
local_module  : = Imgtogray 
Local_src_files: = ImgToGray.cpp 
include $ (build_shared_library)

Interested readers can start debugging the code described in this article, I believe it will be for everyone to develop the Android project has some help.

Related Article

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.