Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ports/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
out/
target/
*.class
hello.png
58 changes: 58 additions & 0 deletions ports/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,61 @@ live site's `{"publicKey":{...},"privateJwk":{...}}` JSON.
(8192 KiB t=1/t=3 and 131072 KiB t=3 vectors); Blake2b matches RFC 7693 vectors.
- Wrong-password, tampered-code and forged-signature rejection verified both directions.
- Full suite: 15/15 cross-compat checks pass, plus v1 parity.

## Install

No dependencies. Java 17+.

```bash
javac -d out src/*.java examples/EncodeDecode.java
java -cp out EncodeDecode
```

`pom.xml` carries the package metadata (`mvn package` builds the jar from `src/`).

## Example

`examples/EncodeDecode.java`:

```java
byte[] payload = "Hello from Comb Code".getBytes(StandardCharsets.UTF_8);
Comb.Code code = Comb.encode(payload, -1, 1, 0, 0, 0);
ImageIO.write(Comb.render(code, 12), "png", new File("hello.png"));
Comb.Decoded d = Comb.decodeImage(ImageIO.read(new File("hello.png")));
System.out.println(new String(d.payload, StandardCharsets.UTF_8));
```

## API reference (`Comb`)

- `Comb.encode(payload, size, ecc, mode, type, logo)` returns a `Comb.Code`.
`size` -1 is automatic, 0..5 fixed; `ecc` 0..3 is 10/20/30/40% repair data;
`mode` 0 color, 1 black-and-white; `type` 0 text, 1 binary; `logo` 0.
`Comb.encode(payload)` uses the defaults.
- `Comb.render(code, radius)` returns a `BufferedImage`; `radius` is pixels per cell unit.
- `Comb.decodeImage(image)` returns a `Comb.Decoded`; the payload is only returned
when the header CRC-4 and payload CRC-32 pass.
- `Comb.capacity(size, ecc, mode)` returns the payload capacity.

## Photo reader

`src/PrismReader.java` reads codes from photos: detection, hypothesis search,
Reed-Solomon recovery, distance-fit recovery and the page retry chain. It is a
port of the live reader (`fast-reader.html`) and needs only the JDK.

```bash
java -cp out PrismReader scene.rgba WIDTH HEIGHT chain
```

`scene.rgba` is raw RGBA bytes. Modes: `chain` (full retry chain), `fast`
(single worker pass), `mix` (palette-mix attempt); with no mode it runs the
first detection stage only. The output is one JSON line with `ok`, and on
success the payload.

Limits: the reader has no `dd`, `pq` or `veil` recovery variants (the C, C++ and
C# readers have them), canvas resizes are approximated, and there is no
wall-clock cap. Synthetic bench scenes only, no physical-camera tests.

## License

Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit.
No warranty.
25 changes: 25 additions & 0 deletions ports/java/examples/EncodeDecode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Comb Code Java example: encode a text payload to a PNG, decode it back.
import javax.imageio.ImageIO;
import java.io.File;
import java.nio.charset.StandardCharsets;

public class EncodeDecode {
public static void main(String[] args) throws Exception {
String text = "Hello from Comb Code";
byte[] payload = text.getBytes(StandardCharsets.UTF_8);

// Encode: automatic size, 20% repair data, color mode, 12 px per cell unit.
Comb.Code code = Comb.encode(payload, -1, 1, 0, 0, 0);
ImageIO.write(Comb.render(code, 12), "png", new File("hello.png"));
System.out.println("encoded " + payload.length + " bytes -> hello.png");

// Decode it back. The payload is only returned when its CRC-32 passes.
Comb.Decoded d = Comb.decodeImage(ImageIO.read(new File("hello.png")));
String out = new String(d.payload, StandardCharsets.UTF_8);
System.out.println("decoded " + d.payload.length + " bytes: " + out);

boolean ok = out.equals(text);
System.out.println("round-trip " + (ok ? "OK" : "MISMATCH"));
System.exit(ok ? 0 : 1);
}
}
33 changes: 33 additions & 0 deletions ports/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.getcombcode</groupId>
<artifactId>comb-code</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Comb Code</name>
<description>Comb Code encoder, decoder, Comb Code Secure and photo reader.</description>
<url>https://getcombcode.github.io/</url>

<developers>
<developer>
<name>Avi Shor</name>
</developer>
</developers>

<licenses>
<license>
<name>Free to use, with or without credit</name>
<url>https://github.com/getcombcode/getcombcode.github.io/blob/main/LICENSE.md</url>
</license>
</licenses>

<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
2 changes: 1 addition & 1 deletion ports/java/src/Prism.java → ports/java/src/Comb.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public static Code encode(byte[] payload, int size, int ecc, int mode, int type,

// ---------------------------------------------------------------- render / PNG

/** Same geometry as Prism.render: hard-edged hexagons on white. */
/** Same geometry as Comb.render: hard-edged hexagons on white. */
public static BufferedImage render(Code code, int r) {
Layout l = code.layout;
int w = (int) Math.ceil((l.W + 2 * Q) * r);
Expand Down
8 changes: 4 additions & 4 deletions ports/java/src/PrismCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static void main(String[] a) throws Exception {
int logo = Integer.parseInt(arg(a, "logo", "0"));
String out = arg(a, "out", null);
if (out == null) throw new IllegalArgumentException("need --out");
Prism.Code code = Prism.encode(payload, size, ecc, mode, type, logo);
ImageIO.write(Prism.render(code, radius), "png", new File(out));
Comb.Code code = Comb.encode(payload, size, ecc, mode, type, logo);
ImageIO.write(Comb.render(code, radius), "png", new File(out));
String symbols = arg(a, "symbols", null);
if (symbols != null) {
StringBuilder sb = new StringBuilder();
Expand All @@ -43,14 +43,14 @@ public static void main(String[] a) throws Exception {
sb.append("]}");
Files.writeString(Paths.get(symbols), sb.toString());
}
var img = Prism.render(code, radius);
var img = Comb.render(code, radius);
System.out.println("wrote " + out + " (" + img.getWidth() + "x" + img.getHeight() + " px, grid "
+ code.layout.C + "x" + code.layout.R + " cells, size " + code.size + ", mode " + mode
+ ", ecc " + ecc + ", " + payload.length + " payload bytes)");
System.out.println("capacity at these settings: " + code.capacity.payload + " payload bytes");
} else if (a[0].equals("decode")) {
String png = a[1];
Prism.Decoded d = Prism.decodeImage(ImageIO.read(new File(png)));
Comb.Decoded d = Comb.decodeImage(ImageIO.read(new File(png)));
String out = arg(a, "out", null);
if (out != null) {
Files.write(Paths.get(out), d.payload);
Expand Down
Loading