亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于微信小程序?qū)崿F(xiàn)透明背景人像分割功能

 更新時(shí)間:2022年10月08日 10:41:04   作者:摔跤貓子  
這篇文章主要介紹了基于小程序?qū)崿F(xiàn)透明背景人像分割,此文主要實(shí)現(xiàn)識(shí)別人體的輪廓范圍,與背景進(jìn)行分離并保存效果圖,適用于拍照背景替換及透明背景的人像圖(png格式)轉(zhuǎn)換,需要的朋友可以參考下

一、文章前言

此文主要實(shí)現(xiàn)識(shí)別人體的輪廓范圍,與背景進(jìn)行分離并保存效果圖,適用于拍照背景替換及透明背景的人像圖(png格式)轉(zhuǎn)換。

二、具體流程及準(zhǔn)備

2.1、注冊(cè)百度開(kāi)放平臺(tái)及微信公眾平臺(tái)賬號(hào)。
2.2、下載及安裝微信Web開(kāi)發(fā)者工具。
2.3、如需通過(guò)SDK調(diào)用及需準(zhǔn)備對(duì)應(yīng)語(yǔ)言的開(kāi)發(fā)工具。

三、開(kāi)發(fā)步驟

2.1、打開(kāi)微信開(kāi)發(fā)者工具,新建項(xiàng)目,選擇不使用模板、不使用云服務(wù)。

在這里插入圖片描述

2.2、在pages文件夾下面創(chuàng)建一個(gè)文件夾并新建對(duì)應(yīng)的page文件。

在這里插入圖片描述

2.3、在js的onLoad事件中請(qǐng)求獲取Token的接口,將接口返回access_token存儲(chǔ)到該頁(yè)的變量當(dāng)中,用于后續(xù)請(qǐng)求圖像分割的接口憑證。ApiKey和SecretKey建議密文保存。

/**
   * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
   */
  onLoad(options) {
    let that = this;
    let ApiKey='';
    let SecretKey='';
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + ApiKey+'&client_secret='+SecretKey,
      method: 'POST',
      success: function (res) {
        that.setData({
          AccessToken:res.data.access_token
        });
      }
    });
  },

2.4、隨后在wxml和js中實(shí)現(xiàn)對(duì)應(yīng)的選擇圖片及轉(zhuǎn)換base64的功能效果。

<view class="containerBox">
  <view class="leftBtn" bindtap="loadImage">上傳圖片</view>
  <view class="rightBtn" bindtap="identify">圖像轉(zhuǎn)換</view>
</view>
<view >
  <image src="{{choiceImg}}" class="showImg"></image>
  <image src="data:image/png;base64,{{imgBase64}}" class="showImg"></image>
</view>
let that = this;
    wx.chooseImage({
      success: function (res) {
        that.choiceImg = res.tempFilePaths[0];
        wx.getFileSystemManager().readFile({
          filePath:res.tempFilePaths[0],
          encoding:'base64',
          success(data){
            let baseData = data.data;
            that.setData({
              choiceImg: res.tempFilePaths[0],
              baseData:baseData
            })
          }
        });
      }
    });

2.5、給圖像轉(zhuǎn)換按鈕對(duì)應(yīng)的響應(yīng)事件中綁定開(kāi)放平臺(tái)接口,將參數(shù)進(jìn)行拼接傳遞。

在這里插入圖片描述

    let that = this;
    wx.request({
      url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=' + that.data.AccessToken,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        image:that.data.baseData
      },
      success: function (identify) {
        that.setData({
          imgBase64: identify.data.foreground
      })
      }
    })

2.6、根據(jù)接口返回的數(shù)據(jù)來(lái)看,我們先取foreground也就是分割后的人像前景摳圖進(jìn)行展示。

在這里插入圖片描述

2.7、能夠成功將解析后的圖片進(jìn)行展示后,我們將返回的base64格式的圖片進(jìn)行處理然后保存到本地,就可以得到一個(gè)透明背景的png,我們可以自己對(duì)這個(gè)圖片進(jìn)行上色、PS等操作。

