javaWeb開發實現多圖上傳和調用C++Deeplearning算法進行圖片對比檢測

首先實現前臺界面,上傳圖片並保存到指定路徑,而後java製做dll,利用jni調用C++代碼,將保存的圖片路徑傳給dll處理,並返回一個處理結果。css

整個環境以下:
window10 x64
jdk1.7 x86,tomcat7.0 x86
myeclipse 2014 32位
struts-2.3.16.3html

第一步:利用struts2實現多圖片上傳,界面以下:java

clipboard.png

項目的目錄結構以下:web

clipboard.png

首先下載struts2,並在struts-2.3.16.3apps找到struts2-blank.war,將其解壓,而後把WEB-INF目錄下的全部jar包複製到這,以下圖:spring

clipboard.png

在src文件夾右鍵,新創建一個struts.xml(這裏用直接複製struts目錄下的struts.xml到項目下,編譯器會報錯,具體緣由我也沒弄清楚),具體步驟如圖:apache

clipboard.png

clipboard.png

struts.xml內容:segmentfault

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    "http://struts.apache.org/dtds/struts-2.3.dtd">  
      
<struts>  
   
  <!-- 該屬性指定須要Struts2處理的請求後綴,該屬性的默認值是action,即全部匹配*.action的請求都由Struts2處理。
        若是用戶須要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。 -->
    <constant name="struts.action.extension" value="do" />
    <!-- 設置瀏覽器是否緩存靜態內容,默認值爲true(生產環境下使用),開發階段最好關閉 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 當struts的配置文件修改後,系統是否自動從新加載該文件,默認值爲false(生產環境下使用),開發階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 開發模式下使用,這樣能夠打印出更詳細的錯誤信息 -->
    <constant name="struts.devMode" value="true" />
    <!-- 默認的視圖主題 -->
    <constant name="struts.ui.theme" value="simple" />
    <!--<constant name="struts.objectFactory" value="spring" />-->
    <!--解決亂碼    -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.multipart.maxSize" value="10701096"/>
    
    <package name="struts2" extends="struts-default">
        <action name="upLoad" class="com.action.upLoadAction" method="execute">
            <!-- 要建立/image文件夾,不然會報找不到文件 -->
            <param name="savePath">/image</param>
            <result name="success">index.jsp</result>
        </action>
    </package>
   
       
</struts>

在WEB-INF目錄下新建web.xml以下圖:數組

clipboard.png

clipboard.png

web.xml內容:瀏覽器

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>Test</display-name>  
    
  <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    </filter>  
  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
      
  <welcome-file-list>  
    <welcome-file>index.html</welcome-file>  
    <welcome-file>index.htm</welcome-file>  
    <welcome-file>index.jsp</welcome-file>  
    <welcome-file>default.html</welcome-file>  
    <welcome-file>default.htm</welcome-file>  
    <welcome-file>default.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>

而後在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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'upLoad.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
        <!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
        <!-- ${pageContext.request.contextPath}/upload1/upload1.do -->
        <!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
        <!--  -->
        <form action="${pageContext.request.contextPath}/upLoad.do" enctype="multipart/form-data" method="post">
            文件1:<input type="file" name="image"><br/>
            文件2:<input type="file" name="image"><br/>
                <input type="submit" value="上傳" />
        </form>
    </body>
</html>

index.jsp代碼以下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'message.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    上傳成功
  <br/>
  <s:debug></s:debug>
  </body>
</html>

到此爲止,圖片就可上傳成功了
其中還有就是要注意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 <jni.h>
/* 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
目錄下還有這幾個數據文本也是

clipboard.png

相關文章
相關標籤/搜索