请在2024 Clojure状态调查!分享您的想法。

欢迎!请参阅关于页面以获取更多关于此工作方式的信息。

0投票
ClojureScript

我需要使用这个库https://github.com/silvia-odwyer/photon,并且我使用这个示例https://github.com/minimal-xyz/minimal-shadow-cljs-nodejs作为起点,并希望复制此块代码以进行测试

   var fs = require('fs');

var photon = require("@silvia-odwyer/photon-node");

const fetch = require('node-fetch');
global.fetch = fetch;

function grayscaleImage() {
    // read file, then convert to base64
    var base64 = fs.readFileSync(`input.png`, { encoding: 'base64' });
    let data = base64.replace(/^data:image\/(png|jpg);base64,/, "");

    // convert base64 to PhotonImage
    var phtn_img = photon.PhotonImage.new_from_base64(data);

    photon.grayscale(phtn_img);

    // get base64 from filtered image, and write 
    let output_base64 = phtn_img.get_base64();
    let output_image_name = "output.png";
    var output_data = output_base64.replace(/^data:image\/\w+;base64,/, '');

    fs.writeFile(output_image_name, output_data, {encoding: 'base64'}, function(err) {
    });
    console.log(`Saved ${output_image_name}`);

}

grayscaleImage();

因此我写了这个

(ns server.main
  (:require ["@silvia-odwyer/photon-node" :as sop]
                    ["fs" :as fs]
                    ["node-fetch" :as nf]
                    [clojure.string :as string]))

(defn main! []
  (let [base64 (fs/readFileSync "input.png" #js {:encoding "base64"})
        data (.replace base64 #"^data:image/(png|jpg);base64," "")
        phtn-img (sop/PhotonImage.new_from_base64 data)
        phtn-img (sop/grayscale phtn-img)
        output-base64 (sop/get_base64 phtn-img)]
    (println output-base64)))

但是当我运行
‘shadow-cljs release app’和‘node target/main.js’
它抛出了

    C:\Users\usuario\desktop\clojure\minimal-shadow-cljs-nodejs\target\mai
n.js:230
Vc.prototype.Z=B;Vc.prototype.J=function(a,b){return ob(b,"()")};ra.pr
ototype.Z=B;ra.prototype.J=function(a,b,c){return Te(this,Se,b,c)};Ie.
prototype.Z=B;Ie.prototype.J=function(a,b,c){return Ke(b,Se,"("," ",")
",c,this)};tc.prototype.Z=B;tc.prototype.J=function(a,b,c){return Ke(b
,Se,"("," ",")",c,this)};"undefined"!==typeof console&&za();za();var y
a=new Qb(null,"print-length","print-length",1931866356),sa=new Qb(null
,"flush-on-newline","flush-on-newline",-151457939),ua=new Qb(null,"met
a","meta",1499536964),Ve=new Qb(null,"fallback-impl","fallback-impl",-
1501286995),ta=new Qb(null,"readably","readably",1129599760),Ue=new Qb
(null,"alt-impl","alt-impl",670969595),va=new Qb(null,"dup","dup",5562
98533),Le=new Qb(null,"more-marker","more-marker",-14717935);var af=re
quire("@silvia-odwyer/photon-node");var cf=require("fs");require("node
-fetch");pd(function(){$e(qc(["App loaded!"]));$e(qc(["asdasd"]));var
a=cf.readFileSync("input.png",{encoding:"base64"}).replace(/^data:imag
e\/(png|jpg);base64,/,"");a=af.PhotonImage.new_from_base64(a);a=af.gra
yscale(a);a=af.get_base64(a).replace(/^data:image\/\w+;base64,/,"");re
turn $e(qc([a]))},process.argv.slice(2));

TypeError: af.get_base64 is not a function
    at Function.<anonymous> (C:\Users\usuario\desktop\clojure\minimal-
shadow-cljs-nodejs\target\main.js:230:1066)
    at pd (C:\Users\usuario\desktop\clojure\minimal-shadow-cljs-nodejs
\target\main.js:112:412)
    at C:\Users\usuario\desktop\clojure\minimal-shadow-cljs-nodejs\tar
get\main.js:230:850
    at Object.<anonymous> (C:\Users\usuario\desktop\clojure\minimal-sh
adow-cljs-nodejs\target\main.js:231:3)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1
035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/r
un_main.js:60:12)
    at internal/main/run_main_module.js:17:47

说get_base64不是一个函数...
文档:https://docs.rs/photon-rs/0.2.0/photon_rs/struct.PhotonImage.html#method.get_base64

不知道node-fetch是否与此有关

我做错了吗?

1 答案

0投票

(sop/get_base64 phtn-img) 应该是 (.get_base64 phtn-img)

您可以使用此应用程序查看如何将JS调用转换为cljs

https://roman01la.github.io/javascript-to-clojurescript/

头像
好的,但是为什么?这不是一个Photon函数吗?或者是JavaScript吗?
另一方面,当我应用灰度效果时,该函数返回nil而不是对象,你知道如何获取该对象本身吗?我尝试了Object.values,但是不起作用。
...