javaWeb开发实现多图上传和调用C++Deeplearning算法进行图片对比检测

首先实现前台界面,上传图片并保存到指定路径,然后java制作dll,利用jni调用C++代码,将保存的图片路径传给dll处理,并返回一个处理结果。

整个环境如下:
window10 x64
jdk1.7 x86,tomcat7.0 x86
myeclipse 2014 32位
struts-2.3.16.3

第一步:利用struts2实现多图片上传,界面如下:

项目的目录结构如下:

首先下载struts2,并在struts-2.3.16.3apps找到struts2-blank.war,将其解压,然后把WEB-INF目录下的所有jar包复制到这,如下图:

在src文件夹右键,新建立一个struts.xml(这里用直接复制struts目录下的struts.xml到项目下,编译器会报错,具体原因我也没弄清楚),具体步骤如图:

struts.xml内容:

  
  
      
  
   
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
            
            /image
            index.jsp
        
    
   
       
  

在WEB-INF目录下新建web.xml如下图:

web.xml内容:

  
  
  Test  
    
    
        struts2  
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
      
  
      
        struts2  
        /*  
      
      
    
    index.html  
    index.htm  
    index.jsp  
    default.html  
    default.htm  
    default.jsp  
    
  

然后在WebRoot目录下新建一个image(文件夹的名称不同的话,需要在struts.xml进行同步修改)文件夹备用:

接下来新建com.action包,在此包下新建upLoadAction.java,(如何要修改名称的话,也需要在struts.xml中修改一下配置信息),代码:

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 使用数组上传多个文件
 * 
 * @author ljq
 *
 */
@SuppressWarnings("serial")
public class upLoadAction extends ActionSupport{
    public native int Contrast(String path1,String path2);
    private File[] image; //上传的文件
    private String[] imageFileName; //文件名称
    private String[] imageContentType; //文件类型
    private String savePath;

    @Override
    public String execute() throws Exception {
        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
        //取得需要上传的文件数组
        File[] files = getImage();
        String []str=new String[2];
        if (files !=null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                //建立上传文件的输出流, getImageFileName()[i]
                str[i]=getSavePath() + "\\" + getImageFileName()[i];
                System.out.println(getSavePath() + "\\" + getImageFileName()[i]);
                FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]);
                //建立上传文件的输入流
                FileInputStream fis = new FileInputStream(files[i]);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len=fis.read(buffer))>0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                fis.close();
            }
        }
        //这一段代码是用来调用dll,进行图片对比的
        try{
            upLoadAction t=new upLoadAction();
            System.loadLibrary("Test4");
            int a=3;
            a=t.Contrast(str[0],str[1]);
            System.out.println("**************"+a);
        }catch(Exception exp){
            System.out.println("处理图片出错!");
        }
        
        
        return SUCCESS;
    }

    public File[] getImage() {
        return image;
    }

    public void setImage(File[] image) {
        this.image = image;
    }

    public String[] getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String[] imageFileName) {
        this.imageFileName = imageFileName;
    }

    public String[] getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(String[] imageContentType) {
        this.imageContentType = imageContentType;
    }

    /**
     * 返回上传文件保存的位置
     * 
     * @return
     * @throws Exception
     */
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
    
    
}

在WebRoot下新建upload.jsp,代码如下:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'upLoad.jsp' starting page
    
    
    
        
    
    
    

  
  
  
        
        
        
        
        
文件1:
文件2:

index.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>


  
    
    My JSP 'message.jsp' starting page
    
    
    
        
  
  
  
    上传成功
  

到此为止,图片就可上传成功了
其中还有就是要注意tomcat,jdk版本要一致,我都是用的1.7对应7.0,然后myeclipse2014 32位,而且要在myeclipse中将自己的jdk1.7和tomcat配置进去,而不能用myeclipse自带的
具体配置可以参考我的另一篇文章:
https://segmentfault.com/a/11...

第二步,制作dll,并调用,制作dll的过程在我的另一篇文章里面有:
https://segmentfault.com/a/11...
我这里只贴出程序入口处的代码:

#include "Test.h"
#include "main.h"

JNIEXPORT jint JNICALL Java_com_action_upLoadAction_Contrast
(JNIEnv *env, jobject obj, jstring path1, jstring path2){
    //将java字符串类型转化为char* 型
    const char* str1 = env->GetStringUTFChars(path1, 0);
    const char* str2 = env->GetStringUTFChars(path2, 0);
    //不能直接把const char*型作为imread()的实参,因此将char *str1复制到cap1中
    char cap1[128];
    char cap2[128];
    strcpy(cap1, str1);
    strcpy(cap2, str2);
    //printf(cap1);
    //printf(cap2);
    cv::Mat im1 = cv::imread(cap1);
    cv::Mat im2 = cv::imread(cap2);
    int predictout = image(im1, im2);
    return predictout;
}

头文件的代码:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_action_upLoadAction */

#ifndef _Included_com_action_upLoadAction
#define _Included_com_action_upLoadAction
#ifdef __cplusplus
extern "C" {
#endif
    /*
    * Class:     com_action_upLoadAction
    * Method:    Contrast
    * Signature: (Ljava/lang/String;Ljava/lang/String;)I
    */
    JNIEXPORT jint JNICALL Java_com_action_upLoadAction_Contrast
        (JNIEnv *env, jobject obj, jstring path1, jstring path2);

#ifdef __cplusplus
}
#endif
#endif

制作好的dll要放在tomcat的E:\tom1\apache-tomcat-7.0.78_x86\bin
目录下还有这几个数据文本也是

你可能感兴趣的:(struts2,dll编译,jni,java-web,tomcat)