Android uses the NDK to process bitmap and how to control the suface display of the view directly through C + +, learning notes and directions

Source: Internet
Author: User

The recent study is really difficult, because after learning the SDK development project, after doing some simple projects, feel that in the UI thread loading more pictures, there will always be a lag feeling, especially when the finger sliding down, this lag is more noticeable, and Java's structural development ability is really very powerful, Using Java development is really a great thing to enjoy, but Java has no advantage in some high-density computing, and it's going to be a bit of a bad thing when it comes to having a memory auto-recycle. Any language or thing, there will always be pros and cons, this is Java so easy to use a side effect. So in order not to make Java side effects harmful to developers using Java,java produced a very attractive technology JNI technology that allows Java to invoke programs written in C/C + + and other languages. This technology also supports--NDK in Android, and the NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries, and automatically package so and Java applications together as APK, which is handy to use.

Say a bunch of crap, and then record what they have learned recently, it can be said that one of the important learning materials of the NDK is the information provided by the official Android website, including the download of the NDK, but also to see some simple project using the NDK source code, These sources let us know how Android is officially advising us on how to use the NDK and can even develop applications in a pure C + + language, but that's not something I'm interested in.

According to the official use of the method or relatively simple, here is a C language to extract the photo grayscale function, and in Java call

Write Java Code First