//獲取文件管理器對(duì)象
    const fs = wx.getFileSystemManager()
    //文件保存路徑
    const Imgpath = wx.env.USER_DATA_PATH + '/qrcodeImg' + '.png'
    //base64圖片文件
    let imageSrc = this.data.imgBase64.replace(/^data:image\/\w+;base64,/, '')
    //寫(xiě)入本地文件
    fs.writeFile({
      filePath: Imgpath,
      data: imageSrc,
      encoding: 'base64',
      success(res) {
        //保存到手機(jī)相冊(cè)
        wx.saveImageToPhotosAlbum({
          filePath: Imgpath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          },
          fail: function(err) {
          }
        })
      }
    })

在這里插入圖片描述

四、完整代碼

<!--index.wxml-->
<view class="containerBox">
  <view class="leftBtn" bindtap="loadImage">上傳圖片</view>
  <view class="rightBtn" bindtap="identify">圖像轉(zhuǎn)換</view>
</view>
<view >
  <image src="{{choiceImg}}" class="showImg"></image>
  <image src="data:image/png;base64,{{imgBase64}}" class="showImg"></image>
</view>
<view class="saveBtn" bindtap="saveBtn">保存圖片</view>
<!--index.wxss-->
.containerBox{
  width:750rpx;
  display:flex;
  height:62rpx;
  margin-top:20rpx;
}
.leftBtn{
  width:181rpx;
  height:62rpx;
  color:#4FAFF2;
  border:1rpx solid #4FAFF2;
  border-radius:10rpx;
  text-align: center;
  line-height:62rpx;
  font-size:28rpx;
  margin-left: 158rpx;
}
.rightBtn{
  width:181rpx;
  height:62rpx;
  color:white;
  border:1rpx solid #4FAFF2;
  border-radius:10rpx;
  text-align: center;
  line-height:62rpx;
  font-size:28rpx;
  margin-left: 73rpx;
  background:#4FAFF2;
}
.showImg{
  width:415rpx;
  height:415rpx;
  margin-left:167rpx;
  margin-top:25rpx;
  border-radius:20rpx;
}
.result{
  margin-top:20rpx;
}
.resultTitle{
  margin-left:75rpx;
}
.productTableTr{
  height: 80rpx;line-height: 80rpx;border-bottom: 1rpx solid #F8F8F8;display:flex;
}
.leftTr{
  width: 283rpx;height: 80rpx;line-height: 80rpx;
}
.rightTr{
  width: 419rpx;height: 80rpx;line-height: 80rpx;color: #FF2525;font-size: 26rpx;
}
.leftTrText{
  color: #2B79F5;font-size: 28rpx;margin-left: 15rpx;width: 283rpx;
}
.productDetailTable{
  width: 702rpx;margin-left: 24rpx;border:1rpx solid #F8F8F8;border-radius: 6rpx;
}
.saveBtn{
  width:181rpx;
  height:62rpx;
  color:white;
  border:1rpx solid #4FAFF2;
  border-radius:10rpx;
  text-align: center;
  line-height:62rpx;
  font-size:28rpx;
  margin-left: 284rpx;
  background:#4FAFF2;
  margin-top:20rpx;
}
  identify(){
    let that = this;
    wx.request({
      url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?access_token=' + that.data.AccessToken,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        image:that.data.baseData
      },
      success: function (identify) {
        that.setData({
          imgBase64: identify.data.foreground
      })
      }
    })
  },
  saveBtn(){
    //獲取文件管理器對(duì)象
    const fs = wx.getFileSystemManager()
    //文件保存路徑
    const Imgpath = wx.env.USER_DATA_PATH + '/qrcodeImg' + '.png'
    //base64圖片文件
    let imageSrc = this.data.imgBase64.replace(/^data:image\/\w+;base64,/, '')

    //寫(xiě)入本地文件
    fs.writeFile({
      filePath: Imgpath,
      data: imageSrc,
      encoding: 'base64',
      success(res) {
        //保存到手機(jī)相冊(cè)
        wx.saveImageToPhotosAlbum({
          filePath: Imgpath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          },
          fail: function(err) {
          }
        })
      }
    })

  },

到此這篇關(guān)于基于小程序?qū)崿F(xiàn)透明背景人像分割的文章就介紹到這了,更多相關(guān)小程序透明背景人像分割內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論