在native层中访问Java层对象

首先创建java层对象

package jp.co.ricoh.advop.mini.idcard;

/**
 * Created by tangzhongliang on 9/16/2016.
 */
public class CardInfo {
    int positionX ;
    int positionY ;
    String path;
    int scale;

    public CardInfo(int positionX, int positionY, String path,int scale) {
        this.positionX = positionX;
        this.positionY = positionY;
        this.path = path;
        this.scale = scale;
    }

    @Override
    public String toString() {
        return String.format("start %d,%d,%s,%d",positionX,positionY,path,scale);
    }
}

 将CardInfo数组传入jni中,在Native层中通过反射的方式访问CardInfo信息

JNIEXPORT jint JNICALL Java_jp_co_ricoh_advop_mini_idcard_ImageHandle_combinecard
(JNIEnv * env, jobject obj,jstring dstString,jint width,jint height,jobjectArray objarray){
    LOGD("combine card");
    jsize len = env->GetArrayLength(objarray);
    int ow = (int)width;
    int oh = (int)height;
    Mat newMat = Mat(Size(ow,oh),CV_8UC3,Scalar(255,255,255));
    for(int i = 0;i<len;i++){
       jobject cardinfo = env->GetObjectArrayElement(objarray,(jsize)i);
       jclass cls = env->GetObjectClass(cardinfo);
       jfieldID fid = env->GetFieldID(cls,"positionX","I");
       int x = (int)env->GetIntField(cardinfo,fid);
       fid = env->GetFieldID(cls,"positionY","I");
       int y = (int)env->GetIntField(cardinfo,fid);
       fid = env->GetFieldID(cls,"path","Ljava/lang/String;");
       jstring jstr = (jstring)env->GetObjectField(cardinfo,fid);
       char* path = jstring2string(env,jstr);
       fid = env->GetFieldID(cls,"scale","I");
       int scale = (int)env->GetIntField(cardinfo,fid);
       //resize
       Mat logo = imread(path,-1);
       resize(logo,logo,Size(logo.cols*scale,logo.rows*scale));
       LOGD("%d,%d,%d",ow,oh,scale);
       if(mixImage(newMat,logo,x,y)==-1){
           return -1;
       }
    }
    char* dstPath = jstring2string(env,dstString);
    imwrite(dstPath,newMat);
    LOGD("complete");
    return 0;
  }

 

转载于:https://my.oschina.net/leonardtang/blog/797037

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值