public class Photoprocessingactivity extends Activity{private Bitmap bmoriginal,bmgray;private ImageView iv;private Button Btgray,btopen;static{system.loadlibrary ("photoprocessing");} @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.photo_processing_activity); setup ();} public void Setup () {IV = (ImageView) Findviewbyid (r.id.showphoto); bmoriginal = Imageshare.mbitmap;iv.setimagebitmap ( bmoriginal); btgray = (Button) Findviewbyid (R.id.btgray); Btgray.setonclicklistener (onclick); btopen = (Button) Findviewbyid (R.id.btopen); Btopen.setonclicklistener (onclick);} Onclicklistener onclick = new Onclicklistener () {@Overridepublic void onclick (View v) {//TODO auto-generated method STUBSW Itch (V.getid ()) {Case R.id.btgray:bmgray = Bitmap.createbitmap (Bmoriginal.getwidth (), Bmoriginal.getheight (), Config.alpha_8); Grayphoto (bmoriginal, Bmgray); Iv.setimagebitmap (Bmgray); Break;case r.id.btopen:intent Intent= new Intent (photoprocessingactivity.this,surfaceprocessingactivity.class); startactivity (Intent); break;}}; public native void Grayphoto (Bitmap bmoriginal,bitmap bmgray);
Use the Javah tool to get the header file PhotoProcessingActivity.h and add some of the header files needed to process the image, which are included in the NDK, and can be used to link the folder in

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class com_example_photoprocessing _activity_photoprocessingactivity * * #include <android/bitmap.h> #include <android/log.h> #include < math.h> #include <string.h> #ifndef _included_com_example_photoprocessing_activity_ Photoprocessingactivity#define _included_com_example_photoprocessing_activity_photoprocessingactivity#ifdef __ Cplusplusextern "C" {#endif #undef com_example_photoprocessing_activity_photoprocessingactivity_mode_private# Define Com_example_photoprocessing_activity_photoprocessingactivity_mode_private 0l#undef Com_example_ Photoprocessing_activity_photoprocessingactivity_mode_world_readable#define Com_example_photoprocessing_ Activity_photoprocessingactivity_mode_world_readable 1l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_mode_world_writeable#define Com_example_photoprocessing_activity_ Photoprocessingactivity_mode_world_writeable 2l#undef Com_examplE_photoprocessing_activity_photoprocessingactivity_mode_append#define Com_example_photoprocessing_activity_ Photoprocessingactivity_mode_append 32768l#undef Com_example_photoprocessing_activity_photoprocessingactivity_ Mode_multi_process#define com_example_photoprocessing_activity_photoprocessingactivity_mode_multi_process 4L# undef Com_example_photoprocessing_activity_photoprocessingactivity_mode_enable_write_ahead_logging#define Com_ Example_photoprocessing_activity_photoprocessingactivity_mode_enable_write_ahead_logging 8L#undef Com_example_ Photoprocessing_activity_photoprocessingactivity_bind_auto_create#define Com_example_photoprocessing_activity_ Photoprocessingactivity_bind_auto_create 1l#undef Com_example_photoprocessing_activity_photoprocessingactivity_ Bind_debug_unbind#define Com_example_photoprocessing_activity_photoprocessingactivity_bind_debug_unbind 2L#undef Com_example_photoprocessing_activity_photoprocessingactivity_bind_not_foreground#define Com_example_ Photoprocessing_actiVity_photoprocessingactivity_bind_not_foreground 4l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_bind_above_client#define Com_example_photoprocessing_activity_photoprocessingactivity_ Bind_above_client 8l#undef Com_example_photoprocessing_activity_photoprocessingactivity_bind_allow_oom_ Management#define com_example_photoprocessing_activity_photoprocessingactivity_bind_allow_oom_management 16L# undef Com_example_photoprocessing_activity_photoprocessingactivity_bind_waive_priority#define Com_example_ Photoprocessing_activity_photoprocessingactivity_bind_waive_priority 32l#undef Com_example_photoprocessing_ Activity_photoprocessingactivity_bind_important#define Com_example_photoprocessing_activity_ Photoprocessingactivity_bind_important 64l#undef Com_example_photoprocessing_activity_photoprocessingactivity_ Bind_adjust_with_activity#define Com_example_photoprocessing_activity_photoprocessingactivity_bind_adjust_with_ ACTIVITY 128l#undef Com_example_photoprocessing_activity_Photoprocessingactivity_context_include_code#define Com_example_photoprocessing_activity_ Photoprocessingactivity_context_include_code 1l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_context_ignore_security#define Com_example_photoprocessing_activity_ Photoprocessingactivity_context_ignore_security 2l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_context_restricted#define Com_example_photoprocessing_activity_photoprocessingactivity_ context_restricted 4l#undef Com_example_photoprocessing_activity_photoprocessingactivity_result_canceled#define com_example_photoprocessing_activity_photoprocessingactivity_result_canceled 0L#undef Com_example_ Photoprocessing_activity_photoprocessingactivity_result_ok#define Com_example_photoprocessing_activity_ Photoprocessingactivity_result_ok-1l#undef Com_example_photoprocessing_activity_photoprocessingactivity_result_ First_user#define Com_example_photoprocessing_activity_photoprocessingactivity_result_first_user 1L#undef Com_example_photoprocessing_activity_photoprocessingactivity_default_keys_disable#define Com_example_ Photoprocessing_activity_photoprocessingactivity_default_keys_disable 0l#undef Com_example_photoprocessing_ Activity_photoprocessingactivity_default_keys_dialer#define Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_dialer 1l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_shortcut#define Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_shortcut 2l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_search_local#define Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_search_local 3l#undef Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_search_global#define Com_example_photoprocessing_activity_ Photoprocessingactivity_default_keys_search_global 4l/* * class:com_example_photoprocessing_activity_ Photoprocessingactivity* Method:grayphoto * Signature: (landroid/graphics/bitmap;  Landroid/graphics/bitmap;) V *//** * Xuan */#define LOG_TAG "photoprocessing" #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 Jniexport void Jnicall Java_com_example_photoprocessing_activity_photoprocessingactivity_grayphoto (JNIENV *, Jobject, Jobject, jobject); #ifdef __cplusplus} #endif #endif
then write the C code

#include "PhotoProcessingActivity.h" Jniexport void Jnicall java_com_example_photoprocessing_activity_ Photoprocessingactivity_grayphoto (jnienv *env, Jobject photoprocessingactivity, Jobject bmOriginal,jobject bmGray) { Androidbitmapinfo origanalcolor;void* Pixelscolor; Androidbitmapinfo infogray;void* pixelsgray;int ret;int y;int x; Logi ("in Converttogray"); if (ret = Androidbitmap_getinfo (env, bmoriginal, &origanalcolor)) < 0) {LOGE (" Androidbitmap_getinfo () failed! error=%d ", ret); return;} if (ret = Androidbitmap_getinfo (env, Bmgray, &infogray)) < 0) {LOGE ("Androidbitmap_getinfo () failed! error=%d ", ret); return;} Logi ("Original image:: Width is%d; Height is%d; Stride is%d; Format is%d;flags is%d,stride is%u ", Origanalcolor.width, Origanalcolor.height, Origanalcolor.stride, Origanalcolor.format, Origanalcolor.flags, origanalcolor.stride); if (Origanalcolor.format! = ANDROID_BITMAP_FORMAT_ rgba_8888) {LOGE ("Bitmap format is not rgba_8888!"); /return;} if (origanalcolor.foRmat = = android_bitmap_format_rgb_565) {logi ("Original Image is android_bitmap_format_rgb_565"); if (ret = Androidbitmap_lockpixels (env, bmoriginal, &pixelscolor)) < 0) {LOGE ("Androidbitmap_lockpixels () failed! error=%d ", ret);} if (ret = Androidbitmap_lockpixels (env, Bmgray, &pixelsgray)) < 0) {LOGE ("Androidbitmap_lockpixels () failed! error=%d ", ret);} Modify pixels with image processing algorithmfor (y = 0, y < origanalcolor.height; y++) {__uint16_t * line = (__uint 16_t *) pixelscolor;uint8_t * Grayline = (uint8_t *) pixelsgray;for (x = 0; x < origanalcolor.width; + +) {Grayline[x] = (uint8_t) (((Line[x] >> <<3) + (Line[x] >> 5 & 3) + (LINE[X]&AMP;31 * 8))/);//logi ("%d %d%d%d ", line[x].alpha,line[x].red,line[x].green,line[x].blue);/*if (x ==0) {logi (" Line:%o grayline%o ", Line[x],grayline[x]);} */}pixelscolor = (char *) Pixelscolor + Origanalcolor.stride;pixelsgray = (char *) Pixelsgray + infogray.stride;} Logi ("Unlocking Pixels"); Androidbitmap_unlockpixels (env, bmoriginal); Androidbitmap_unlockpixels (env, Bmgray); Logi ("Return!!"); return;} Logi ("Gray Image:: width is%d; Height is%d; Stride is%d; Format is%d;flags are%d%d,stride is%u ", Infogray.width, Infogray.height, Infogray.stride, Infogray.format, Infogray.fla GS, Infogray.stride), if (infogray.format! = android_bitmap_format_a_8) {LOGE ("BITMAP format is not a_8!"); return;} if (ret = Androidbitmap_lockpixels (env, bmoriginal, &pixelscolor)) < 0) {LOGE ("androidbitmap_lockpixels () Failed! error=%d ", ret);} if (ret = Androidbitmap_lockpixels (env, Bmgray, &pixelsgray)) < 0) {LOGE ("Androidbitmap_lockpixels () failed! error=%d ", ret);} Modify pixels with image processing algorithmfor (y = 0, y < origanalcolor.height; y++) {ARGB * line = (ARGB *) Pixe lscolor;uint8_t * Grayline = (uint8_t *) pixelsgray;for (x = 0, x < origanalcolor.width, x + +) {Grayline[x] = (LINE[X].R Ed + line[x].green + line[x].blue)/3;} Pixelscolor = (char *) PIXELSCOlor + Origanalcolor.stride;pixelsgray = (char *) Pixelsgray + infogray.stride;} Logi ("unlocking pixels"); Androidbitmap_unlockpixels (env, bmoriginal); Androidbitmap_unlockpixels (env, bmgray);}
Write the Android.mk file, which mainly indicates the. so file to be used, and generates our own. so file

Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = photoprocessinglocal_src_files: = Photoprocessing.cpplocal_ldlibs    : =-llog-ljnigraphicsinclude $ (build_shared_library)
Some of the key code is these, where the processing of the grayscale algorithm may not be very optimized, which processing RGB565 part of my own according to the definition of grayscale in the image processing, which also thanks to Sun Zhihai brother to my help!

It can be said that using this method is relatively simple, but it seems that the problem that I started with is still not solved, that is, this way is only to improve the efficiency of the program, but still need to update the interface in the UI thread, here can also use Surfaceview to do asynchronous update interface, but here, I want to know, Without modifying the Android system source, the NDK's limit is whether it can directly control the view's surface, so that it does not through the Java code directly to the interface refresh it? This may involve Linux file permissions issues, as well as the need to flip through the Android system source code.

Hope to be interested in this area of friends, can communicate with me, or can give me some advice. My QQ is 835060947, can also send e-mail to me.




Android uses the NDK to process bitmap and how to control the suface display of the view directly through C + +, learning notes and directions

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.