Unity微信小游戏开启摄像头的问题
/*发现获取罗盘数值 “StartCompassOption” 是可以正常使用,可以获取到数值
但是摄像头 无法启动,麻烦帮忙看下是 哪里的问题
摄像头权限是 已经获取到,但是开启摄像头的方法 “Success”并未调用。
不用制作拍照和录像,只用获取到摄像头画面即可。
https://developers.weixin.qq.com/minigame/dev/api/media/camera/wx.createCamera.html
*/
public RawImage img_target;
private void Awake()
{
WX.InitSDK(InitCallBack);
WX.OnNeedPrivacyAuthorization((sub) =>
{
Debug.Log(sub);
});
WXInit();
}
public void WXInit()
{
AuthorizeOption ao = new AuthorizeOption();
ao.scope = "scope.camera";
WX.Authorize(ao);
//开启陀螺仪
sco = new StartCompassOption();
sco.success += Success;
sco.fail += Fail;
WX.StartCompass(sco);
WX.OnCompassChange((result) =>
{
Debug.Log(result.direction);
}
//开启摄像头
CreateCameraOption cco = new CreateCameraOption();
cco.devicePosition = "back";
cco.fail += Fail;
cco.success += Success;
WXCamera wxCam = WXBase.CreateCamera(cco);
wxCam.ListenFrameChange();
wxCam.OnCameraFrame((data) =>
{
Debug.Log("=====" + data.data.Length);
Texture2D texture = ConvertArrayBufferToTexture(data.data);
if (texture != null)
{
img_target.material.mainTexture = texture;
}
});
var a = WX.GetAppAuthorizeSetting();
Debug.Log("摄像头权限:" + a.cameraAuthorized);
}
public void InitCallBack(int i)
{
Debug.Log("WXInit Success!!-----------" + i);
}
public void Success(GeneralCallbackResult gg)
{
var a = WX.GetAppAuthorizeSetting();
Debug.Log("相机权限:" + a.cameraAuthorized);
}
public void Fail(GeneralCallbackResult gg)
{
Debug.LogError("失败!!");
}
Texture2D ConvertArrayBufferToTexture(byte[] buffer)
{
int width = 500;
int height = 500;
Texture2D texture = new Texture2D(width, height);
if (texture.LoadImage(buffer))
{
return texture;
}
return null;
}