[英]Convert ZPL (Base64) in image (PNG)
I need to convert a ZPL file into an image.我需要将 ZPL 文件转换为图像。 I already use the labelary web service, but I need a solution that is less dependent on an external application.我已经在使用 labelary Web 服务,但我需要一个对外部应用程序依赖较少的解决方案。
I am proceeding in the following way.我正在按以下方式进行。 I'm getting the base64 encoded part: eJzs281u4zYQAGAKLMCewmsPhfgaPWipPtIeVUC1GPiQYx6hr9FDgdL....rest of the code (Z64 encoded), which would be the image of the ZPL file, and I'm trying to convert this to a png image.我得到了 base64 编码的部分:eJzs281u4zYQAGAKLMCewmsPhfgaPWipPtIeVUC1GPiQYx6hr9FDgdL....其余的代码(Z64 编码),这将是 ZPL 文件的图像,我正在尝试将其转换为 png 图像。 I tried using the code below, but I get this error: Error: Could not find MIME for Buffer我尝试使用下面的代码,但出现此错误:错误:找不到缓冲区的 MIME
const fs = require('fs');
var base64Img = require('base64-img');
var Jimp = require('jimp');
try{
var data = fs.readFileSync('./test.zpl', 'base64')
} catch (err) {
console.log(err);
}
var buffer = Buffer.from(data, 'base64');
Jimp.read(buffer).then(res => {
return res
.quality(50)
.rotate(90)
.resize(250,250)
.writeAsync('out.png');
}).catch(err => {
console.log(err);
});
I tried using this method too, but it generates a png image, but it cannot be processed by the image viewer.我也尝试过使用这种方法,但它生成了一个 png 图像,但图像查看器无法对其进行处理。
const fs = require('fs');
var base64Img = require('base64-img');
try{
var data = fs.readFileSync('./test.zpl', 'base64')
} catch (err) {
console.log(err);
}
var buffer = Buffer.from(data, 'base64');
fs.writeFileSync("./out.png", buffer);
Grateful.感激。
Since you are dealing with a GRF (which is a header-less 1-bit bitmap), you will need a png-writing library to create the image - pngjs is the usual one to pull in:由于您正在处理 GRF(它是一个无标题的 1 位位图),您将需要一个 png-writing 库来创建图像 - pngjs 是通常的拉入库:
const fs = require('fs'),
zlib = require('zlib'),
PNG = require('pngjs').PNG;
let match = /(\d+),(\d+),(\d+),:Z64:([^:]+):/
.exec(fs.readFileSync('test.zpl', 'ascii'));
let size = +match[1];
let rowl = +match[3];
let grf = zlib.inflateSync(Buffer.from(match[4], 'base64'));
// These values are from the ^GF command
const png = new PNG({
width: rowl * 8,
height: size / rowl,
filterType: -1
});
let offs = 0;
let data = png.data;
for (let i = 0; i < size; i++) {
let byte = grf[i];
for (let bit = 0x80; bit; bit = bit >>> 1) {
let bw = (bit & byte) ? 0 : 255; // black (0) or white (255)
data[offs++] = bw;
data[offs++] = bw;
data[offs++] = bw;
data[offs++] = 255; // fully opaque
}
}
png.pack().pipe(fs.createWriteStream('test.png'));
That is from memory and some cut and paste of existing code, so there could be a typo...那是根据记忆和现有代码的一些剪切和粘贴,所以可能有一个错字......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.