Changed QrCode.getModule() in {Java, JavaScript, Python, C++} language versions to return Boolean instead of 0/1 - to match {C, Rust} language versions - and updated comments and usages.

pull/16/head
Project Nayuki 7 years ago
parent 6f9116dfcb
commit 9c1a25aba4

@ -159,11 +159,8 @@ int QrCode::getMask() const {
}
int QrCode::getModule(int x, int y) const {
if (0 <= x && x < size && 0 <= y && y < size)
return module(x, y) ? 1 : 0;
else
return 0; // Infinite white border
bool QrCode::getModule(int x, int y) const {
return 0 <= x && x < size && 0 <= y && y < size && module(x, y);
}
@ -180,7 +177,7 @@ std::string QrCode::toSvgString(int border) const {
bool head = true;
for (int y = -border; y < size + border; y++) {
for (int x = -border; x < size + border; x++) {
if (getModule(x, y) == 1) {
if (getModule(x, y)) {
if (head)
head = false;
else

@ -142,10 +142,10 @@ class QrCode final {
/*
* Returns the color of the module (pixel) at the given coordinates, which is either
* 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0).
* If the given coordinates are out of bounds, then 0 (white) is returned.
* false for white or true for black. The top left corner has the coordinates (x=0, y=0).
* If the given coordinates are out of bounds, then false (white) is returned.
*/
public: int getModule(int x, int y) const;
public: bool getModule(int x, int y) const;
/*

@ -168,7 +168,7 @@ static void printQr(const QrCode &qr) {
int border = 4;
for (int y = -border; y < qr.size + border; y++) {
for (int x = -border; x < qr.size + border; x++) {
std::cout << (qr.getModule(x, y) == 1 ? "##" : " ");
std::cout << (qr.getModule(x, y) ? "##" : " ");
}
std::cout << std::endl;
}

@ -88,7 +88,7 @@ int main() {
std::cout << qr.version << std::endl;
for (int y = 0; y < qr.size; y++) {
for (int x = 0; x < qr.size; x++)
std::cout << qr.getModule(x, y) << std::endl;
std::cout << (qr.getModule(x, y) ? 1 : 0) << std::endl;
}
} catch (const char *msg) {

@ -259,17 +259,14 @@ public final class QrCode {
/**
* Returns the color of the module (pixel) at the specified coordinates, which is either
* 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0).
* If the specified coordinates are out of bounds, then 0 (white) is returned.
* false for white or true for black. The top left corner has the coordinates (x=0, y=0).
* If the specified coordinates are out of bounds, then false (white) is returned.
* @param x the x coordinate, where 0 is the left edge and size&minus;1 is the right edge
* @param y the y coordinate, where 0 is the top edge and size&minus;1 is the bottom edge
* @return the module's color, which is either 0 (white) or 1 (black)
* @return the module's color, which is either false (white) or true (black)
*/
public int getModule(int x, int y) {
if (0 <= x && x < size && 0 <= y && y < size)
return modules[y][x] ? 1 : 0;
else
return 0; // Infinite white border
public boolean getModule(int x, int y) {
return 0 <= x && x < size && 0 <= y && y < size && modules[y][x];
}
@ -289,8 +286,8 @@ public final class QrCode {
BufferedImage result = new BufferedImage((size + border * 2) * scale, (size + border * 2) * scale, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) {
int val = getModule(x / scale - border, y / scale - border); // 0 or 1
result.setRGB(x, y, val == 1 ? 0x000000 : 0xFFFFFF);
boolean val = getModule(x / scale - border, y / scale - border);
result.setRGB(x, y, val ? 0x000000 : 0xFFFFFF);
}
}
return result;
@ -318,7 +315,7 @@ public final class QrCode {
boolean head = true;
for (int y = -border; y < size + border; y++) {
for (int x = -border; x < size + border; x++) {
if (getModule(x, y) == 1) {
if (getModule(x, y)) {
if (head)
head = false;
else

@ -84,7 +84,7 @@ public final class QrCodeGeneratorWorker {
System.out.println(qr.version);
for (int y = 0; y < qr.size; y++) {
for (int x = 0; x < qr.size; x++)
System.out.println(qr.getModule(x, y));
System.out.println(qr.getModule(x, y) ? 1 : 0);
}
} catch (IllegalArgumentException e) {

@ -35,7 +35,7 @@
* - Constructor QrCode(list<int> datacodewords, int mask, int version, QrCode.Ecc ecl)
* - Fields int version, size, mask
* - Field QrCode.Ecc errorCorrectionLevel
* - Method getModule(int x, int y) -> int
* - Method getModule(int x, int y) -> bool
* - Method drawCanvas(int scale, int border, HTMLCanvasElement canvas) -> void
* - Method toSvgString(int border) -> str
* - Enum Ecc:
@ -115,7 +115,7 @@ var qrcodegen = new function() {
} else if (initData instanceof qrcodegen.QrCode) {
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
modules[y][x] = initData.getModule(x, y) == 1;
modules[y][x] = initData.getModule(x, y);
isFunction[y][x] = initData.isFunctionModule(x, y);
}
}
@ -164,13 +164,10 @@ var qrcodegen = new function() {
/*---- Accessor methods ----*/
// (Public) Returns the color of the module (pixel) at the given coordinates, which is either
// 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0).
// If the given coordinates are out of bounds, then 0 (white) is returned.
// false for white or true for black. The top left corner has the coordinates (x=0, y=0).
// If the given coordinates are out of bounds, then false (white) is returned.
this.getModule = function(x, y) {
if (0 <= x && x < size && 0 <= y && y < size)
return modules[y][x] ? 1 : 0;
else
return 0; // Infinite white border
return 0 <= x && x < size && 0 <= y && y < size && modules[y][x];
};
// (Package-private) Tests whether the module at the given coordinates is a function module (true) or not (false).
@ -198,7 +195,7 @@ var qrcodegen = new function() {
var ctx = canvas.getContext("2d");
for (var y = -border; y < size + border; y++) {
for (var x = -border; x < size + border; x++) {
ctx.fillStyle = this.getModule(x, y) == 1 ? "#000000" : "#FFFFFF";
ctx.fillStyle = this.getModule(x, y) ? "#000000" : "#FFFFFF";
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
}
}
@ -219,7 +216,7 @@ var qrcodegen = new function() {
var head = true;
for (var y = -border; y < size + border; y++) {
for (var x = -border; x < size + border; x++) {
if (this.getModule(x, y) == 1) {
if (this.getModule(x, y)) {
if (head)
head = false;
else

@ -161,7 +161,7 @@ def print_qr(qrcode):
border = 4
for y in range(-border, qrcode.get_size() + border):
for x in range(-border, qrcode.get_size() + border):
print(u"\u2588 "[qrcode.get_module(x,y)] * 2, end="")
print(u"\u2588 "[1 if qrcode.get_module(x,y) else 0] * 2, end="")
print()
print()

@ -66,7 +66,7 @@ def main():
print(qr.get_version())
for y in range(qr.get_size()):
for x in range(qr.get_size()):
print(qr.get_module(x, y))
print(1 if qr.get_module(x, y) else 0)
except ValueError as e:
if e.args[0] != "Data too long":

@ -37,7 +37,7 @@ This module "qrcodegen", public members:
- Method get_size() -> int
- Method get_error_correction_level() -> QrCode.Ecc
- Method get_mask() -> int
- Method get_module(int x, int y) -> int
- Method get_module(int x, int y) -> bool
- Method to_svg_str(int border) -> str
- Enum Ecc:
- Constants LOW, MEDIUM, QUARTILE, HIGH
@ -226,9 +226,9 @@ class QrCode(object):
def get_module(self, x, y):
"""Returns the color of the module (pixel) at the given coordinates, which is either
0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0).
If the given coordinates are out of bounds, then 0 (white) is returned."""
return 1 if ((0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x]) else 0
False for white or True for black. The top left corner has the coordinates (x=0, y=0).
If the given coordinates are out of bounds, then False (white) is returned."""
return (0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x]
# ---- Public instance methods ----
@ -241,7 +241,7 @@ class QrCode(object):
parts = []
for y in range(-border, self._size + border):
for x in range(-border, self._size + border):
if self.get_module(x, y) == 1:
if self.get_module(x, y):
parts.append("M{},{}h1v1h-1z".format(x + border, y + border))
return """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

@ -100,7 +100,7 @@ Examples:
border = 4
for y in range(-border, qr1.get_size() + border):
for x in range(-border, qr1.get_size() + border):
color = qr1.get_module(x, y) # 0 for white, 1 for black
color = qr1.get_module(x, y) # False for white, True for black
# (... paint the module onto pixels ...)
More complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/python/qrcodegen-demo.py .

Loading…
Cancel
Save