denorg / qrcode

📇 Generate QR code images in Deno
https://denorg.github.io/qrcode/
MIT License
42 stars 11 forks source link

Could I use this to create a jpg files? #9

Closed edu-sonya-cc closed 1 year ago

edu-sonya-cc commented 1 year ago

I want to use node to create some qrcode jpg files, and then I find this library. But I don't know how to use it to create the jpg files.

edu-sonya-cc commented 1 year ago

I complete it by this library and the decode, imagemagick libraries.

import { qrcode } from "https://deno.land/x/qrcode@v2.0.0/mod.ts";

// import * as qrcodeStd from "https://deno.land/x/qrcode_std@v0.0.0/mod.ts"; import { decode } from "https://deno.land/x/fetchbase64@1.0.0/deps.ts"; import { ImageMagick, IMagickImage, initializeImageMagick, MagickFormat, } from "https://deno.land/x/imagemagick_deno/mod.ts";

await initializeImageMagick();

const GOAL_PATH: string = 'P:/2022/20220613a/3code/9_tools/deno/imageHelper/2_qrcode/';

// qrcode("bitcoin:ADDRESS?amount=0.5&label=ORDER"); // const fixedSizeImage = qrcode("bitcoin:ADDRESS?amount=0.5&label=ORDER", { size: 500 }); const homeQrCode = qrcode('http://edu.sonya.cc', { size: 100 }); console.log(homeQrCode);

const goalFileFullName = GOAL_PATH.concat('home.jpg');

homeQrCode.then((promise) => { const decodeResult: Uint8Array = decode(promise.replace(/^data:image\/\w+;base64,/, ''), 'base64'); console.log(decodeResult);

ImageMagick.read(decodeResult, (img: IMagickImage) => {
    // img.resize(200, 100);
    img.resize(100, 100);
    //img.blur(20, 6);

    img.write(
        (data: Uint8Array) => Deno.writeFile(goalFileFullName, data),
        MagickFormat.Jpeg,
    );
});

});

edu-sonya-cc commented 1 year ago

Now, I can batch create image files. These are the codes:

import { qrcode } from 'https://deno.land/x/qrcode@v2.0.0/mod.ts';

// import * as qrcodeStd from 'https://deno.land/x/qrcode_std@v0.0.0/mod.ts'; import { decode } from 'https://deno.land/x/fetchbase64@1.0.0/deps.ts'; import { ImageMagick, IMagickImage, initializeImageMagick, MagickFormat, } from 'https://deno.land/x/imagemagick_deno/mod.ts';

const GOAL_PATH: string = 'P:/ecs_person/websites/sonya.cc/edu_git/src/images/1home/teachers/'; const IS_PNG = true; const MAGICK_FORMAT = IS_PNG ? MagickFormat.Png : MagickFormat.Jpeg; const FILE_EXTENSION = IS_PNG ? '.png': '.jpg';

await initializeImageMagick();

// // Randomize failed. Sometimes, all are right. // [ // { name: 'khanacademy', link: 'https://www.khanacademy.org/' }, // { name: 'mozilla', link: 'https://developer.mozilla.org/en-US/' }, // { name: 'typescript', link: 'https://www.typescriptlang.org/' }, // { name: 'deno', link: 'https://deno.land/' }, // { name: 'canonCreativePark', link: 'https://creativepark.canon/sc/index.html' }, // { name: 'unicef', link: 'https://www.unicef.cn/' }, // { name: 'cctf', link: 'https://www.cctf.org.cn/' }, // { name: 'kidsNationalGeographic', link: 'https://kids.nationalgeographic.com/' }, // { name: 'vuejs', link: 'https://vuejs.org/' }, // { name: 'threejs', link: 'https://threejs.org/' }, // { name: 'fontawesome', link: 'https://fontawesome.com/' }, // { name: 'echarts', link: 'https://echarts.apache.org/' }, // { name: 'element-plus', link: 'https://element-plus.gitee.io/en-US/' }, // ].forEach(({ name, link }) => { // qrcode(link, { size: 100 }).then((encodedResult) => { // const decodeResult: Uint8Array = decode(encodedResult.replace(/^data:image\/\w+;base64,/, ''), 'base64'); //
// ImageMagick.read(decodeResult, (img: IMagickImage) => { // img.resize(100, 100); // // img.write( // (data: Uint8Array) => Deno.writeFile(GOAL_PATH.concat(name, FILE_EXTENSION), data), // MAGICK_FORMAT, // ); // }); // }); // });

// https://www.atatus.com/blog/introduction-to-async-await-in-typescript/ // function createQrCode(name: string, link: string) { // }

async function create(name: string, link: string) { // console.log(create('${name}', '${link}')); const encodedResult = await qrcode(link, { size: 100 }); // console.log(encodedResult);

const decodeResult: Uint8Array = decode(encodedResult.replace(/^data:image\/\w+;base64,/, ''), 'base64'); 

await ImageMagick.read(decodeResult, (img: IMagickImage) => {
    img.resize(100, 100);

    img.write(
        (data: Uint8Array) => Deno.writeFile(GOAL_PATH.concat(name, FILE_EXTENSION), data),
        MAGICK_FORMAT,
    );
});

}

[ { name: 'khanacademy', link: 'https://www.khanacademy.org/' }, { name: 'mozilla', link: 'https://developer.mozilla.org/en-US/' }, { name: 'typescript', link: 'https://www.typescriptlang.org/' }, { name: 'deno', link: 'https://deno.land/' }, { name: 'canonCreativePark', link: 'https://creativepark.canon/sc/index.html' }, { name: 'unicef', link: 'https://www.unicef.cn/' }, { name: 'cctf', link: 'https://www.cctf.org.cn/' }, { name: 'kidsNationalGeographic', link: 'https://kids.nationalgeographic.com/' }, { name: 'vuejs', link: 'https://vuejs.org/' }, { name: 'threejs', link: 'https://threejs.org/' }, { name: 'fontawesome', link: 'https://fontawesome.com/' }, { name: 'echarts', link: 'https://echarts.apache.org/' }, { name: 'element-plus', link: 'https://element-plus.gitee.io/en-US/' }, ].forEach(({ name, link }) => { create(name, link); });