Steve
06/11/2024, 12:29 AMseancorfield
seancorfield
dswitzer
06/11/2024, 11:14 AMString text = "Hello, world!"; // User-supplied Unicode text
This declares a variable "text" that is of type String.
Since CF is a typeless language, the type of the variable does not matter. So that line can be translated to CFML as:
var text = "Hello, world!";
Let's take a look at the next line:
QrCode.Ecc errCorLvl = QrCode.Ecc.LOW; // Error correction level
Once again, the type (e.g. QrCode.Ecc) is irrelevant to CFML, so ignore it on our CFML code. The QrCode
is a reference to the io.nayuki.qrcodegen.QrCode
class defined in one of the import
statements. So this line can be translated to something like:
# this emulates the "import" line
var QrCode = createObject("java", "io.nayuki.qrcodegen.QrCode");
# now we can use our imported class
var errCorLvl = QrCode.Ecc.LOW;
Now, let's move to a line with a constructor:
File svgFile = new File("hello-world-QR.svg");
Once again, we can ignore the type declaration. So now do we emulate the constructor?
CFML uses the same pattern as components to pass in the constructor arguments, so that line can be translated to:
var svgFile = createObject("java", "java.io.File").init("hello-world-QR.png");
That should be enough to get you started...seancorfield
QrCode.Ecc
is a nested class so I don't think your errCorLvl
assignment will work... I can't remember how CFML handles nested classes but it need:
var QrCodeEcc = createObject("java", "io.nayuki.qrcodegen.QrCode$Ecc");
var errCorLvl = QrCodeEcc.LOW;
(different languages handle interop with nested classes differently but most use Outer$Inner
as the reference)Steve
06/11/2024, 6:40 PMSteve
06/11/2024, 7:52 PMQrCode qr = QrCode.encodeText(text, errCorLvl);
and I don't see a constructor. Yet the javadoc shows a constructor of
*QrCode*(int ver, *QrCode.Ecc* ecl, byte[] dataCodewords, int msk)
Refs:
⢠https://github.com/nayuki/QR-Code-generator/blob/master/java/QrCodeGeneratorDemo.java?ts=4
⢠https://www.nayuki.io/res/qr-code-generator-library/javadoc/io/nayuki/qrcodegen/QrCode.html
BTW, I started this path because the latest Java broke the google QR generation library which had CF examples. (Unable to make public final java.nio.file.Path sun.nio.fs.UnixFileSystem.getPath(java.lang.String,java.lang.String[]) accessible: module java.base does not opens sun.nio.fs to unnamed module...)
I should suggest a class on this at the CF summit. @Mark Takata (Adobe) ^^seancorfield
seancorfield
QrCode.Ecc.LOW
told me it was the LOW
static field of the class Ecc
nested inside the class QrCode
and Java encodes nested classes in bytecode as top-level classes but with the name Outer$Inner
.seancorfield
createObject("java", "the.package.for.TheClass")
without calling .init()
to run a constructor.foundeo
Steve
06/11/2024, 8:24 PMSteve
06/11/2024, 11:37 PMvar TYPE_INT_RGB = createObject("java", "java.awt.image.BufferedImage$TYPE_INT_RGB");
did not work, instead I used
var TYPE_INT_RGB = createObject("java", "java.awt.image.BufferedImage").TYPE_INT_RGB;
I know there is probably a valid reason for the discrepancy, but this is where I struggle with Java-->CF conversion.Steve
06/11/2024, 11:44 PMseancorfield
BufferedImage.TYPE_INT_RGB
indicates a static field in a class, not a nested class. you can tell from the context of the Java code (or the docs)seancorfield