Improve image normalising
parent
ae5128a33a
commit
d0e428b728
|
@ -23,11 +23,17 @@ class ParseQRCode extends Operation {
|
||||||
|
|
||||||
this.name = "Parse QR Code";
|
this.name = "Parse QR Code";
|
||||||
this.module = "Image";
|
this.module = "Image";
|
||||||
this.description = "Reads an image file and attempts to detect and read a QR code from the image.";
|
this.description = "Reads an image file and attempts to detect and read a QR code from the image.<br><br><u>Normalise Image</u><br>Attempt to normalise the image before parsing it, to try and improve detection of a QR code.";
|
||||||
this.infoURL = "https://wikipedia.org/wiki/QR_code";
|
this.infoURL = "https://wikipedia.org/wiki/QR_code";
|
||||||
this.inputType = "byteArray";
|
this.inputType = "byteArray";
|
||||||
this.outputType = "string";
|
this.outputType = "string";
|
||||||
this.args = [];
|
this.args = [
|
||||||
|
{
|
||||||
|
"name": "Normalise image",
|
||||||
|
"type": "boolean",
|
||||||
|
"value": true
|
||||||
|
}
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,38 +43,55 @@ class ParseQRCode extends Operation {
|
||||||
*/
|
*/
|
||||||
async run(input, args) {
|
async run(input, args) {
|
||||||
const type = Magic.magicFileType(input);
|
const type = Magic.magicFileType(input);
|
||||||
|
const [normalise] = args;
|
||||||
// Make sure that the input is an image
|
// Make sure that the input is an image
|
||||||
if (type && type.mime.indexOf("image") === 0){
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
|
let normalisedImage = null;
|
||||||
return new Promise((resolve, reject) => {
|
if (normalise){
|
||||||
// Read the input
|
// Process the image to be easier to read by jsqr
|
||||||
jimp.read(Buffer.from(input))
|
// Disables the alpha channel
|
||||||
.then(image => {
|
// Sets the image default background to white
|
||||||
image.rgba(false); // Disable RGBA (uses just RGB)
|
// Normalises the image colours
|
||||||
|
// Makes the image greyscale
|
||||||
// Get the buffer of the new image and read it in Jimp
|
// Converts image to a JPEG
|
||||||
// Don't actually need the new image buffer, just need
|
normalisedImage = await new Promise((resolve, reject) => {
|
||||||
// Jimp to refresh the current object
|
jimp.read(Buffer.from(input))
|
||||||
image.getBuffer(image.getMIME(), (err, buffer) => {
|
.then(image => {
|
||||||
jimp.read(buffer)
|
image
|
||||||
.then(newImage =>{
|
.rgba(false)
|
||||||
// If the image has been read correctly, try to find a QR code
|
.background(0xFFFFFFFF)
|
||||||
if (image.bitmap != null){
|
.normalize()
|
||||||
const qrData = jsqr(image.bitmap.data, image.getWidth(), image.getHeight());
|
.greyscale()
|
||||||
if (qrData != null) {
|
.getBuffer(jimp.MIME_JPEG, (error, result) => {
|
||||||
resolve(qrData.data);
|
resolve([...result]);
|
||||||
} else {
|
|
||||||
log.error(image.bitmap);
|
|
||||||
reject(new OperationError("Error parsing QR code from image."));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
reject(new OperationError("Error reading the image data."));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
reject(new OperationError("Error reading the image file."));
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
normalisedImage = input;
|
||||||
|
}
|
||||||
|
if (normalisedImage instanceof OperationError){
|
||||||
|
return normalisedImage;
|
||||||
|
}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
jimp.read(Buffer.from(normalisedImage))
|
||||||
|
.then(image => {
|
||||||
|
if (image.bitmap != null){
|
||||||
|
const qrData = jsqr(image.bitmap.data, image.getWidth(), image.getHeight());
|
||||||
|
if (qrData != null){
|
||||||
|
resolve(qrData.data);
|
||||||
|
} else {
|
||||||
|
reject(new OperationError("Couldn't read a QR code from the image."));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reject(new OperationError("Error reading the normalised image file."));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
reject(new OperationError("Error opening the image. Are you sure this is an image file?"));
|
reject(new OperationError("Error reading the normalised image file."));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue