using System;
using System.Threading;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
namespace HDL_ON.UI
{
public class FacePassagePage : FrameLayout
{
FrameLayout bodyView;
FrameLayout contentView;
Button btnCommunityAccessControlIcon;
Button btnSetFaceId;
Button btnEraseData;
#region 图标选择部分图标
///
/// 背景图选项区域
///
FrameLayout pictureOptionView;
///
/// 背景图选项选择区域
///
VerticalScrolViewLayout optionView;
///
/// 拍照按钮
///
Button btnTakePicture;
///
/// 相册按钮
///
Button btnAlbum;
///
/// 取消按钮
///
Button btnCancel;
#endregion
///
/// imageHeight
/// OutputYSize 400代表Y分辨率高的值,所以最终得到图片的分辨率为:266*400
/// 0代表不指定不压缩,直接保存裁剪后的图片,目前只对iOS有效 Android设置小于0的话默认800
///
#if __IOS__
const int imageHeight = 0;
#else
const int imageHeight = 400;
#endif
///
/// 住户详情
///
CustomerObj customerObj = new CustomerObj();
public FacePassagePage()
{
bodyView = this;
}
public void LoadPage()
{
new TopViewDiv(bodyView, Language.StringByID(StringId.FacePassage)).LoadTopView();
bodyView.BackgroundColor = CSS_Color.BackgroundColor;
contentView = new FrameLayout()
{
Y = Application.GetRealHeight(64),
Height = Application.GetRealHeight(667 - 64),
};
bodyView.AddChidren(contentView);
var topView = new FrameLayout()
{
Height = Application.GetRealHeight(189),
BackgroundColor = CSS_Color.MainBackgroundColor,
};
contentView.AddChidren(topView);
var btnFaceIcon = new Button()
{
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealHeight(20),
Width = Application.GetRealWidth(102),
Height = Application.GetRealWidth(102),
UnSelectedImagePath = "PersonalCenter/FacePassage/FaceIcon.png",
};
topView.AddChidren(btnFaceIcon);
var btnText1 = new Button()
{
X = Application.GetRealWidth(16),
Y = Application.GetRealWidth(54),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextID = StringId.ApplyFaceIdTo,
IsBold = true,
};
topView.AddChidren(btnText1);
topView.AddChidren(new Button() { Height = 1, BackgroundColor = CSS_Color.DividingLineColor, Y = Application.GetRealHeight(188), X = Application.GetRealWidth(16), Width = Application.GetRealWidth(359) });
#region 社区门禁通行
var communityRow = new FrameLayout()
{
Y = topView.Bottom,
Height = Application.GetRealHeight(50),
BackgroundColor = CSS_Color.MainBackgroundColor,
};
contentView.AddChidren(communityRow);
var btnCommunityTitle = new Button()
{
X = Application.GetRealWidth(16),
Width = Application.GetRealWidth(300),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.TextFontSize,
TextID = StringId.CommunityAccessControl,
};
communityRow.AddChidren(btnCommunityTitle);
btnCommunityAccessControlIcon = new Button()
{
X = Application.GetRealWidth(314),
Gravity = Gravity.CenterVertical,
Width = Application.GetMinRealAverage(48),
Height = Application.GetMinRealAverage(36),
UnSelectedImagePath = "Public/Switch.png",
SelectedImagePath = "Public/SwitchOn.png",
};
communityRow.AddChidren(btnCommunityAccessControlIcon);
btnCommunityAccessControlIcon.MouseUpEventHandler = (sender, e) =>
{
if (customerObj.faceClose == 3)//没有人脸数据不能开启人脸通行功能
{
return;
}
btnCommunityAccessControlIcon.IsSelected = !btnCommunityAccessControlIcon.IsSelected;
int switchStatus = btnCommunityAccessControlIcon.IsSelected == true ? 1 : 2;
//刷新回调
Action action = () =>
{
initButtonStatus();
};
editFaceInfo(switchStatus, action);
};
#endregion
var btnTip = new Button()
{
Y = Application.GetRealHeight(270),
Height = Application.GetRealHeight(48),
TextAlignment = TextAlignment.Center,
TextColor = CSS_Color.MainColor,
TextSize = CSS_FontSize.TextFontSize,
TextID = StringId.AboutFaceIdAndPrivacy
};
contentView.AddChidren(btnTip);
btnSetFaceId = new Button()
{
Y = Application.GetRealHeight(539),
Gravity = Gravity.CenterHorizontal,
Width = Application.GetRealWidth(220),
Height = Application.GetRealWidth(44),
Radius = (uint)Application.GetRealWidth(22),
BackgroundColor = CSS_Color.MainColor,
TextID = StringId.SetFace,
TextSize = CSS_FontSize.SubheadingFontSize,
TextColor = CSS_Color.MainBackgroundColor,
TextAlignment = TextAlignment.Center,
IsBold = true,
};
contentView.AddChidren(btnSetFaceId);
btnSetFaceId.MouseUpEventHandler = (sender, e) =>
{
LoadPictureOptionView();
};
//抹掉数据
btnEraseData = new Button()
{
Y = Application.GetRealHeight(481),
Gravity = Gravity.CenterHorizontal,
Width = Application.GetRealWidth(220),
Height = Application.GetRealWidth(44),
Radius = (uint)Application.GetRealWidth(22),
BackgroundColor = CSS_Color.WarningColor,
TextID = StringId.EraseData,
TextSize = CSS_FontSize.SubheadingFontSize,
TextColor = CSS_Color.MainBackgroundColor,
TextAlignment = TextAlignment.Center,
IsBold = true,
};
btnEraseData.MouseUpEventHandler = (sender, e) =>
{
Action action = () =>
{
initButtonStatus();
};
editFaceInfo(3, action);
};
ReadFaceInfo();
}
///
/// 获取人脸信息
///
void ReadFaceInfo(bool isLoading = true)
{
Loading waitPage = null;
if (isLoading)
{
waitPage = new Loading();
waitPage.Start();
}
new Thread(() =>
{
try
{
var hsr = new HttpServerRequest();
var pack = hsr.GetCustomerInfo();
if (pack != null)
{
if (pack.Code == StateCode.SUCCESS)
{
var cus = Newtonsoft.Json.JsonConvert.DeserializeObject(pack.Data.ToString());
if (cus != null)
{
customerObj = cus;
Application.RunOnMainThread(() =>
{
initButtonStatus();
});
}
}
}
}
catch (Exception ex)
{
MainPage.Log($"读取人脸信息异常:{ex.Message}");
}
finally
{
Application.RunOnMainThread(() =>
{
if (waitPage != null)
{
waitPage.RemoveFromParent();
waitPage = null;
}
});
}
})
{ IsBackground = true }.Start();
}
///
/// 清除人脸数据
///
void editFaceInfo(int status, Action action)
{
var waitPage = new Loading();
waitPage.Start();
new Thread(() =>
{
try
{
var hsr = new HttpServerRequest();
var pack = hsr.EditFaceFunction(status);
if (pack != null)
{
if (pack.Code == StateCode.SUCCESS)
{
customerObj.faceClose = status;
}
else
{
Application.RunOnMainThread(() =>
{
IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
});
}
}
}
catch (Exception ex)
{
MainPage.Log($"清除人脸信息异常:{ex.Message}");
}
finally
{
Application.RunOnMainThread(() =>
{
if (waitPage != null)
{
waitPage.RemoveFromParent();
waitPage = null;
}
action?.Invoke();
});
}
})
{ IsBackground = true }.Start();
}
///
/// 初始化底部按钮
///
void initButtonStatus()
{
if (customerObj.faceClose == 1 || customerObj.faceClose == 2)//人脸数据已经存在1:人脸通行开启;2:关闭
{
if (customerObj.faceStatus == 2)//人脸数据下发门口机成功
{
btnCommunityAccessControlIcon.IsSelected = customerObj.faceClose == 1;//人脸通行功能是否开启
contentView.AddChidren(btnEraseData);
btnSetFaceId.TextID = StringId.ResetFace;
return;
}
}
btnCommunityAccessControlIcon.IsSelected = false;
btnEraseData.RemoveFromParent();
btnSetFaceId.TextID = StringId.SetFace;
}
///
/// 加载图标选择选项
///
void LoadPictureOptionView()
{
var pView = new FrameLayout()
{
BackgroundColor = CSS_Color.DialogTransparentColor1,
};
bodyView.AddChidren(pView);
pictureOptionView = new FrameLayout()
{
Y = Application.GetRealHeight(445 + 50),
Height = Application.GetRealHeight(250),
AnimateSpeed = 0.3f,
Animate = Animate.DownToUp,
};
pView.AddChidren(pictureOptionView);
optionView = new VerticalScrolViewLayout()
{
Gravity = Gravity.CenterHorizontal,
Width = Application.GetRealWidth(343),
Height = Application.GetRealHeight(100),
BackgroundColor = CSS_Color.MainBackgroundColor,
Radius = (uint)Application.GetRealWidth(12),
};
pictureOptionView.AddChidren(optionView);
btnTakePicture = new Button()
{
Height = Application.GetRealHeight(50),
TextAlignment = TextAlignment.Center,
TextColor = CSS_Color.TextualColor,
SelectedTextColor = CSS_Color.MainColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextID = StringId.TakePicture,
};
optionView.AddChidren(btnTakePicture);
optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
btnAlbum = new Button()
{
Height = Application.GetRealHeight(50),
TextAlignment = TextAlignment.Center,
TextColor = CSS_Color.TextualColor,
SelectedTextColor = CSS_Color.MainColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextID = StringId.Album,
};
optionView.AddChidren(btnAlbum);
optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
btnCancel = new Button()
{
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealHeight(8) + optionView.Bottom,
Width = Application.GetRealWidth(343),
Height = Application.GetRealHeight(50),
BackgroundColor = CSS_Color.MainBackgroundColor,
Radius = (uint)Application.GetRealWidth(12),
TextID = StringId.Cancel,
TextColor = CSS_Color.WarningColor,
TextSize = CSS_FontSize.SubheadingFontSize,
};
pictureOptionView.AddChidren(btnCancel);
LoadEvent_PictureOptionViewEventList(pView);
}
///
/// 加载背景图选择区域事件列表
///
void LoadEvent_PictureOptionViewEventList(FrameLayout pView)
{
pictureOptionView.MouseUpEventHandler = (sender, e) =>
{
pictureOptionView.Parent.RemoveFromParent();
};
pView.MouseUpEventHandler = (sender, e) =>
{
pictureOptionView.Parent.RemoveFromParent();
};
btnCancel.MouseUpEventHandler = (sender, e) =>
{
pictureOptionView.Parent.RemoveFromParent();
};
btnTakePicture.MouseDownEventHandler = (sender, e) =>
{
btnTakePicture.IsSelected = true;
};
btnTakePicture.MouseUpEventHandler = (sender, e) =>
{
btnTakePicture.IsSelected = false;
var imageName = Guid.NewGuid().ToString();
CropImage.TakePicture((imagePath) =>
{
CropImageCallBack(imagePath, 1, imageName);
}, imageName, 4, 6, imageHeight);
pictureOptionView.Parent.RemoveFromParent();
};
btnAlbum.MouseDownEventHandler = (sender, e) =>
{
btnAlbum.IsSelected = true;
};
btnAlbum.MouseUpEventHandler = (sender, e) =>
{
btnAlbum.IsSelected = false;
//从相册选择图片裁剪
var imageName = Guid.NewGuid().ToString();
//var imageName = scene.sid;
CropImage.SelectPicture((imagePath) =>
{
CropImageCallBack(imagePath, 2, imageName);
}, imageName, 4, 6, imageHeight);
pictureOptionView.Parent.RemoveFromParent();
};
}
///
/// 裁剪完照片回调,统一处理
///
/// 裁剪后的真实路径
/// 照片来源;1:拍照;2:图库
void CropImageCallBack(string selectImagePath, int imageSource, string imageName)
{
if (string.IsNullOrEmpty(selectImagePath) == true)
{
return;
}
//上传成功到回调
Action uploadSuccessAction = (isSuccess) =>
{
//点击重新录入事件
Action action = () =>
{
if (!isSuccess)
{
if (imageSource == 1)
{
CropImage.TakePicture((imagePath) =>
{
CropImageCallBack(imagePath, 2, imageName);
}, imageName, 4, 6, imageHeight);
}
else
{
CropImage.SelectPicture((imagePath) =>
{
CropImageCallBack(imagePath, 1, imageName);
}, imageName, 4, 6, imageHeight);
}
}
};
if (isSuccess)
{
ReadFaceInfo(false);
}
Application.RunOnMainThread(() =>
{
var page = new FaceSettingResultPage(action);
MainPage.BasePageView.AddChidren(page);
page.LoadPage(isSuccess);
MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
});
};
//上传图片到云端
UploadImage(selectImagePath, uploadSuccessAction);
}
///
/// 上传图片方法
///
/// 裁剪后的图片路径
///
///
void UploadImage(string selectImagePath, Action uploadResultAction)
{
try
{
//1.读取裁剪后的图片,然后删除
var imageBytes = Shared.IO.FileUtils.ReadFile(selectImagePath);
System.IO.File.Delete(selectImagePath);
var waitPage = new Loading();
bodyView.AddChidren(waitPage);
waitPage.Start(Language.StringByID(StringId.PleaseWait));
//开始上传
new Thread(() =>
{
try
{
string base64string = Convert.ToBase64String(imageBytes);
//Utlis.WriteLine("上传图片Length:" + imageBytes.Length + " base64:" + base64string);
var pack = new HttpServerRequest().FaceSetting(base64string);
if (pack != null)
{
Utlis.WriteLine("上传结果:" + pack.message);
uploadResultAction?.Invoke(pack.Code == StateCode.SUCCESS);
}
}
catch (Exception ex)
{
}
finally
{
Application.RunOnMainThread(() =>
{
if (waitPage != null)
{
waitPage.RemoveFromParent();
waitPage = null;
}
});
}
})
{ IsBackground = true }.Start();
}
catch { }
}
}
}