|
|
|
|
|
|
|
|
package com.elink.aicare; |
|
|
package com.elink.aicare; |
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
/** |
|
|
/** |
|
|
* |
|
|
* |
|
|
*/ |
|
|
*/ |
|
|
public class BodyFatAlgorithmUtils { |
|
|
public class BodyFatAlgorithmUtils { |
|
|
|
|
|
|
|
|
public static void main(String args[]){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String args[]) throws Exception{ |
|
|
int sex=1; |
|
|
int sex=1; |
|
|
int age=24; |
|
|
int age=24; |
|
|
double weight = 60.5; |
|
|
double weight = 60.5; |
|
|
|
|
|
|
|
|
* @param height 身高;单位:cm,取值范围:0< height <=270 |
|
|
* @param height 身高;单位:cm,取值范围:0< height <=270 |
|
|
* @param adc 阻抗;取值范围:0 < adc <= 1000 |
|
|
* @param adc 阻抗;取值范围:0 < adc <= 1000 |
|
|
* @return |
|
|
* @return |
|
|
|
|
|
* @throws Exception |
|
|
*/ |
|
|
*/ |
|
|
public static BodyFatData getBodyFatData(int algorithmType,int sex, int age, double weight,int height, int adc){ |
|
|
public static BodyFatData getBodyFatData(int algorithmType,int sex, int age, double weight,int height, int adc){ |
|
|
|
|
|
|
|
|
if (age < 0 || age > 120){ |
|
|
|
|
|
System.out.println("年龄age取值不正确,取值范围:0<=age<=120 ;单位:年"); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
if (weight <= 0.0 && weight > 220.0){ |
|
|
|
|
|
System.out.println("体重weight取值不正确,取值范围:0.0< weight < 220 ;单位:kg"); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
if (height <= 0 && height > 270){ |
|
|
|
|
|
System.out.println("身高height取值不正确,取值范围:0< height <=270 ;单位:cm"); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
if (adc < 0 && adc > 1000){ |
|
|
|
|
|
System.out.println("阻抗adc取值不正确,取值范围:0 < adc <= 1000"); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
return getDataByAicare(sex, age, weight,height, adc); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
if (age < 0 || age > 120){ |
|
|
|
|
|
throw new Exception("年龄age取值不正确,取值范围:0<=age<=120 ;单位:年"); |
|
|
|
|
|
} |
|
|
|
|
|
if (weight <= 0.0 && weight > 220.0){ |
|
|
|
|
|
throw new Exception("体重weight取值不正确,取值范围:0.0< weight < 220 ;单位:kg"); |
|
|
|
|
|
} |
|
|
|
|
|
if (height <= 0 && height > 270){ |
|
|
|
|
|
throw new Exception("身高height取值不正确,取值范围:0< height <=270 ;单位:cm"); |
|
|
|
|
|
} |
|
|
|
|
|
if (adc < 0 && adc > 1000){ |
|
|
|
|
|
throw new Exception("阻抗adc取值不正确,取值范围:0 < adc <= 1000"); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
return getDataByAicare(sex, age, weight,height, adc); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 调用体脂算法库文件,计算对应的数据 |
|
|
|
|
|
* @param sex |
|
|
|
|
|
* @param age |
|
|
|
|
|
* @param weight |
|
|
|
|
|
* @param heightCM |
|
|
|
|
|
* @param adc |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
private static BodyFatData getDataByAicare(int sex, int age, double weight,int heightCM, int adc){ |
|
|
private static BodyFatData getDataByAicare(int sex, int age, double weight,int heightCM, int adc){ |
|
|
BodyFatAlgorithmAicare bodyFatAlgorithmAicare = BodyFatAlgorithmAicare.INSTANTCE; |
|
|
|
|
|
|
|
|
//调用体脂算法库,计算基础数据 |
|
|
|
|
|
BodyFatAlgorithmAicare bodyFatAlgorithmAicare = BodyFatAlgorithmAicare.INSTANTCE; |
|
|
double height = bodyFatAlgorithmAicare.getHeight(heightCM); |
|
|
double height = bodyFatAlgorithmAicare.getHeight(heightCM); |
|
|
double bmi = bodyFatAlgorithmAicare.getBMI(weight,height); |
|
|
double bmi = bodyFatAlgorithmAicare.getBMI(weight,height); |
|
|
double bm = bodyFatAlgorithmAicare.getBM(sex,bmi, height, age, adc, weight); |
|
|
double bm = bodyFatAlgorithmAicare.getBM(sex,bmi, height, age, adc, weight); |
|
|
|
|
|
|
|
|
int uvi = bodyFatAlgorithmAicare.getUVI( sex, weight, height, age, adc); |
|
|
int uvi = bodyFatAlgorithmAicare.getUVI( sex, weight, height, age, adc); |
|
|
int bmr = bodyFatAlgorithmAicare.getBMR( sex, weight, height, age, adc); |
|
|
int bmr = bodyFatAlgorithmAicare.getBMR( sex, weight, height, age, adc); |
|
|
int bodyAge = bodyFatAlgorithmAicare.getBodyAge( age, bmi, sex); |
|
|
int bodyAge = bodyFatAlgorithmAicare.getBodyAge( age, bmi, sex); |
|
|
//处理返回数据 |
|
|
|
|
|
|
|
|
//处理基础数据 |
|
|
BodyFatData fatData = new BodyFatData(); |
|
|
BodyFatData fatData = new BodyFatData(); |
|
|
fatData.setBmi(bmi); |
|
|
fatData.setBmi(bmi); |
|
|
fatData.setBfr(Math.round(bfr * 10) / 10.0); |
|
|
fatData.setBfr(Math.round(bfr * 10) / 10.0); |
|
|
|
|
|
|
|
|
fatData.setVwc(Math.floor(vwc * 10) / 10); |
|
|
fatData.setVwc(Math.floor(vwc * 10) / 10); |
|
|
fatData.setBodyAge(bodyAge); |
|
|
fatData.setBodyAge(bodyAge); |
|
|
fatData.setPp(Math.floor(pp * 10) / 10); |
|
|
fatData.setPp(Math.floor(pp * 10) / 10); |
|
|
|
|
|
//依据基础数据,计算其他体脂数据 |
|
|
|
|
|
double standardWeight = returnStandardWeight(sex, heightCM); |
|
|
|
|
|
double controlWeight = returnControlWeight(weight, standardWeight); |
|
|
|
|
|
double fat = returnFat(weight, bfr); |
|
|
|
|
|
double removeFatWeight = returnRemoveFatWeight(weight, fat); |
|
|
|
|
|
double muscleMass = returnMuscleMass(weight, rom); |
|
|
|
|
|
double protein = returnProtein(weight, pp); |
|
|
|
|
|
fatData.setStandardWeight(keepDecimal(standardWeight, 1)); |
|
|
|
|
|
fatData.setControlWeight(keepDecimal(controlWeight, 1)); |
|
|
|
|
|
fatData.setFat(keepDecimal(fat, 1)); |
|
|
|
|
|
fatData.setRemoveFatWeight(keepDecimal(removeFatWeight, 1)); |
|
|
|
|
|
fatData.setMuscleMass(keepDecimal(muscleMass, 1)); |
|
|
|
|
|
fatData.setProtein(keepDecimal(protein, 1)); |
|
|
|
|
|
BodyFatData.FatLevel fatLevel = returnFatLevel(weight, standardWeight); |
|
|
|
|
|
fatData.setFatLevel(fatLevel); |
|
|
return fatData; |
|
|
return fatData; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 标准体重(kg) |
|
|
|
|
|
* @param sex 性别 |
|
|
|
|
|
* @param height 身高 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double returnStandardWeight(int sex, int height) { |
|
|
|
|
|
double standardWeight = 0.0D; |
|
|
|
|
|
switch(sex) { |
|
|
|
|
|
case 1: |
|
|
|
|
|
standardWeight = (double)(height - 80) * 0.7D; |
|
|
|
|
|
break; |
|
|
|
|
|
case 2: |
|
|
|
|
|
standardWeight = (double)(height - 70) * 0.6D; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return standardWeight; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 体重控制量(kg) |
|
|
|
|
|
* @param curWeight 体重 |
|
|
|
|
|
* @param standardWeight 标准体重 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double returnControlWeight(double curWeight, double standardWeight) { |
|
|
|
|
|
return curWeight - standardWeight; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 脂肪量 |
|
|
|
|
|
* @param curWeight 体重 |
|
|
|
|
|
* @param bfr 脂肪 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double returnFat(double curWeight, double bfr) { |
|
|
|
|
|
return curWeight * bfr / 100.0D; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 去脂体重 |
|
|
|
|
|
* @param curWeight 体重 |
|
|
|
|
|
* @param fat 脂肪量 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double returnRemoveFatWeight(double curWeight, double fat) { |
|
|
|
|
|
return curWeight - fat; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 肌肉量 |
|
|
|
|
|
* @param curWeight 体重 |
|
|
|
|
|
* @param rom 肌肉率 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double returnMuscleMass(double curWeight, double rom) { |
|
|
|
|
|
return curWeight * rom / 100.0D; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 蛋白量 |
|
|
|
|
|
* @param curWeight 体重 |
|
|
|
|
|
* @param pp 蛋白率 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double returnProtein(double curWeight, double pp) { |
|
|
|
|
|
return curWeight * pp / 100.0D; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 肥胖等级 |
|
|
|
|
|
* @param curWeight 体重 |
|
|
|
|
|
* @param standardWeight 标准体重 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static BodyFatData.FatLevel returnFatLevel(double curWeight, double standardWeight) { |
|
|
|
|
|
double level = (curWeight - standardWeight) / standardWeight * 100.0D; |
|
|
|
|
|
if (level < -20.0D) { |
|
|
|
|
|
return BodyFatData.FatLevel.UNDER; |
|
|
|
|
|
} else if (level >= -20.0D && level < -10.0D) { |
|
|
|
|
|
return BodyFatData.FatLevel.THIN; |
|
|
|
|
|
} else if (level >= -10.0D && level <= 10.0D) { |
|
|
|
|
|
return BodyFatData.FatLevel.NORMAL; |
|
|
|
|
|
} else { |
|
|
|
|
|
return level > 10.0D && level <= 20.0D ? BodyFatData.FatLevel.OVER : BodyFatData.FatLevel.FAT; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 四舍五入 |
|
|
|
|
|
* @param d 值 |
|
|
|
|
|
* @param count 保留小数位 |
|
|
|
|
|
* @return double |
|
|
|
|
|
*/ |
|
|
|
|
|
private static double keepDecimal(double d, int count) { |
|
|
|
|
|
BigDecimal decimal = new BigDecimal(d); |
|
|
|
|
|
return decimal.setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |