Android 下 JNI 开发

02_美图秀秀案例-35

安装美图秀秀到模拟器,并演示处理图片

使用ColorMatrixDemo.swf修改数组值,改变图片颜色值

反编译美图秀秀和提取美图秀秀的.so文件

  native方法是不混淆的

创建Android工程:MTXXDemo

  包名:.mtxxdemo

 

实现特效效果LomoB

 1.布局文件代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <ImageView

        android:id="@+id/iv"

        android:src="@drawable/leiju"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

 

    <Button

        android:onClick="lomo"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="lomo效果" />

 

</LinearLayout>

 

Java部分代码

 

//点击事件-但点击后把ImageView的图片,转换成lomo效果

public void lomo(View view){

//1.需要获得图片的像数矩阵

Bitmap map = BitmapFactory.decodeResource(getResources(), R.drawable.leiju);

int[] pixels = new int[map.getWidth()*map.getHeight()];

//此方法执行后,pixels数组里面已经有了图片的像数数组

map.getPixels(pixels , 0, map.getWidth(), 0, 0, map.getWidth(), map.getHeight());

//2.把像数矩阵传递给C语言,让它帮我们设置效果

JNI jni = new JNI();

/**

 * p1:图片的像数矩阵

 * p2:图片的宽

 * p3:图片的高

 */

//C语言之间控制的是内存,会直接把像数数组的值修改

jni.StyleLomoB(pixels, map.getWidth(), map.getHeight());

//3.像数矩阵已经改变,根据颜色矩阵创建一种图片设置回ImageView

Bitmap bm= Bitmap.createBitmap(pixels, map.getWidth(), map.getHeight(), Config.ARGB_8888);

iv.setImageBitmap(bm);

}

 

JNI代码通过反编译得到,但包名和美图秀秀必须一样。部分代码如下

package com.mt.mtxx.image;

 

public class JNI {

{

System.loadLibrary("mtimage-jni");

}

    

 

    public native void StyleLomoHDR(int[] p1, int p2, int p3);

    public native void StyleLomoC(int[] p1, int p2, int p3);

    public native void StyleLomoB(int[] p1, int p2, int p3);

 

 

    public native byte[] deal(byte[] p1, int p2, int p3, int p4, int p5, String p6);

 

}

 

运行演示,图片改变颜色

演示不同方法

StyleLomoB:特殊效果

jni.StyleLomoC:黑白效果

StyleLomoHDR:高亮效果

 

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。