single classify

This commit is contained in:
ScorpioMiku
2018-10-06 17:55:42 +08:00
parent dd780cdb72
commit 1817d66cc5
21 changed files with 302 additions and 27 deletions

View File

@@ -20,7 +20,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
@@ -31,43 +35,61 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//butterknife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//logger
implementation 'com.orhanobut:logger:2.2.0'
//超帅侧拉ui
implementation 'com.mxn.soul:flowingdrawer-core:2.0.0'
implementation 'com.nineoldandroids:library:2.4.0'
//图片形状库
implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
//cardview
implementation 'com.android.support:cardview-v7:26.1.0'
//tablayout
implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.0@aar'
//viewpager transforms
implementation 'com.ToxicBakery.viewpager.transforms:view-pager-transforms:1.2.32@aar'
//个人信息的那个条条
implementation 'com.akexorcist:RoundCornerProgressBar:2.0.3'
//wave
implementation 'me.itangqi.waveloadingview:library:0.3.5'
//计步
implementation project(':todaystepcounterlib')
//打分ui
implementation 'com.github.CB-ysx:CBRatingBar:3.0.1'
//折线
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
//searchview
implementation 'com.miguelcatalan:materialsearchview:1.4.0'
//floatbuttom
implementation 'com.nightonke:boommenu:2.1.1'
//recycler and card
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'
//混合式recyclerview
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//翻页view
implementation 'com.github.czy1121:turncardlistview:1.0.0'
//油表盘
implementation 'com.github.lzyzsd:circleprogress:1.2.1'
@@ -80,10 +102,14 @@ dependencies {
//okhttp 网络通信库
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
// json解析库
implementation 'com.google.code.gson:gson:2.8.5'
//横向picker
compile 'com.github.adityagohad:HorizontalPicker:1.0.1'
implementation 'com.github.adityagohad:HorizontalPicker:1.0.1'
implementation files('libs/YoudaoBase.jar')
implementation files('libs/YoudaoCnDictOffline.jar')
implementation files('libs/YoudaoTranslateOffline.jar')
implementation files('libs/YoudaoTranslateOnline.jar')
}

BIN
app/libs/YoudaoBase.jar Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -19,3 +19,13 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-ignorewarnings
-libraryjars libs/ YoudaoBase.jar
-libraryjars libs/ YoudaoCnDictOffline.jar
-libraryjars libs/ YoudaoTranslateOnline.jar
-libraryjars libs/ YoudaoTranslateOffline.jar
-keep class com.youdao.sdk.ydtranslate.** { *;}
-keep class com.youdao.sdk.chdict.** { *;}
-keep class com.youdao.localtransengine.** { *;}
-keep class com.youdao.sdk.ydonlinetranslate.** { *;}

View File

@@ -59,7 +59,8 @@
<!-- android:name=".LoginActivity" -->
<!-- android:label="@string/title_activity_login" /> -->
<activity android:name=".modules.addinformation.AddInformationActivity" />
<activity android:name=".modules.classifyresult.DishResultActivity"></activity>
<activity android:name=".modules.classifyresult.DishResultActivity" />
<activity android:name=".modules.classifyresult.MaterialResultActivity"></activity>
</application>
</manifest>

View File

@@ -13,6 +13,8 @@ import com.example.ninefourone.nutritionmaster.utils.WebUtil;
import com.google.gson.Gson;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.youdao.sdk.app.YouDaoApplication;
import com.youdao.sdk.chdict.ChDictor;
import org.json.JSONObject;
@@ -44,6 +46,7 @@ public class NutritionMaster extends Application {
super.onCreate();
mInstance = this;
init();
initYouDao();
}
/**
@@ -150,7 +153,6 @@ public class NutritionMaster extends Application {
*/
private void initBD() {
ConstantUtils.BD_ACCESS_TOKEN = getAccessToken();
// Logger.d(ConstantUtils.BD_ACCESS_TOKEN);
}
@@ -187,10 +189,8 @@ public class NutritionMaster extends Application {
/**
* 返回结果示例
*/
// System.err.println("result:" + result);
JSONObject jsonObject = new JSONObject(result);
String access_token = jsonObject.getString("access_token");
// Logger.d(access_token);
return access_token;
} catch (Exception e) {
e.printStackTrace();
@@ -198,5 +198,12 @@ public class NutritionMaster extends Application {
return null;
}
/**
* YD init
*/
private void initYouDao() {
YouDaoApplication.init(this, ConstantUtils.YOUDAO_APPKEY);
}
}

View File

