Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

透過 JNI 呼叫美圖秀秀 so 實作 Android 圖片美化

編輯:關於 Android 程式設計

實作概念

這篇文章說明如何透過 JNI 載入 libmtimage-jni.so,並在 Java 端呼叫原生影像處理方法。使用前要確認 so 與裝置 ABI 相符,並把檔案放到正確的 jniLibs 或 libs 對應位置。

JNI 類別

JNI 類別會在 static 區塊中呼叫 System.loadLibrary 載入原生庫,並宣告多個 native 方法。常見方法包含 AutoColor、AutoConstrast、StyleLomoB、SkinWhite、Sharp 等。

public class JNI {
  static {
    System.loadLibrary("mtimage-jni");
  }
  public native void StyleLomoB(int[] pixels, int width, int height);
  public native void SkinWhite(int[] pixels, int width, int height, int value);
  public native void Sharp(int[] pixels, int width, int height, float value1, float value2);
}

介面與呼叫

版面配置可以包含一個 Button 與 ImageView。按下按鈕後,把 Bitmap 的像素取出,傳給 native 方法處理,再用處理後的像素建立新的 Bitmap。

int width = bm.getWidth();
int height = bm.getHeight();
int[] pixels = new int[width * height];
bm.getPixels(pixels, 0, width, 0, 0, width, height);
jni.StyleLomoB(pixels, width, height);
Bitmap bm2 = Bitmap.createBitmap(pixels, width, height, bm.getConfig());
image.setImageBitmap(bm2);

這種做法的核心是 Bitmap 與 int 像素陣列之間的轉換。native 方法修改 pixels 後,Java 端再用新陣列建立圖片,即可看到美化效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved