目錄
目錄
前言
在Android開(kāi)發(fā)中有時(shí)需要我們對(duì)Bitmap進(jìn)行相應(yīng)的操作剃盾,雖然利用Java代碼也能實(shí)現(xiàn),但是如果我們通過(guò)Native層代碼對(duì)Bitmap進(jìn)行操作的話效率會(huì)更高。
Java層與Native層效果對(duì)比
-
Java層效果
Java層 -
Native層效果
Native層
可以看到Java層代實(shí)現(xiàn)的效果是有明顯的卡頓的作媚。
Native層實(shí)現(xiàn)圖像二值化
1.Java層聲明Native方法
/**
* 對(duì)圖像二值化
* @param bitmap
*/
public native void getBinaryBitmap(Object bitmap);
2.生成頭文件(參考Android NDK開(kāi)發(fā)小技巧)
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_hehuidai_binarydemo_MainActivity */
#ifndef _Included_com_hehuidai_binarydemo_MainActivity
#define _Included_com_hehuidai_binarydemo_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_hehuidai_binarydemo_MainActivity
* Method: getBinaryBitmap
* Signature: (Ljava/lang/Object;)V
*/
JNIEXPORT void JNICALL Java_com_hehuidai_binarydemo_MainActivity_getBinaryBitmap
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
3.native-lib.c中進(jìn)行方法實(shí)現(xiàn)(這里我是使用的C語(yǔ)言)
#include <jni.h>
#include <android/bitmap.h>
JNIEXPORT void JNICALL Java_com_hehuidai_binarydemo_MainActivity_getBinaryBitmap
(JNIEnv* env, jobject jobj, jobject jBitmap) {
int result;
// 獲取源Bitmap相關(guān)信息:寬、高等
AndroidBitmapInfo sourceInfo;
result = AndroidBitmap_getInfo(env, jBitmap, &sourceInfo);
if (result < 0) {
return;
}
// 獲取源Bitmap像素?cái)?shù)據(jù) 這里用的是32位的int類(lèi)型 argb每個(gè)8位
uint32_t* sourceData;
//鎖定像素的地址(不鎖定的話地址可能會(huì)發(fā)生改變)
result = AndroidBitmap_lockPixels(env, jBitmap, (void**)& sourceData);
if (result < 0) {
return;
}
// 遍歷各個(gè)像素點(diǎn)
int color;
int red, green, blue , alpha;
int width = sourceInfo.width;
int height = sourceInfo.height;
int w, h;
for (h = 0; h < height; h++) {
for (w = 0; w < width; w++) {
color = sourceData[h * width + w];
alpha = color & 0xff000000;
red = (color & 0x00ff0000) >> 16;
green = (color & 0x0000ff00) >> 8;
blue = color & 0x000000ff;
// 通過(guò)加權(quán)平均算法,計(jì)算出最佳像素值
color = red * 0.3 + green * 0.59 + blue * 0.11;
if (color <= 95) {
color = 0;
}
else
{
color = 255;
}
sourceData[h * width + w] = alpha | (color << 16) | (color << 8) | color;
}
}
AndroidBitmap_unlockPixels(env, jBitmap);
}
4.引入jnigraphics庫(kù)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
#這里注意要改成.c(如果用C語(yǔ)言寫(xiě)的話)
src/main/cpp/native-lib.c)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
#這里對(duì)庫(kù)進(jìn)行鏈接
jnigraphics)
5.Java層進(jìn)行調(diào)用
...省略部分代碼
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_test);
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getBinaryBitmap(bitmap);
mImage.setImageBitmap(bitmap);
}
});