@@ -20,8 +20,8 @@ public class ClassifyResult implements Serializable {
private String imgPath;
private double probability;
private String name;
private double calorie;
private Boolean has_calorie;
private double calorie = 0;
private Boolean has_calorie = false;
private double quantity = -1;
private FoodMenu foodMenu;

View File

@@ -20,10 +20,19 @@ import android.widget.TextView;
import com.example.ninefourone.nutritionmaster.R;
import com.example.ninefourone.nutritionmaster.bean.ClassifyResult;
import com.example.ninefourone.nutritionmaster.modules.classifyresult.DishResultActivity;
import com.example.ninefourone.nutritionmaster.modules.classifyresult.MaterialResultActivity;
import com.example.ninefourone.nutritionmaster.utils.ConstantUtils;
import com.example.ninefourone.nutritionmaster.utils.MaterialClassifier;
import com.example.ninefourone.nutritionmaster.utils.MessageUtils;
import com.example.ninefourone.nutritionmaster.utils.WebUtil;
import com.orhanobut.logger.Logger;
import com.youdao.sdk.app.Language;
import com.youdao.sdk.app.LanguageUtils;
import com.youdao.sdk.ydonlinetranslate.Translator;
import com.youdao.sdk.ydtranslate.Translate;
import com.youdao.sdk.ydtranslate.TranslateErrorCode;
import com.youdao.sdk.ydtranslate.TranslateListener;
import com.youdao.sdk.ydtranslate.TranslateParameters;
import org.json.JSONArray;
import org.json.JSONObject;
@@ -171,27 +180,30 @@ public class ClassifierCamera extends AppCompatActivity {
public void run() {
String result = null;
try {
if (code == 0) {
result = WebUtil.HttpPost(ConstantUtils.BD_DISH_URL,
ConstantUtils.BD_ACCESS_TOKEN, param);
} else if (code == 1) {
if (code == MATERAIL_CODE) {
MaterialClassifier materialClassifier = new MaterialClassifier();
JSONObject jsonObject = materialClassifier.plantDetect(data);
result = jsonObject.getJSONArray("objects")
.getJSONObject(0).getString("value");
translate(result);
} else if (code == DISH_CODE) {
result = WebUtil.HttpPost(ConstantUtils.BD_DISH_URL,
ConstantUtils.BD_ACCESS_TOKEN, param);
JSONObject jsonObject = new JSONObject(result);
ClassifyResult classifyResult = new ClassifyResult();
JSONArray resultObject = jsonObject.getJSONArray("result");
jsonObject = resultObject.getJSONObject(0);
classifyResult.setCalorie(jsonObject.getInt("calorie"));
classifyResult.setHas_calorie(jsonObject.getBoolean("has_calorie"));
classifyResult.setProbability(jsonObject.getDouble("probability"));
classifyResult.setName(jsonObject.getString("name"));
classifyResult.getMenu();
resultList.add(classifyResult);
refreshUI();
} else {
Logger.e("拍照code为-1");
}
JSONObject jsonObject = new JSONObject(result);
ClassifyResult classifyResult = new ClassifyResult();
JSONArray resultObject = jsonObject.getJSONArray("result");
jsonObject = resultObject.getJSONObject(0);
classifyResult.setCalorie(jsonObject.getInt("calorie"));
classifyResult.setHas_calorie(jsonObject.getBoolean("has_calorie"));
classifyResult.setProbability(jsonObject.getDouble("probability"));
classifyResult.setName(jsonObject.getString("name"));
classifyResult.getMenu();
// Logger.d(classifyResult);
resultList.add(classifyResult);
refreshUI();
} catch (Exception e) {
e.printStackTrace();
}
@@ -220,9 +232,15 @@ public class ClassifierCamera extends AppCompatActivity {
cameraCoverLinearlayout.setVisibility(View.VISIBLE);
break;
case R.id.more_takephoto_ok:
Intent intent = new Intent(ClassifierCamera.this, DishResultActivity.class);
Intent intent;
if (code == DISH_CODE) {
intent = new Intent(ClassifierCamera.this, DishResultActivity.class);
} else {
intent = new Intent(ClassifierCamera.this, MaterialResultActivity.class);
}
intent.putExtra("LIST", resultList);
// intent.putExtra("LIST", ConstantUtils.testData);
startActivity(intent);
resultList.clear();
refreshUI();
@@ -281,4 +299,41 @@ public class ClassifierCamera extends AppCompatActivity {
}
});
}
/**
* YOUDAO
*
* @param english
*/
private void translate(String english) {
Language languageFrom = LanguageUtils.getLangByName("英文");
Language languageTo = LanguageUtils.getLangByName("中文");
TranslateParameters tps = new TranslateParameters.Builder()
.source("NM")
.from(languageFrom).to(languageTo).build();
Translator translator = Translator.getInstance(tps);
translator.lookup(english, "5c0e8adb488ba180", new TranslateListener() {
@Override
public void onError(TranslateErrorCode translateErrorCode, String s) {
}
@Override
public void onResult(Translate translate, String s, String s1) {
String result = "";
result = translate.getTranslations().get(0);
// Logger.d(result);
ClassifyResult classifyResult = new ClassifyResult();
classifyResult.setName(result);
resultList.add(classifyResult);
refreshUI();
}
@Override
public void onResult(List<Translate> list, List<String> list1, List<TranslateErrorCode> list2, String s) {
}
});
}
}

View File

@@ -0,0 +1,31 @@
package com.example.ninefourone.nutritionmaster.modules.classifyresult;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.ninefourone.nutritionmaster.R;
import com.example.ninefourone.nutritionmaster.base.BaseActivity;
public class MaterialResultActivity extends BaseActivity {
@Override
public int getLayoutId() {
return R.layout.activity_material_result;
}
@Override
public void initViews(Bundle savedInstanceState) {
}
@Override
public void initToolBar() {
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}

View File

@@ -341,4 +341,6 @@ public class ConstantUtils {
}
return arrayList;
}
public static String YOUDAO_APPKEY = "3ea8061348a6bdd6";
}

