简体   繁体   English

使用 JavaScript 在图像上放置生成的条形码

[英]Placing a generated bar code on an image using JavaScript

I am using the JsBarcode JavaScript library to generate a barcode.我正在使用 JsBarcode JavaScript 库来生成条形码。 I want to place this barcode at a specific place on an image and then download the image.我想将此条形码放在图像上的特定位置,然后下载图像。 How can I do that?我怎样才能做到这一点?

I am storing data in cloud-firestore and then I use the Document id to generate a 128 code.我将数据存储在 cloud-firestore 中,然后使用 Document id 生成 128 代码。 Using the JSBarcode, I can get the id and generate a barcode but I don't know how to place that barcode on an image at a specific place and then download the image.使用 JSBarcode,我可以获取 id 并生成条形码,但我不知道如何将该条形码放置在特定位置的图像上,然后下载图像。

Sorry if I didn't follow any Stackoverflow rules because I am new here.对不起,如果我没有遵守任何 Stackoverflow 规则,因为我是新来的。

Here's the html code:这是 html 代码:

<svg id="barcode"></svg>

JavaScript code: JavaScript 代码:

db.collection("qrcode").add({
            category: category_item,
        }).then(function (docRef) {
            db.collection("qrcode").doc(docRef.id).update({
                category: category_item,
                collected_by: "-",
                date: new firebase.firestore.Timestamp.now().seconds * 1000,
                id: docRef.id,
                item: item,
                status: "Pending",
            }).then(function () {
                window.alert("Data added successfully!");
            }).catch(function (error) {
                window.alert("There was some error. Please try again.");
                console.log(error.code);
                console.log(error.message);
            });
            JsBarcode("#barcode", docRef.id); //Generating the barcode here
        }).catch ...

Copying my answer from the comments, now that the question is open again:从评论中复制我的答案,现在问题再次打开:

I haven't had to do this before, but the first thing that comes to mind is using HTML Canvas to draw the image.我以前不必这样做,但首先想到的是使用 HTML Canvas 来绘制图像。 You can then take the SVG, give it the right width/height and draw it on the canvas.然后,您可以使用 SVG,为其设置正确的宽度/高度,然后将其绘制在 canvas 上。 You can then convert the canvas to an img and display it.然后您可以canvas 转换为 img 并显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM