package safeluck.drive.evaluation.util;
|
|
import android.content.res.Resources;
|
import android.graphics.PointF;
|
import android.os.SystemClock;
|
import android.util.Log;
|
import android.util.TypedValue;
|
|
import com.anyun.exam.lib.MyLog;
|
import com.anyun.exam.lib.util.ByteUtil;
|
import com.safeluck.aykj.utils.BytesUtils;
|
|
import java.math.BigDecimal;
|
import java.security.SecureRandom;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.Calendar;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Random;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKeyFactory;
|
import javax.crypto.spec.DESKeySpec;
|
|
import safeluck.drive.evaluation.bean.Point;
|
|
import static java.lang.Math.cos;
|
import static java.lang.Math.pow;
|
import static java.lang.Math.sin;
|
import static java.lang.Math.toRadians;
|
|
/**
|
* MyApplication2
|
* Created by lzw on 2019/3/18. 13:13:42
|
* 邮箱:632393724@qq.com
|
* All Rights Saved! Chongqing AnYun Tech co. LTD
|
*/
|
public class Utils {
|
private static final String TAG = "Utils";
|
public static float px2dp(float value){
|
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value, Resources.getSystem().getDisplayMetrics());
|
}
|
|
/**
|
* dp值转像素
|
* @param dpValue
|
* @return
|
*/
|
public static float dp2Px(int dpValue){
|
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,dpValue,Resources.getSystem().getDisplayMetrics());
|
}
|
|
/**
|
* 平台协议生成的校验码
|
* @param bytes
|
* @return
|
*/
|
|
public static byte calCheckCode(byte[] bytes){
|
byte checkCode =0;
|
// 参与运算的两个值,如果两个相应bit位相同,则结果为0,否则为1。
|
for (int i = 0; i < bytes.length; i++) {
|
checkCode ^= bytes[i];
|
}
|
|
return checkCode;
|
}
|
|
/**
|
* 转义
|
* 采用Ox7e表示,若校验码、消息头以及消息体中出现0x7e,则要进行转义处理,转义规则定义如下:
|
* 0x7e<——>0x7d后紧跟一个0x02;
|
* 0x7d<——>0x7d后紧跟一个0x01。
|
* @param datas
|
* @return
|
*/
|
public static byte[] transferMeaning(byte[] datas){
|
byte [] temp = new byte[datas.length*2];
|
int y = 0;
|
temp[y++] = 0x7e;
|
for (int i = 1; i < datas.length-1; i++) {
|
if (datas[i] == 0x7E) {
|
temp[y++] = 0x7D;
|
temp[y++] = 0x02;
|
} else if (datas[i] == 0x7D) {
|
temp[y++] = 0x7D;
|
temp[y++] = 0x01;
|
} else {
|
temp[y++] = datas[i];
|
}
|
}
|
temp[y++] = 0x7e;
|
|
byte[] tranferbytes = new byte[y];
|
System.arraycopy(temp,0,tranferbytes,0,y);
|
Log.i(TAG,"转义过后:"+ ByteUtil.byte2hex(tranferbytes));
|
return tranferbytes;
|
}
|
|
/**
|
*接收消息时:转义还原
|
* @param datas
|
* @return
|
*/
|
public static byte[] parseMsg(byte[] datas){
|
byte[] temp = new byte[datas.length];
|
int y =0 ;
|
for (int i = 0; i < datas.length; i++) {
|
if (datas[i]==0x7d && datas[i+1]==0x02 ){
|
temp[y++] = 0x7e;
|
i++;
|
continue;
|
}else if (datas[i]==0x7d && datas[i+1]==0x01 ){
|
|
temp[y++] = 0x7d;
|
i++;
|
continue;
|
}else{
|
temp[y++] = datas[i];
|
}
|
}
|
return temp;
|
}
|
|
public static boolean isDigital(String str) {
|
String regx= "^\\d+";
|
return str.matches(regx);
|
}
|
|
// public static void main(String []args){
|
// String str = "EB00020000031420010000000400A5";
|
//// String str = "7EEB00020000031420010000000400A5007E";
|
// String str1 = "EB000200000314200100000004030D00";
|
//// String str1 = "7EEB000200000314200100000004030D00D57E";
|
// String str2 = "EB000200000314200100000004035000";
|
// byte [] dtas=BytesUtils.hexStringToBytes(str);
|
// byte checkcode = calCheckCode(dtas);
|
// System.out.println(BytesUtils.toHexString(checkcode));
|
// }
|
|
|
|
public static int[] getRandomInts(int size,int randomMax){
|
Random random = new Random();
|
int[] a=new int[size];
|
int index=0;
|
|
while(index<size){
|
System.out.println("---------------");
|
int temp=random.nextInt(randomMax);
|
|
if(temp!=0&&!contains(a,temp)){
|
a[index++]=temp;
|
}
|
}
|
|
for(int i=0;i<a.length;i++){
|
System.out.println(a[i]);
|
}
|
|
|
return a;
|
|
}
|
|
|
|
//该方法完成判断temp在a数组中是否包含
|
//包含返回true
|
public static boolean contains(int[] a,int temp){
|
|
for(int i=0;i<a.length;i++){
|
if(a[i]==temp){
|
return true;
|
}
|
}
|
|
return false;
|
}
|
|
|
public static String getHHmm(){
|
String mMonth,mDay,mWay,mHours,mMinute;
|
StringBuffer stringBuffer = new StringBuffer();
|
Calendar calendar = Calendar.getInstance();
|
mMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1); //获取日期的月
|
mDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)); //获取日期的天
|
mWay = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)); //获取日期的星期
|
|
/**
|
* 如果小时是个位数
|
*
|
*则在前面价格“0”
|
* */
|
if (calendar.get(Calendar.HOUR) < 10) {
|
mHours = "0" + calendar.get(Calendar.HOUR);
|
|
} else {
|
mHours = String.valueOf(calendar.get(Calendar.HOUR));
|
}
|
stringBuffer.append(mHours);
|
stringBuffer.append(":");
|
/**
|
* 如果分钟是个位数
|
*
|
*则在前面价格“0”
|
* */
|
if (calendar.get(Calendar.MINUTE) < 10) {
|
mMinute = "0" + calendar.get(Calendar.MINUTE);
|
} else {
|
mMinute = String.valueOf(calendar.get(Calendar.MINUTE));
|
}
|
stringBuffer.append(mMinute);
|
/**
|
* 获取星期
|
* 并设置出来
|
* */
|
if ("1".equals(mWay)) {
|
mWay = "天";
|
} else if ("2".equals(mWay)) {
|
mWay = "一";
|
} else if ("3".equals(mWay)) {
|
mWay = "二";
|
} else if ("4".equals(mWay)) {
|
mWay = "三";
|
} else if ("5".equals(mWay)) {
|
mWay = "四";
|
} else if ("6".equals(mWay)) {
|
mWay = "五";
|
} else if ("7".equals(mWay)) {
|
mWay = "六";
|
}
|
return stringBuffer.toString();
|
}
|
|
|
/**
|
* 加密
|
* @param datasource byte[]
|
* @param password String
|
* @return byte[]
|
*/
|
public static byte[] encrypt(byte[] datasource, String password) {
|
try{
|
SecureRandom random = new SecureRandom();
|
DESKeySpec desKey = new DESKeySpec(password.getBytes());
|
//创建一个密匙工厂,然后用它把DESKeySpec转换成
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
SecretKey securekey = keyFactory.generateSecret(desKey);
|
//Cipher对象实际完成加密操作
|
Cipher cipher = Cipher.getInstance("DES");
|
//用密匙初始化Cipher对象
|
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
|
//现在,获取数据并加密
|
//正式执行加密操作
|
return cipher.doFinal(datasource);
|
}catch(Throwable e){
|
e.printStackTrace();
|
}
|
return null;
|
}
|
/**
|
* 解密
|
* @param src byte[]
|
* @param password String
|
* @return byte[]
|
* @throws Exception
|
*/
|
public static byte[] decrypt(byte[] src, String password) throws Exception {
|
// DES算法要求有一个可信任的随机数源
|
SecureRandom random = new SecureRandom();
|
// 创建一个DESKeySpec对象
|
DESKeySpec desKey = new DESKeySpec(password.getBytes());
|
// 创建一个密匙工厂
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
// 将DESKeySpec对象转换成SecretKey对象
|
SecretKey securekey = keyFactory.generateSecret(desKey);
|
// Cipher对象实际完成解密操作
|
Cipher cipher = Cipher.getInstance("DES");
|
// 用密匙初始化Cipher对象
|
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
|
// 真正开始解密操作
|
return cipher.doFinal(src);
|
}
|
|
public static int parseUnsignedInt(String s, int radix)
|
throws NumberFormatException {
|
if (s == null) {
|
throw new NumberFormatException("null");
|
}
|
|
int len = s.length();
|
if (len > 0) {
|
char firstChar = s.charAt(0);
|
if (firstChar == '-') {
|
throw new
|
NumberFormatException(String.format("Illegal leading minus sign " +
|
"on unsigned string %s.", s));
|
} else {
|
if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
|
(radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
|
return Integer.parseInt(s, radix);
|
} else {
|
long ell = Long.parseLong(s, radix);
|
if ((ell & 0xffff_ffff_0000_0000L) == 0) {
|
return (int) ell;
|
} else {
|
throw new
|
NumberFormatException(String.format("String value %s exceeds " +
|
"range of unsigned int.", s));
|
}
|
}
|
}
|
} else {
|
throw new NumberFormatException("For input string: \"" + s + "\"");
|
}
|
}
|
|
public static void main(String[] args){
|
long longCurrTIme = System.currentTimeMillis()/1000;
|
long CurrTIme = System.currentTimeMillis();
|
System.out.println("longCurrTIme="+longCurrTIme);
|
System.out.println("intCurrTIme="+CurrTIme);
|
int unsignedTime= parseUnsignedInt(String.valueOf(longCurrTIme),10);
|
System.out.println("unsigned int time = "+unsignedTime);
|
|
double[] map={
|
|
|
|
|
|
|
// 16579.3086, -7626.9764,16579.8354, -7627.7888, 16580.3393, -7628.4316, 16581.181, -7629.2701,16582.0667, -7629.9052,
|
// 16583.1605, -7630.4413,16584.3072, -7630.818, 16585.5416,
|
// -7630.993, 16586.918, -7630.9599, 16588.0948, -7630.7205,16589.1489, -7630.3119, 16590.3826, -7629.6211, 16591.2856, -7628.8977,16592.0383, -7628.0128,
|
//
|
// 16592.8408, -7626.7007, 16593.3354, -7625.4394,16593.886, -7624.4265, 16594.9656, -7623.4808,16596.0498, -7623.0286, 16597.3348, -7622.955,16598.4564, -7623.2286,
|
// 16599.4254, -7623.7931,16600.1166, -7624.5026,
|
};
|
|
for (int i = 0; i < map.length; i++) {
|
Arrays.asList(map[i]);
|
}
|
|
Point p1 = new Point(2.55,1.0);
|
Point p2 = new Point(1.55,1.0);
|
System.out.println(Calc3Point(p1,p2,1.0).toString());
|
}
|
|
public static Point Calc3Point(Point p1,Point p2,double L ){
|
Point p3 = new Point(0.0,0.0);
|
System.out.println(p1.toString()+" "+p2.toString());
|
if (isEqual(p1.getX(), p2.getX())) {
|
p3.setY( p2.getY());
|
if (p2.getY() > p1.getY()) {
|
p3.setX(p2.getX() - L);
|
} else {
|
p3.setX(p2.getX() + L) ;
|
}
|
return p3;
|
}
|
if (isEqual(p1.getY(), p2.getY())) {
|
p3.setX(p2.getX());
|
if (p2.getX() > p1.getX()) {
|
p3.setY(p2.getY()+L);
|
} else {
|
p3.setY(p2.getY() - L);
|
}
|
return p3;
|
}
|
|
double k = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
|
double b = p1.getY() - k*p1.getX();
|
|
double A = 1 + pow(k, 2);
|
double B = 2*k*(b - p2.getY()) - 2*p2.getX();
|
double C = pow(b - p2.getY(), 2) + pow(p2.getX(), 2) - pow(L,2);
|
|
double x3, y3;
|
|
|
if (p1.getX() < p2.getX()) {
|
x3 = (- B - Math.sqrt(pow(B, 2) - 4*A*C))/(2*A);
|
} else {
|
x3 = (- B + Math.sqrt(pow(B, 2) - 4*A*C))/(2*A);
|
}
|
y3 = k * x3 + b;
|
|
|
p3.setX(x3);
|
p3.setY(y3);
|
|
p3 = rotatePoint(p3, p2, 270);
|
|
|
return p3;
|
|
}
|
|
private static boolean isEqual(double a,double b){
|
return (Math.abs(a-b)<1e-3);
|
}
|
private static Point rotatePoint(Point oldPoint,Point centre,double degree){
|
|
Point newPoint = new Point(0.0,0.0);
|
newPoint.setX(getdouble((oldPoint.getX()-centre.getX())*cos(toRadians(degree)) -
|
(oldPoint.getY()-centre.getY())*sin(toRadians(degree)) + centre.getX(),6)) ;
|
newPoint.setY(getdouble( (oldPoint.getX()-centre.getX())*sin(toRadians(degree))
|
+ (oldPoint.getY()-centre.getY())*cos(toRadians(degree)) + centre.getY(),6));
|
return newPoint;
|
}
|
|
|
/**
|
* 对double保留几位小数
|
*/
|
public static Double getdouble(Double d,int reserve) {
|
|
BigDecimal b= new BigDecimal(d);
|
return b.setScale(reserve, BigDecimal.ROUND_HALF_UP).doubleValue();
|
}
|
|
public static String formatTimeYYMMDDHHmmSS(long begin_time) {
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
return simpleDateFormat.format(begin_time);
|
}
|
}
|