浏览器该如何调用c或者c++

在做一个图像识别的项目, 需要通过浏览器去调用同事写的识别算法, 不用说你们也知道, 肯定是底层的, c或c++的.

浏览器直接调用c肯定不现实, 这个我清楚. 但是绕个弯, node.js是可以调用c, 这样只要通过本地启用nodejs服务或者用nw.js封装web就可以把锅甩给nodejs.

根据我目前掌握的信息来看, node.js可以调用c但也不是随意调用, 需要重写c, 使之成为nodejs的c扩展.类似于这个样子:

Handle<Value> Method(const Arguments& args) {
  //code here
}

这样就比较尴尬了, 我所需要调用的库较为庞大, 重写起来也不是件容易的事,
而且, 我也写不了c啊 (〒︿〒).

我该如何去解决这个问题呢?

或许

  1. 一定是我走的方向不对, 不应该拿浏览器调用c, 但如何通过

  2. 有什么黑科技能将c转成js

  3. 只需要重写c的接口就可以解决

老司机带带我~ 囧rz

阅读 16.7k
11 个回答

大家给的建议基本都是可以实现的, 我后来翻了翻调用的第三方库, 发现它其实本身就是基于c编译的js, 用的Emscipten, 所以准备尝试一下, 这个应该是最最不破坏原有结构的做法了.

  1. 将浏览器的那个图片上传到服务器

  2. 服务器上,不论是PHP,或者是Node,都可以有导入SO、DLL的方法,并且有执行系统exe、bin文件的方法

  3. 通过SO、DLL识别了之后,返回结果给客户端

你的同事应该生成一个dll/so/bin/exe文件让你调用。

能否直接截屏保存或者把要处理的网页图片保存后作为输入数据传给图像模块?然后图像模块定期读取图片存储路径下图片即可

嘿嘿,出个馊主意,IE的bho

其实不用重写原来的库,只需要再写一个简单的库提供新的接口,内部用来调用你需要的接口,然后封装成支持nodejs调用的形式

ActiveX,或者服务端用nodejs调用,前端只做视频流上传的功能

我觉得可以这样,把你同事的C程序部署在服务器,让PHP或其他语言通过命令行调用,就像PHP调用imagemagick和ffmpeg这些命令工具来处理图像和视频那样,处理好了返回即可.

比如说,有个需求要用PHP实现二维码编解码,很多开发者可能首先想到的就是找一些PHP二维码相关的库,其实我觉得完全可以换个思路,直接找找Linux上有什么工具命令能够实现编解码,结果发现编码可以用qrencode,解码可以用zbar-tools,于是直接apt-get安装,然后就是PHP调用这些工具命令实现了二维码图片编解码.
编码: qrencode -o mi.png http://www.mi.com
解码: zbarimg mi.png

如果你要客户端调用,只能写浏览器插件。IE用ActiveX,火狐用NPAPI。Chrome旧版本可以用NPAPI,45之后版本只能用PPAPI方式。js里可以直接调用。举个例子就是像Flash插件那样,Flash插件是C语言写的吧?
如果是服务器端调用,Java的话可以用JNI/JNA。PHP什么的有C扩展模块。

https://github.com/cefsharp/C...

CefSharp lets you embed Chromium in .NET apps. It is a lightweight .NET wrapper around the Chromium Embedded Framework (CEF) by Marshall A. Greenblatt. About 30% of the bindings are written in C++/CLI with the majority of code here is C#. It can be used from C# or VB, or any other CLR language. CefSharp provides both WPF and WinForms web browser control implementations.

CefSharp is BSD licensed, so it can be used in both proprietary and free/open source applications. For the full details, see the LICENSE file.

Releases

Stable binaries are released on NuGet, and contain everything you need to embed Chromium in your .Net/CLR application. For usage see the Quick Start guide or FAQ #8.

CefSharp.WinForms
CefSharp.Wpf
CefSharp.OffScreen
Documentation

See the CefSharp.Wpf.Example or CefSharp.WinForms.Example projects for example web browsers built with CefSharp. They demo most of the available features.
See the CefSharp.MinimalExample project for a basic demo of using the CefSharp NuGet packages.
See the Wiki for work-in-progress documentation
See the FAQ for help with common issues
Upgrading from an earlier version of CefSharp? See the ChangeLog for breaking changes and upgrade tips.
CefSharp API generated from the souce code comments.
Contact

If you have a simple question please start by asking it on Gitter. Please keep the Issue Tracker for Bugs only please! Before submitting a PR please read CONTRIBUTING

Stackoverflow as always is a useful resource, you can post your more complex issues here. The CefSharp Google Group is no longer active. It is however a useful resource for archived questions/answers.

Branches & Forks

This is the official CefSharp fork, as maintained by the CefSharp community. You can also view the entire network of public forks/branches.

Development is done in the master branch. New features are preferably added in feature branches, if the changes are more than trivial. New PR's should be targeted against master.

When a new release is imminent a release branch is created. We try to avoid making public facing API changes in release branches (Adding new features is fine, just not breaking changes).

Build Status

CI Builds
Every commit on master produces a Nuget package. Use at your own risk! CefSharp MyGet Feed

emscripten基于llvm的,不知道能不能满足楼主需求

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题