admin
2021-03-13 3c853e808d9e617662fd4ce207d609daa8681df3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.lcjian.library.emotion;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.content.Context;
import android.util.Log;
 
public class EmotionHandler {
 
    public static Map<String,String> faceMap = new LinkedHashMap<String,String>();
    
    private static void prepareFaceMap(String arg) {
        try {
            JSONObject root = new JSONObject(arg);
            JSONArray array = root.getJSONArray("emotions");
            if(array == null || array.length() == 0)
                return;
            for(int i=0; i < array.length(); i ++) {
                JSONObject pair = array.getJSONObject(i);
                faceMap.put(pair.optString("emotionString"), pair.optString("fileName"));
            }
            
        } catch (JSONException e) {
            Log.e("error", e.getMessage());
        }
        
        
    }
 
    public static void initEmotion(Context context) {
        InputStream is;
        try {
            is = context.getAssets().open("emotion_des.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sbr = new StringBuilder();
            String str;
            while((str = br.readLine())!= null) {
                sbr.append(str);
            }
            prepareFaceMap(sbr.toString());
        } catch (IOException e) {
            Log.e("error", e.getMessage());
        }
    }
}