微信小程式,與百度人臉庫對比,並讀出使用者資料
這是微信wxml的程式碼,構建出一個照相機,並且可以實現前後轉換攝像頭
<!--pages/camera/camera.wxml--> <!-- <text>pages/camera/camera.wxml</text> --> <camera device-position="{{status}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> <view class="weui-btn-area"> <button class="weui-btn" type="primary" bindtap="takePhoto">拍照 </button> </view> <switch checked bindchange='switch1Change'/>
以下是js程式碼,實現轉換攝像頭與訪問介面
// pages/camera/camera.js Page({ /** * 頁面的初始資料 */ data: { path:null, status:'front' }, switch1Change:function(e){ if (e.detail.value) { this.setData({ status: 'back' }) } else { this.setData({ status: 'front'}) } }, /** * 生命週期函式--監聽頁面載入 */ onLoad: function (options) { this.ctx=wx.createCameraContext() }, takePhoto(){ this.ctx.takePhoto({ quality: 'high', success: (res) => { console.log(res.tempImagePath); this.setData({ path: res.tempImagePath}) wx.uploadFile({ url: 'login', //僅為示例,非真實的介面地址 filePath: this.data.path, name: 'file', success: (res)=> { // var data = res.data; // var msg=JSON.parse(data); // console.log(msg); wx.showModal({ title: '提示', content: res.data, }) } }) } }) }, startRecord(){ this.ctx.startRecord({ success: (res) => { console.log('startRecord') } }) }, stopRecord(){ this.ctx.stopRecord({ success: (res) => { this.setData({ src: res.tempThumbPath, videoSrc:res.tempVideoPath }) } }) }, /** * 生命週期函式--監聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命週期函式--監聽頁面顯示 */ onShow: function () { }, /** * 生命週期函式--監聽頁面隱藏 */ onHide: function () { }, /** * 生命週期函式--監聽頁面解除安裝 */ onUnload: function () { }, /** * 頁面相關事件處理函式--監聽使用者下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函式 */ onReachBottom: function () { }, /** * 使用者點選右上角分享 */ onShareAppMessage: function () { } })
這是我後臺程式碼,如果不是從微信小程式進入,是訪問不到使用者資料的。
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { const HOST=''; public function _initialize(){ if(!session('?openid') || !session('?session_key')){ die('非法訪問'); } } //初始化人臉 private function init_face(){ $APP_ID=''; $API_KEY=''; $SECRET_KEY=''; $dir=APP_PATH.'/facesdk/'; require_once $dir.'AipFace.php'; return new \Aipface($APP_ID,$API_KEY,$SECRET_KEY); } public function index($no,$name,$sex,$age){ $data['no']=$no; $data['name']=$name; $data['sex']=$sex; $data['age']=$age; $model = M('rl_students'); if ($model->where("no='{$no}'")->find()) { return $this->ajaxReturn(array('error'=>true,'msg'=>'學號重複!')); }else{ $id=$model->add($data); if($id){ return $this->ajaxReturn(array('error'=>false,'id'=>$id)); }else{ return $this->ajaxReturn(array('error'=>true,'msg'=>'添加出錯')); } } } public function upload($id=''){ if(empty($id)){ return false; } $no=M("rl_students")->where("id={$id}")->getField('no'); $dir="./Uploads/studentface/";//上傳檔案路徑 $upload = new \Think\Upload();// 例項化上傳類 $upload->maxSize = 2048000 ;// 設定附件上傳大小2m $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別 $upload->rootPath = $dir; // 設定附件上傳根目錄 //$upload->savePath = ''; // 設定附件上傳(子)目錄 $upload->saveName=$no; $upload->replace=true; $upload->autoSub=false; // 上傳檔案 $info = $upload->uploadOne($_FILES['file']); if(!$info) {// 上傳錯誤提示錯誤資訊 return json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE); }else{// 上傳成功 $file=$dir.$info['savepath'].$info['savename']; $image=base64_encode(file_get_contents($file)); $this->facevalid($no,$image); $m=M('rl_head'); $data=$m->where("no='{$no}'")->find(); if($data){ $m->where("no={$no}")->save(array('base64'=>$image,'path'=>$file)); }else{ $m->add(array('no'=>$no,'base64'=>$image,'path'=>$file)); } // return $this->ajaxReturn(array('error'=>false,'msg'=>$info['savepath'].$info['savename'],'id'=>$id)); echo "採集圖片成功"; } } public function show($condition=''){ // echo session('openid'); // exit; //安全認證 if(!session('?openid') || !session('?session_key')){ die('非法訪問'); } $where=array(); if(!empty($condition)){ $where['s.no|s.name']=$condition; } $data=M('rl_students') ->alias('s') ->join('rl_head h on s.no=h.no','LEFT') ->field('s.id,s.no,s.name,s.sex,s.age,h.path,h.base64') ->order('id') ->where($where) ->page($_GET['page'],6) ->select(); foreach ($data as &$row) { $path=$row['path']; if(!empty($path)){ if(!file_exists($path)){ $dir=dirname($path); mkdir($dir,0777,true); file_put_contents($path, base64_decode($row['base64'])); } $row['path']=self::HOST .ltrim($path,'.'); } unset($row['base64']); } $this->ajaxReturn($data); } //獲取小組 private function face_group(){ //組名 $groupname='test'; $client=$this->init_face(); $ret=$client->getGroupList(); if($ret['error_code']==0){ $grouplist=$ret['result']['group_id_list']; if(in_array($groupname, $grouplist)){ return $groupname; }else{ $ret=$client->groupAdd($groupname); if($ret['error_code']==0){ return $groupname; }else{ return false; } } }else{ return false; } } //刪除組 private function del_facegroup(){ $client=$this->init_face(); $ret=$client->groupDelete('test'); print_r($ret); } //人臉檢測併入庫 public function facevalid($no,$image){ // $file='./Uploads/2.jpg'; // if(!file_exists($file)){ // die('檔案不存在'); // } // $image=base64_encode(file_get_contents($file)); //如果有可選引數 $options=array(); //$options['face_field']="age"; $options["max_face_num"]=2; // $options["face_type"]="LIVE"; $client=$this->init_face(); $ret=$client->detect($image,'BASE64',$options); // print_r($ret); if($ret['error_code']==0){ //有人臉 $result=$ret['result']; $face_num=$result['face_num']; if(1==$face_num){//人臉數量為1 $face_probability=$result['face_list'][0]['face_probability']; if(1==$face_probability){ // $guid=myguid(); $group=$this->face_group(); // echo $group; // exit; $client->addUser($image,'BASE64',$group,$no); echo "人臉檢測完成,並已入庫"; }else{ die('可靠性:'.$face_probability); } }else{ die('人臉數量大於1'); } }else{ die('沒有人臉'); exit; } } public function login(){ $dir="./Uploads/temp/";//上傳檔案路徑 if(!file_exists($dir)){ mkdir($dir,0777,true); } $upload = new \Think\Upload();// 例項化上傳類 $upload->maxSize = 2048000 ;// 設定附件上傳大小2m $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別 $upload->rootPath = $dir; // 設定附件上傳根目錄 $upload->savePath=''; $upload->autoSub=false; $info=$upload->uploadOne($_FILES['file']); if(!$info){ return json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE); }else{ // echo "111"; // exit; $file=$dir.$info['savepath'].$info['savename']; $image=base64_encode(file_get_contents($file)); // echo $image; // exit; $client=$this->init_face(); $options['liveness_control']='NORMAL'; $options['max_user_num']=1; $ret=$client->search($image,'BASE64','test',$options); // echo json_encode($ret,JSON_UNESCAPED_UNICODE); // exit; if($ret['error_code']==0){ $user=$ret['result']['user_list'][0]; $no=$user['user_id']; $score=$user['score']; if(!empty($no)){ $data=M('rl_students')->field('no,name,sex')->where("no='{$no}'")->find(); // $this->ajaxReturn($data); echo json_encode($data,JSON_UNESCAPED_UNICODE); } } } } }
<?php
namespace Home\Controller;
use Think\Controller;
class SessionController extends Controller {
const HOST='';
public function session($code){
if(empty($code)) return false;
$appid="";
$secret="";
$url="https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
$ret=https_get($url);
// echo $ret;
// exit;
$arr=json_decode($ret,true);
print_r($arr);
if(isset($arr['openid']) && isset($arr['session_key'])){
session('openid',$arr['openid']);
session('session_key',$arr['session_key']);
// $session_id=session_id();
// echo $session_id;
}else{
die($ret);
}
}
}
相關推薦
微信小程式,與百度人臉庫對比,並讀出使用者資料
這是微信wxml的程式碼,構建出一個照相機,並且可以實現前後轉換攝像頭<!--pages/camera/camera.wxml--> <!-- <text>pages/camera/camera.wxml</text> -->
微信小程式(利用百度雲後臺進行人臉識別)
takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { t
微信小程式之獲取百度天氣
能夠獲取天氣的開放平臺有很多,比如高德地圖、百度地圖、YY天氣等,這裡我們主要講述一下呼叫百度API實時獲取天氣的方式。 獲取金鑰之後,點選左側的“配置環境”, 根據其指引下載得到 bmap-xw.js 檔案,並放在小程式專案中(新建libs資料夾,與pages資
看這一篇就夠啦!微信小程式入門與實戰,橫掃常用元件API開發技巧(完整版包含全部原始碼)
第1章:什麼是微信小程式? 1 開篇及課程特色介紹 2 直觀感受一下微信小程式 3 小程式適合做什麼樣的應用 4 對開發者的影響 5 學習基礎 6 小作業 第2章:小程式環境大件與開發工具介紹 1 開篇介紹及下載工具 2 小程式目前情況及限制 3 小程式開發
微信小程式實現與後臺PHP互動
微信小程式實現與後臺PHP互動 接下來將講後臺如何與前臺進行資料及圖片之間的互動,相信這一點是很多人所關注的,因為當時我實在團隊中負責後臺開發,因此對前端不是特別瞭解,這裡我會貼出前端開發時的部分程式碼截圖,微信小程式的官方api介紹地址是: https://mp.weix
初嘗微信小程式開發與實踐
以後端程式設計師的視角來聊一聊小程式。 小程式的火熱相信不用我多說了,年初的時候老婆去浦東某達面試,甚至都被問有沒有小程式測試經驗。儼然小程式成為了網際網路公司自PC,WAP,安卓,IOS之後又一不可或缺的入口。正好這段時間公司也在做一款小程式,於是順便也學習了一把。當然因為我是一個後端猿,自然
解決微信小程式登入與釋出的一些問題
解決微信小程式的問題 圖片在電腦上顯示但在手機上卻無法顯示的問題 要使用的是本地圖片,不想把圖片上傳到網路再通過https的方式解決,解決方法如下: 1.image src中的圖片地址對英文字母大小寫敏感,必須確認大小寫對得上; 2.圖片大小問題。本地圖片要小於10KB才能順利在真機上顯示。 為什麼
微信小程式入門與實戰 常用元件 API 開發技巧 專案實戰
開始就以專案為出發點,不會講一大堆枯燥的語法再補充兩個案例了事,將帶你快速熟悉小程式基礎知識,然後直接進入實戰開發環節,將小程式的知識點貫穿在整個專案中 課程不僅僅講解小程式開發,更會通過實際的編碼來
微信小程式安卓手機訪問不到圖片,無法顯示圖片
關於微信小程式不顯示圖片 通病可能有以下幾個可能性: 非本地圖片:確定圖片資源存在,copy 圖片url再瀏覽器開啟,確定圖片資源存在且能正常訪問 本地圖片:確定相對路徑或者絕對路徑正確 微信小程式圖片路徑,不可以存在中文,使用英文做路徑和檔名 檔案字尾文,小寫。且保證正確 網路圖片,必須確保域名已經備案
微信小程式——文字沉底居中(0基礎開發者,好痛苦)
這幾天想把一段文字置於頁面底部,試了好多方法都不可以,今天終於成功了上傳一下成功經歷。 最開始的方法,只是沉底,但是無法居中。 .section_text{ position: fixed; bottom: 30rpx; left: 0;
微信小程式如何轉換成百度小程式
微信小程式轉換成百度小程式,git上都已經有大神出了程式,可以通過程式批量替換,但是沒玩過,不過我們也可以通過手動替換的方式,轉換成百度小程式。 一、修改檔案字尾 百度和微信的page是頁面數量一致都是js、css、html、json,替換方式為: wxml ——&g
微信小程式標籤與HTML的同異處
微信小程式 HTML view div text h1~h6 p span icon i input/ checkbox/ radio/ input標籤加type=‘各種對應的型別。。大家懂得’ picker+
微信小程式頁面跳轉傳遞引數(實體,物件)
我們要傳遞的實體是object型別 queryItemClick: function (e) { var that = this //拿到點選的index下標 var index = e.currentTarget.dataset.index //將物件轉為st
微信小程式開發——websocket(socket線上測試伺服器,或本地socket伺服器)
WeChat小程式交流(QQ群:769977169)效果圖程式碼示例xxx.wxml<button bindtap='startClick'>連線</button> <bu
微信小程式rpx與px如何換算
rpx單位是微信小程式中css的尺寸單位,rpx可以根據螢幕寬度進行自適應。 規定螢幕寬為750rpx。如在 iPhone6 上,螢幕寬度為375px,共有750個物理畫素,則750rpx = 375px = 750物理畫素,1rpx = 0.5px 裝置
微信小程式中wxml和wxss的樣式說明,徹底搞定佈局排版
對於css不熟悉的Android程式設計師來說,開發微信小程式面臨的一個比較困難的問題就是介面的排版了。微信小程式的排版就跟wxml和wxss有關了,它們兩者相當於android的佈局檔案,其中wxml指定了介面的框架結構,而wxss指定了介面的框架及元素的顯示樣式。 一、wxml
微信小程式頂部用 scroll-view 元件橫向滾動,類似tab選項卡的效果,
<viewwx:for="{{navDate}}"class="{{curMeal===index ? 'selected-meal':'meal-time-item'}}"data-index="{{index}}"bindtap="selectMeal"data-id="{{item._id}}"&
【視訊分享】微信小程式入門與實戰
看到網友留言想要小程式的,想了想還是把這個提前吧,那幾個留言要小程式的記得讚賞嘿嘿。小程式官方正
微信小程式 本地資源圖片無法通過 WXSS 獲取,可以使用網路圖片,或者 base64,或者使用image標籤。
url('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QMvaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3p
微信小程式 路由跳轉傳參wx.navigateTo ,字串轉物件報錯
官方文件地址:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html (一)問題 看了文件之後,傳一個引數沒有問題,就是如果你路由跳轉想傳物件,必須將物件轉換成字串, J