View File

@@ -0,0 +1,134 @@
package com.example.ninefourone.nutritionmaster.utils;
import android.util.Log;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import javax.net.ssl.SSLException;
/**
* Created by ScorpioMiku on 2018/10/6.
*/
public class MaterialClassifier {
private String TAG = "face";
final String api_url = "https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject";
final String api_key = "waTeK75Yeqd4YsnyaXRgik_PAJ8bUMea";
final String api_secret = "aQpFps5C-gqncIENA5ruPTwhV6fL9Rit";
private HashMap<String, String> map = new HashMap<>();
private HashMap<String, byte[]> byteMap = new HashMap<>();
public JSONObject plantDetect(byte[] image) {
String str = "没有训练该样本";
JSONObject re = null;
map.put("api_key", api_key);
map.put("api_secret", api_secret);
map.put("objects", "value,confidence");
// file = new File(path);
byteMap.put("image_file", image);
try {
byte[] bacd = post(api_url, map, byteMap);
str = new String(bacd);
re = new JSONObject(str);
Log.d(TAG, "plantDetect: " + re);
} catch (Exception e) {
e.printStackTrace();
}
return re;
}
private final static int CONNECT_TIME_OUT = 30000;
private final static int READ_OUT_TIME = 50000;
private static String boundaryString = getBoundary();
protected static byte[] post(String url, HashMap<String, String> map, HashMap<String, byte[]> fileMap) throws Exception {
HttpURLConnection conne;
URL url1 = new URL(url);
conne = (HttpURLConnection) url1.openConnection();
conne.setDoOutput(true);
conne.setUseCaches(false);
conne.setRequestMethod("POST");
conne.setConnectTimeout(CONNECT_TIME_OUT);
conne.setReadTimeout(READ_OUT_TIME);
conne.setRequestProperty("accept", "*/*");
conne.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);
conne.setRequestProperty("connection", "Keep-Alive");
conne.setRequestProperty("user-agent", "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)");
DataOutputStream obos = new DataOutputStream(conne.getOutputStream());
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry) iter.next();
String key = entry.getKey();
String value = entry.getValue();
obos.writeBytes("--" + boundaryString + "\r\n");
obos.writeBytes("Content-Disposition: form-data; name=\"" + key
+ "\"\r\n");
obos.writeBytes("\r\n");
obos.writeBytes(value + "\r\n");
}
if (fileMap != null && fileMap.size() > 0) {
Iterator fileIter = fileMap.entrySet().iterator();
while (fileIter.hasNext()) {
Map.Entry<String, byte[]> fileEntry = (Map.Entry<String, byte[]>) fileIter.next();
obos.writeBytes("--" + boundaryString + "\r\n");
obos.writeBytes("Content-Disposition: form-data; name=\"" + fileEntry.getKey()
+ "\"; filename=\"" + encode(" ") + "\"\r\n");
obos.writeBytes("\r\n");
obos.write(fileEntry.getValue());
obos.writeBytes("\r\n");
}
}
obos.writeBytes("--" + boundaryString + "--" + "\r\n");
obos.writeBytes("\r\n");
obos.flush();
obos.close();
InputStream ins = null;
int code = conne.getResponseCode();
try {
if (code == 200) {
ins = conne.getInputStream();
} else {
ins = conne.getErrorStream();
}
} catch (SSLException e) {
e.printStackTrace();
return new byte[0];
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[4096];
int len;
while ((len = ins.read(buff)) != -1) {
baos.write(buff, 0, len);
}
byte[] bytes = baos.toByteArray();
ins.close();
return bytes;
}
private static String getBoundary() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 32; ++i) {
sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".charAt(random.nextInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_".length())));
}
return sb.toString();
}
private static String encode(String value) throws Exception {
return URLEncoder.encode(value, "UTF-8");
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ninefourone.nutritionmaster.modules.classifyresult.MaterialResultActivity">
</android.support.constraint.ConstraintLayout>