Skip to content

Commit 9242a56

Browse files
committed
ANDROID: add tool to generate SB qrcodes
1 parent 2076fae commit 9242a56

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

web/qrcode/src/main/java/net/sourceforge/smallbasic/QrEncoder.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.ByteArrayOutputStream;
77
import java.io.DataInputStream;
88
import java.io.DataOutputStream;
9+
import java.io.File;
910
import java.io.FileInputStream;
1011
import java.io.FileNotFoundException;
1112
import java.io.FileOutputStream;
@@ -30,24 +31,44 @@
3031
* @author chrisws
3132
*/
3233
public class QrEncoder {
34+
private static final int DEF_SIZE = 500;
35+
3336
/**
3437
* @param args
3538
* @throws IOException
3639
* @throws WriterException
3740
*/
3841
public static void main(String[] args) throws IOException, WriterException {
39-
new QrEncoder().process(args[0]);
42+
File input = new File(args[0]);
43+
if (!input.exists() || !input.canRead() || !input.isFile()) {
44+
System.err.println("Invalid input: " + input.toString());
45+
} else {
46+
int size = DEF_SIZE;
47+
if (args.length > 1) {
48+
size = Integer.valueOf(args[1]);
49+
}
50+
new QrEncoder().process(input, size);
51+
}
4052
}
4153

4254
protected String cleanupCode(String fileText) {
55+
// convert DOS line endings to UNIX
56+
String result = fileText.replaceAll("\\r\\n", "\n");
57+
4358
// remove quotes
44-
String result = fileText.replaceAll("(?mi)^\\s*?rem.*[\\r\\n]", "");
45-
result = result.replaceAll("(?mi)^\\s*?'.*[\\r\\n]", "");
46-
result = result.replaceAll("(?mi)^\\s*?#.*[\\r\\n]", "");
59+
result = fileText.replaceAll("(?mi)^\\s*?rem.*[\\r\\n]", "");
60+
result = result.replaceAll("(?m)^\\s*?'.*[\\r\\n]", "");
61+
result = result.replaceAll("(?m)^\\s*?#.*[\\r\\n]", "");
62+
63+
// remove leading white space
64+
result = result.replaceAll("(?m)^\\s*", "");
4765

48-
// remove extra white space
66+
// remove double spaces
4967
result = result.replaceAll("[ \t]{2,}", " ");
5068

69+
// remove empty blank lines
70+
result = result.replaceAll("[\\r\\n]{2,}", "\n");
71+
5172
return result.trim();
5273
}
5374

@@ -78,9 +99,9 @@ protected byte[] createQRCode(String qrText, int size) throws WriterException, I
7899
return output.toByteArray();
79100
}
80101

81-
protected String getFileText(String fileName) throws IOException {
102+
protected String getFileText(File input) throws IOException {
82103
ByteArrayOutputStream out = new ByteArrayOutputStream();
83-
try (DataInputStream is = new DataInputStream(new FileInputStream(fileName));) {
104+
try (DataInputStream is = new DataInputStream(new FileInputStream(input));) {
84105
byte[] buf = new byte[1024];
85106
int len = is.read(buf);
86107
while (len != -1) {
@@ -91,11 +112,11 @@ protected String getFileText(String fileName) throws IOException {
91112
return out.toString("utf-8");
92113
}
93114

94-
protected void process(String fileName) throws IOException,
115+
protected void process(File input, int size) throws IOException,
95116
FileNotFoundException, WriterException {
96117

97118
// load the file text
98-
String fileText = getFileText(fileName);
119+
String fileText = getFileText(input);
99120

100121
// remove comments and empty white space
101122
fileText = cleanupCode(fileText);
@@ -104,9 +125,9 @@ protected void process(String fileName) throws IOException,
104125
String zipText = zipBase64(fileText);
105126

106127
// encode the zipped data into a qrcode
107-
byte[] out = createQRCode("smallbasic://x/" + zipText, 512);
128+
byte[] out = createQRCode("smallbasic://x/" + zipText, size);
108129

109-
writeOutput(out, fileName + ".png");
130+
writeOutput(out, input + ".png");
110131
}
111132

112133
protected void writeOutput(byte[] bytes, String fileName) throws FileNotFoundException, IOException {

web/qrcode/src/test/java/net/sourceforge/smallbasic/TestQrEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class TestQrEncoder {
1515
public void testCleanup() {
1616
QrEncoder inst = new QrEncoder();
1717
String fileText = " 'quote comments\r\n# hash comments\nREM remarks"
18-
+ "\r\n? 'hello'\n? 10";
19-
String expectedText = "? 'hello'\n? 10";
18+
+ "\r\n\r\n ? 'hello'\n ? 10\r\n\r\n\r\n?";
19+
String expectedText = "? 'hello'\n? 10\n?";
2020
String result = inst.cleanupCode(fileText);
2121
Assert.assertEquals(expectedText, result);
2222
}

0 commit comments

Comments
 (0)