【发布时间】:2021-11-20 20:44:54
【问题描述】:
我正在尝试将 openCv.js 导入到 next.js 应用中
一个。该项目是通过以下方式创建的:
npx create-next-app
b.然后安装:
$ yarn add @techstark/opencv-js
c。导入opencv:
import cv from "@techstark/opencv-js"
d。 只有上一行就可以报错:
./node_modules/@techstark/opencv-js/dist/opencv.js:30:1175 模块 未找到:无法解析“fs”
f。正如我所读到的,将以下行添加到 webpack.config.js 不会尝试解析 fs
module.exports = {
resolve: {
modules: [...],
fallback: {
fs: false,
path: false,
crypto: false
}
}
};
但它似乎不起作用,并且错误仍在继续。
g.但是使用 react(而不是 next.js)创建一个非常相似的项目:
create-react-app
$ yarn add @techstark/opencv-js
相同的代码(很少适应反应)可以正常工作 我可以毫无问题地使用opencv.js的功能
h。我的问题:
- 有没有办法在浏览器(客户端)中加载 opencv.js 而无需解析 fs?
- 为什么它在 react 中有效,而在 next.js 中无效?
这是 next.js 应用程序的“index.js”有错误:
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import React,{useRef, useState} from 'react'
import Script from 'next/script'
//---
import dynamic from 'next/dynamic'
import cv from "@techstark/opencv-js"
export default function Home() {
const canvas1 = useRef()
const img1 = useRef()
function handleFileChange(e) {
const files = e.target.files
if (!files || files.length === 0) {
return
}
const file = files[0]
var fr = new FileReader()
fr.readAsDataURL(file)
var img = img1.current
fr.onload = (evt) => {
if( evt.target.readyState === FileReader.DONE) {
img.src = evt.target.result
}
}
}
function handleImageLoad() {
var img=img1.current
img2canvas(img,canvas1.current)
img.className = ""
}
function img2canvas(img, canvas) {
var ctx = canvas.getContext('2d')
var ratioImage = img.naturalWidth / img.naturalHeight
var widthAdj = 400 //canvas.width
var heightAdj = Math.ceil(widthAdj / ratioImage)
canvas.width = widthAdj //(img.width/2)
canvas.height =heightAdj // (img.height/2)
ctx.width = widthAdj + 'px' //(img.width/2) + 'px'
ctx.height = heightAdj + 'px' //(img.height/2) + 'px'
ctx.drawImage(img, 0, 0, widthAdj, heightAdj)
}
async function testOpenCv() {
var img = img1.current
var mat = cv.imread(img)
await cv.cvtColor(mat,mat, openCv.COLOR_RGBA2GRAY)
await cv.bitwise_not(mat, mat)
await cv.imshow(canvas1.current, mat)
}
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1>Test</h1>
<div>
<label class="btn">file: </label>
<input id="file" type="file" onChange={handleFileChange} />
<br />
<button type="button" onClick={testOpenCv}>Test OpenCv!</button>
<br />
<br />
</div>
<div >
<img src="" alt="testimg" ref={img1} onLoad={handleImageLoad} style={{display:"none"}}/>
<br />
</div>
<div>
<canvas ref={canvas1}></canvas>
</div>
</main>
</div>
)
}
这是 React 应用的 App.js(工作正常)
import logo from './logo.svg';
import './App.css';
import cv from "@techstark/opencv-js";
import React,{useRef, useState} from 'react';
function App() {
const canvas1 = useRef()
const img1 = useRef()
function handleFileChange(e) {
const files = e.target.files;
if (!files || files.length === 0) {
return
}
const file = files[0];
var fr = new FileReader();
fr.readAsDataURL(file);
var img = img1.current
fr.onload = (evt) => {
if( evt.target.readyState === FileReader.DONE) {
img.src = evt.target.result;
}
}
}
function handleImageLoad() {
var img=img1.current
img2canvas(img,canvas1.current)
img.className = ""
}
function img2canvas(img, canvas) {
var ctx = canvas.getContext('2d')
var ratioImage = img.naturalWidth / img.naturalHeight;
var widthAdj = 400 //canvas.width;
var heightAdj = Math.ceil(widthAdj / ratioImage)
canvas.width = widthAdj //(img.width/2);
canvas.height =heightAdj // (img.height/2);
ctx.width = widthAdj + 'px' //(img.width/2) + 'px';
ctx.height = heightAdj + 'px' //(img.height/2) + 'px';
ctx.drawImage(img, 0, 0, widthAdj, heightAdj);
}
async function testOpenCv() {
var img = img1.current
var mat = cv.imread(img)
await cv.cvtColor(mat,mat, cv.COLOR_RGBA2GRAY);
await cv.bitwise_not(mat, mat)
await cv.imshow(canvas1.current, mat);
}
return (
<div className="App">
<header className="App-header">
<h1>Test</h1>
<div>
<label class="btn">file: </label>
<input id="file" type="file" onChange={handleFileChange} />
<br />
<button type="button" onClick={testOpenCv}>Test OpenCv!</button>
<br />
<br />
</div>
<div >
<img src="" alt="testimg" ref={img1} onLoad={handleImageLoad} style={{display:"none"}}/>
<br />
</div>
<div>
<canvas ref={canvas1}></canvas>
</div>
</header>
</div>
);
}
export default App;
【问题讨论】:
-
在 Next.js 中,应将自定义 webpack 配置添加到
next.config.js文件中。