UNPKG

w-puppeteer-uitest

Version:
130 lines (90 loc) 3.71 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>compareB64.mjs - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> <script src="scripts/nav.js" defer></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav > <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="w-puppeteer-uitest.html">w-puppeteer-uitest</a><ul class='methods'><li data-type='method'><a href="w-puppeteer-uitest.html#.build">build</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.compareB64">compareB64</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.expBuild">expBuild</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.expTest">expTest</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.getB64">getB64</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.getFiles">getFiles</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.readJson">readJson</a></li><li data-type='method'><a href="w-puppeteer-uitest.html#.test">test</a></li></ul></li></ul> </nav> <div id="main"> <h1 class="page-title">compareB64.mjs</h1> <section> <article> <pre class="prettyprint source linenums"><code>import Jimp from 'jimp' /** * 陣列數值比較 * * @private * @param {Array} arr1 輸入陣列1 * @param {Array} arr2 輸入陣列2 * @returns {Number} 回傳相似度0~1 */ function arrCompare(arr1, arr2) { if (arr1.length !== arr2.length) { return 0 } let rr = 0 let n = arr1.length for (let i = 0, len = arr1.length; i &lt; len; i++) { let r = Math.abs(arr1[i] - arr2[i]) rr += r / 255 } rr = 1 - Math.pow(rr / n, 1 / 3) //rr=1-Math.sqrt(rr/n) return rr } /** * 圖片base64轉ImageData * * @private * @param {String} b64 * @returns {Promise} 回傳Promise,resolve為ImageData的陣列,reject為處理失敗 */ async function b642imgdata(b64) { let b = Buffer.from(b64, 'base64') let image = await Jimp.read(b) return image.bitmap.data } /** * 2張圖片base64比對 * * @memberOf w-puppeteer-uitest * @param {String} b64_a 輸入圖片a的base64格式字串 * @param {String} b64_b 輸入圖片b的base64格式字串 * @returns {Promise} 回傳Promise,resolve為相似度0~1,reject為處理失敗 */ async function compareB64(b64_a, b64_b) { let data_a = await b642imgdata(b64_a) let data_b = await b642imgdata(b64_b) return arrCompare(data_a, data_b) } export default compareB64 </code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sat Mar 23 2024 23:37:02 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/polyfill.js"></script> <script src="scripts/linenumber.js"></script> </body> </html>