将 HTML5 画布另存为单色 bmp
我有一个 HTML5 canvas 元素,我想将其作为单色 bmp 保存到服务器。我在网上看到过将其保存为彩色 bmp 的示例,但由于大小和色彩空间限制,我的应用程序需要将其保存为小型单色 bmp。有谁知道这个问题的简单解决方案(或者我是否必须编写一个类来从头开始创建 bmp 文件?)
I have an HTML5 canvas element which I would like to save to the server as a monochrome bmp. I have seen examples online of saving it as a color bmp, but because of size and colorspace restrictions my application has I need it to be a small monochrome bmp. Does anyone know of a simple solution for this (or am I going to have to write a class to create a bmp file from scratch?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现场演示
这是一个部分解决方案,因为它不会转换它是位图而不是png。您看到的第一张图像是画布,第二张是原始图像,第三张是画布中的数据放入图像中。
如果它必须是bmp,您可以尝试检查此内容以将数据更改为bmp
http://devpro.it/code/216.html
Live Demo
This is a partial solution, as it does not convert it to a bitmap but a png. The first image you see is the canvas, the second is the original image, and the third is the data from the canvas put into an image.
if it has to be a bmp you could try checking this out for changing the data to a bmp
http://devpro.it/code/216.html
我通过处理以下示例代码找到了一个很好的解决方案: rephrase.net/box/bitmap 作者提供了帮助我实现了一个可以将像素数组转换为单色位图的函数。从那里我能够获取画布数据并迭代它并将其解析为像素数组,我可以将其转换为单色 bmp。
我创建这个函数是为了方便将基于 alpha 值的画布数据(因为画布数据在我的应用程序中也是单色的)转换为可供 jsbmp 脚本使用的像素数组:
下面是 bmp_mono 的代码:
I found a good solution by working off of this example code: rephrase.net/box/bitmap The author helped me implement a function that could turn a pixel array into a monochrome bitmap. From there I was able to take canvas data and iterate through it and parse it into a pixel array that I could convert into a monochrome bmp.
I created this function to facilitate converting canvas data based on alpha values (since the canvas data is also monochrome in my application) to a pixel array which can be used by the jsbmp script:
And here is the code for bmp_mono: